# ViolinPlot x-axis

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...

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))

• How do I re-factor the grouping category?
– mmpp
2 days ago

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")
$$$$
`
• I have attached an image as well as a better description.
– mmpp
Sep 28 at 19:03