<p>You need to enforce the order you need on the read-name column. Let's say <code>df</code> is your data frame with <code>read.name</code> and <code>length</code> as the two columns.</p>
<p>This is a fairly easy operation.</p>
<pre class="lang-r prettyprint-override"><code>df &lt;- df[order(df$length),]
df$read.name &lt;- factor(df$read.name, levels = df$read.name, ordered = TRUE)
ggplot(df, ...) #Your current ggplot code goes here
</code></pre>

CC BY-SA 4.0
2023-05-17T19:09:50.087Z
Hmm this gave me an error `factor level [99] is duplicated` but I guess I should just google that too, right? No offense but there's no need to be rude with your response. I did Google this question extensively before making this post and the order and factor methods did pop up and I did try them but didn't get it to work which is why I posted this....
rimo
CC BY-SA 4.0
2023-05-18T02:22:49.267Z
My apologies @rimo I'm a bit slow on this. The solution to your data is that it needs ranking before its plotted, for example `df$rank <-  rank(df$length)` then if you place the `df$rank` into `ggplot2` that should work. RamRS is using `order` instead.  If it was Python I'd code it for you (coding is easier than writing). It is fairly easy with R however.
M__
CC BY-SA 4.0
2023-05-18T13:39:11.257Z
Yes, you should google that too. That's how we learn. Google error messages and try stuff until you either understand the whole picture or run into something a lot beyond your understanding. The error says your read names are duplicated. In that case, I'd either make them unique by adding a new component, use `rank` as M__ suggests or plot length vs 1:nrow(df) in the x axis so sorting by x-axis doesn't do a thing, as your plot doesn't really need read names on an axis.
Ram RS
CC BY-SA 4.0
2023-05-17T18:42:17.077Z
2023-05-18T02:26:27.537Z
2023-05-18T02:26:27.537Z
Ram RS
<p>I'm trying to make a plot that looks like this:</p>
<p><a href="https://i.sstatic.net/p7vGO.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/p7vGO.png" alt="excel plot" /></a></p>
<p>This is a rudimentary plot I did in excel. It's a plot of reads by length. Very simple nothing crazy to it. The important thing to note is that it's plotting the data in increasing order of length.</p>
<p>When I try to use ggplot to make this same plot in R it does something like this:</p>
<p><a href="https://i.sstatic.net/l5ueD.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/l5ueD.png" alt="r plot" /></a></p>
<p>Where it orients the read names in alphabetical order not in ascending order of the lengths. The dataframe/table I have read into R is already listed in ascending length order. For example:</p>
<pre><code>1   c6dd66b4-dab5-484b-92b6-c4ecdcc155b1   1570
2   ecf15d4f-15b1-4636-a97d-e0928281e311   1739   
3   461f05c7-0fc6-4564-95df-6188966fe616   1790       
4   e989c32c-491e-4af1-a37d-4d4f5c87a3d9   1797       
5   ce94c2b3-442b-4b8c-bd59-6d7c23d5fe3d   1824       
6   ed15ea16-a28d-42ca-9a07-2818d90ddd4b   1832 
7   481550cd-6f87-44c7-915f-f0f3d638ffe4   1853      
</code></pre>
<p>As you can see the lengths increase as you go down and the read names are in no particular order. Can someone help me read in the correct order to make this plot actually work in ggplot?</p>

CC BY-SA 4.0
2023-05-18T02:51:21.800Z
the technical term you want is called "ranking". The question (and the answer) will be much clearer once that is used. `rank` is one of the possible R methods that will do this.
M__
<r><ggplot2>
change the order in ggplot
# change the order in ggplot

(Asked by: username_0 on 2023-05-17)

I'm trying to make a plot that looks like this:

This is a rudimentary plot I did in excel. It's a plot of reads by length. Very simple nothing crazy to it. The important thing to note is that it's plotting the data in increasing order of length.
When I try to use ggplot to make this same plot in R it does something like this:

Where it orients the read names in alphabetical order not in ascending order of the lengths. The dataframe/table I have read into R is already listed in ascending length order. For example:
1   c6dd66b4-dab5-484b-92b6-c4ecdcc155b1   1570
2   ecf15d4f-15b1-4636-a97d-e0928281e311   1739   
3   461f05c7-0fc6-4564-95df-6188966fe616   1790       
4   e989c32c-491e-4af1-a37d-4d4f5c87a3d9   1797       
5   ce94c2b3-442b-4b8c-bd59-6d7c23d5fe3d   1824       
6   ed15ea16-a28d-42ca-9a07-2818d90ddd4b   1832 
7   481550cd-6f87-44c7-915f-f0f3d638ffe4   1853      

As you can see the lengths increase as you go down and the read names are in no particular order. Can someone help me read in the correct order to make this plot actually work in ggplot?

**Tags:** r, ggplot2
> **Comments:**
> **username_1**: the technical term you want is called "ranking". The question (and the answer) will be much clearer once that is used. `rank` is one of the possible R methods that will do this.  

---

## 1 answers

### Answer by username_2 on 2023-05-17. Score: -2

You need to enforce the order you need on the read-name column. Let's say df is your data frame with read.name and length as the two columns.
This is a fairly easy operation.
df <- df[order(df$length),]
df$read.name <- factor(df$read.name, levels = df$read.name, ordered = TRUE)
ggplot(df, ...) #Your current ggplot code goes here

> **Comments:**
> **username_0**: Hmm this gave me an error `factor level [99] is duplicated` but I guess I should just google that too, right? No offense but there's no need to be rude with your response. I did Google this question extensively before making this post and the order and factor methods did pop up and I did try them but didn't get it to work which is why I posted this....  
> **username_1**: My apologies @rimo I'm a bit slow on this. The solution to your data is that it needs ranking before its plotted, for example `df$rank <-  rank(df$length)` then if you place the `df$rank` into `ggplot2` that should work. RamRS is using `order` instead.  If it was Python I'd code it for you (coding is easier than writing). It is fairly easy with R however.  
> **username_2**: Yes, you should google that too. That's how we learn. Google error messages and try stuff until you either understand the whole picture or run into something a lot beyond your understanding. The error says your read names are duplicated. In that case, I'd either make them unique by adding a new component, use `rank` as M__ suggests or plot length vs 1:nrow(df) in the x axis so sorting by x-axis doesn't do a thing, as your plot doesn't really need read names on an axis.  

---

