<p>Seurat does violin plots using ggplot, which orders based on the factor levels. You need to re-factor your grouping category so that the levels matches your preferred order:</p>
<pre><code># default level sort order is alphabetical
obj[[&quot;grouping&quot;]] &lt;- factor(unlist(obj[[&quot;grouping&quot;]])) # refactor
VlnPlot(obj, group.by = &quot;grouping&quot;, features = c(&quot;classical_basal&quot;),
        pt.size = 0) +
  NoLegend() + theme(axis.text.x = element_text(angle = 90, hjust = 1))
</code></pre>

CC BY-SA 4.0
2021-12-03T21:43:40.273Z
How do I re-factor the grouping category?
mmpp
CC BY-SA 4.0
2021-09-29T03:57:59.503Z
2021-12-04T05:58:45.827Z
2021-12-04T05:58:45.827Z
gringer
<p>It is difficult to say exactly what you would like to achieve without an example.</p>
<p>But if you would like an individual axis based on a factor like patient maybe you want to use facet_wrap for the Patient variable like in the example below?</p>
<pre><code>library(ggplot2)

data &lt;- iris

ggplot(data, aes(x=Sepal.Width, y=Sepal.Length, fill=Species)) +
  geom_violin() +
  facet_wrap(~Species, scales=&quot;free_x&quot;)
<span class="math-container">```</span>
</code></pre>

CC BY-SA 4.0
2021-09-28T19:03:45.900Z
I have attached an image as well as a better description.
mmpp
CC BY-SA 4.0
2021-09-23T01:43:58.997Z
2021-09-23T01:43:58.997Z
Lukas
<p><a href="https://i.sstatic.net/kuE63.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/kuE63.png" alt="enter image description here" /></a>I'm trying to set the x-axis in my Violin Plot to go by each patient rather than it being scattered.</p>
<pre><code>VlnPlot(obj, features = c(&quot;classical_basal&quot;), pt.size = 0) + NoLegend() + theme(axis.text.x = element_text(angle = 90, hjust = 1))
</code></pre>
<p>The above code gives me the x-axis but there is no pattern or order.
I would like the x-axis to be grouped with the HTB, for example ALL of the HTB2867 first, followed by HTB2876, etc...</p>

<r><seurat><single-cell><ggplot2>
ViolinPlot x-axis
# ViolinPlot x-axis

(Asked by: username_0 on 2021-09-22)

I'm trying to set the x-axis in my Violin Plot to go by each patient rather than it being scattered.
VlnPlot(obj, features = c("classical_basal"), pt.size = 0) + NoLegend() + theme(axis.text.x = element_text(angle = 90, hjust = 1))

The above code gives me the x-axis but there is no pattern or order.
I would like the x-axis to be grouped with the HTB, for example ALL of the HTB2867 first, followed by HTB2876, etc...

**Tags:** r, seurat, single-cell, ggplot2

---

## 2 answers

### Answer by username_1 on 2021-09-29. Score: 2

Seurat does violin plots using ggplot, which orders based on the factor levels. You need to re-factor your grouping category so that the levels matches your preferred order:
# default level sort order is alphabetical
obj[["grouping"]] <- factor(unlist(obj[["grouping"]])) # refactor
VlnPlot(obj, group.by = "grouping", features = c("classical_basal"),
        pt.size = 0) +
  NoLegend() + theme(axis.text.x = element_text(angle = 90, hjust = 1))

> **Comments:**
> **username_0**: How do I re-factor the grouping category?  

---

### Answer by username_2 on 2021-09-23. Score: 0

It is difficult to say exactly what you would like to achieve without an example.
But if you would like an individual axis based on a factor like patient maybe you want to use facet_wrap for the Patient variable like in the example below?
library(ggplot2)

data <- iris

ggplot(data, aes(x=Sepal.Width, y=Sepal.Length, fill=Species)) +
  geom_violin() +
  facet_wrap(~Species, scales="free_x")
```

> **Comments:**
> **username_0**: I have attached an image as well as a better description.  

---

