<p>ggplot2 uses factors on the underlying data frame. To have the data plotted in a custom order, apply a custom ordered factor on the underlying dataset.</p>
<p>For example, if <code>clusters_per_sample$sample</code> is your custom-ordered vector, do this:</p>
<pre><code>clusters_per_sample<span class="math-container">$sample &lt;- factor(clusters_per_sample$</span>sample, levels = unique(clusters_per_sample$sample), ordered = TRUE)
</code></pre>
<p>Now when you plot <code>sample</code> as an aesthetic, ggplot has a factor component to work with already, so it won't try and make a new one (this new factor would have been in alphabetical order, which we circumvent with the <code>ordered=TRUE</code> parameter in our custom factor).</p>

CC BY-SA 4.0
2021-08-18T17:25:40.703Z
Where do I add that line in my code?
mmpp
CC BY-SA 4.0
2021-08-18T22:11:11.033Z
Add it in before passing the object to `ggplot()`. This step is part of preparing your data for plotting.
Ram RS
CC BY-SA 4.0
2021-08-06T17:42:59.077Z
2021-08-06T17:42:59.077Z
Ram RS
<p>I'm inputting a .csv file and then plotting a barplot based on the colnames but when I go plot the order the names are being alphabetized rather than get the order that is in the .csv file. How to I prevent that from happening?</p>
<pre><code>p2 = ggplot(data=clusters_per_sample, aes(x=sample, y=Perc, fill=cluster)) +
  geom_bar(stat=&quot;identity&quot;)+
  scale_fill_manual(values = getPalette(22))+
  theme_minimal() + labs(y=&quot;percentage of total # cells in the sample&quot;) + theme(axis.text.x = element_text(angle = 90, face = &quot;bold&quot;)) + theme(axis.text.x = element_text(size=12, color=&quot;black&quot;)) 
</code></pre>
<p>I tried this to get rid of the alphabetizing but my bars are very thin when doing so:</p>
<pre><code>p2 = ggplot(data=clusters_per_sample_2, aes(x=sample, y=Perc, fill=cluster)) +
geom_bar(stat=&quot;identity&quot;)+ scale_x_discrete(limits=clusters_per_sample_2$sample) +
heme_minimal() + labs(y=&quot;percentage of total # cells in the sample&quot;) + theme(axis.text.x = element_text(angle = 90, face = &quot;bold&quot;)) + theme(axis.text.x = element_text(size=12, color=&quot;black&quot;)) 
</code></pre>
<p>Any suggestions?</p>

<r><ggplot2><graphs>
Barplot without alphabetizing x-axis
# Barplot without alphabetizing x-axis

(Asked by: username_0 on 2021-08-06)

I'm inputting a .csv file and then plotting a barplot based on the colnames but when I go plot the order the names are being alphabetized rather than get the order that is in the .csv file. How to I prevent that from happening?
p2 = ggplot(data=clusters_per_sample, aes(x=sample, y=Perc, fill=cluster)) +
  geom_bar(stat="identity")+
  scale_fill_manual(values = getPalette(22))+
  theme_minimal() + labs(y="percentage of total # cells in the sample") + theme(axis.text.x = element_text(angle = 90, face = "bold")) + theme(axis.text.x = element_text(size=12, color="black")) 

I tried this to get rid of the alphabetizing but my bars are very thin when doing so:
p2 = ggplot(data=clusters_per_sample_2, aes(x=sample, y=Perc, fill=cluster)) +
geom_bar(stat="identity")+ scale_x_discrete(limits=clusters_per_sample_2$sample) +
heme_minimal() + labs(y="percentage of total # cells in the sample") + theme(axis.text.x = element_text(angle = 90, face = "bold")) + theme(axis.text.x = element_text(size=12, color="black")) 

Any suggestions?

**Tags:** r, ggplot2, graphs

---

## 1 answers

### Answer by username_1 on 2021-08-06. Score: 0 | Accepted answer

ggplot2 uses factors on the underlying data frame. To have the data plotted in a custom order, apply a custom ordered factor on the underlying dataset.
For example, if clusters_per_sample$sample is your custom-ordered vector, do this:
clusters_per_sample$sample <- factor(clusters_per_sample$sample, levels = unique(clusters_per_sample$sample), ordered = TRUE)

Now when you plot sample as an aesthetic, ggplot has a factor component to work with already, so it won't try and make a new one (this new factor would have been in alphabetical order, which we circumvent with the ordered=TRUE parameter in our custom factor).

> **Comments:**
> **username_0**: Where do I add that line in my code?  
> **username_1**: Add it in before passing the object to `ggplot()`. This step is part of preparing your data for plotting.  

---

