I am using Seurat to analyze my single cell data. I have 2 conditions, treated and untreated. I am trying to create a stacked bar graph in order to show the differences in cell types for each condition but need to collect the percentages of each cluster for the specific cell types. How to I put together a sheet that contains the percentages of each cluster and the make a stacked bar graph of comparison of each condition.
2 Answers
pt <- table(Idents(myobj), myobj$orig.ident)
pt <- as.data.frame(pt)
pt$Var1 <- as.character(pt$Var1)
ggplot(pt, aes(x = Var2, y = Freq, fill = Var1)) +
theme_bw(base_size = 15) +
geom_col(position = "fill", width = 0.5) +
xlab("Sample") +
ylab("Proportion") +
scale_fill_manual(values = brewer.pal(12, "Paired")) +
theme(legend.title = element_blank())
This shall work. myobj
is your seurat object. This is my old code so if I remember correct, orig.ident
is your treated and untreated conditions. Idents
should give you cell clusters.
If this does not work, please let me know
You can access these data from a Seurat object as columns of [email protected]
and tabulate them.
There are functions such as “propellor” which will also implement this:
https://github.com/Oshlack/speckle
Note that a Seurat vignette also explores this topic:
https://satijalab.org/seurat/archive/v3.0/interaction_vignette.html
You need to be careful when doing this to ensure different proportions are biological as single cell data is prone to technical errors.