0
$\begingroup$

I want to create a column plot for my single cell analysis that has 2 sample types, a normal and PDAC. I want to show, the percentage of different cell types per sample type.

How do I go about determining the percent of each cell type in each condition and then go about plotting the column plot? Also, is it possible to determine the SD from the dataset and add it?

dput(head(all@meta.data))

structure(list(orig.ident = c("PDAC", "PDAC", "PDAC", "PDAC", 
"PDAC", "PDAC"), nCount_RNA = c(7945, 7616, 7849, 5499, 853, 
1039), nFeature_RNA = c(2497L, 2272L, 2303L, 2229L, 509L, 588L
), percent.mt = c(2.63058527375708, 3.7421218487395, 4.9433048796025, 
0.490998363338789, 0.234466588511137, 0.192492781520693), RNA_snn_res.0.5 = structure(c(5L, 
5L, 5L, 5L, 1L, 6L), .Label = c("0", "1", "2", "3", "4", "5", 
"6", "7", "8", "9", "10", "11", "12", "13", "14", "15", "16", 
"17", "18", "19"), class = "factor"), seurat_clusters = structure(c(5L, 
5L, 5L, 5L, 1L, 6L), .Label = c("0", "1", "2", "3", "4", "5", 
"6", "7", "8", "9", "10", "11", "12", "13", "14", "15", "16", 
"17", "18", "19"), class = "factor"), S.Score = c(0.0171539298241524, 
-0.00599651468836135, 0.011058574403656, -0.0424413740104151, 
0.03549521163095, -0.0243347616753686), G2M.Score = c(-0.0591387992008088, 
0.0253275642795205, -0.0512402869839816, -0.0239076248512967, 
-0.0263164126515597, -0.0339086517371782), Phase = structure(c(3L, 
2L, 3L, 1L, 3L, 1L), .Label = c("G1", "G2M", "S"), class = "factor"), 
    old.ident = structure(c(1L, 1L, 1L, 1L, 1L, 3L), .Label = c("Fibroblast", 
    "T Cell", "Endothelial", "Tumor", "Stellate", "Macrophage", 
    "B Cell", "Mast Cell", "Acinar", "Endocrine", "Exocrine"), class = "factor")), row.names = c("PDAC_AAACCCAGTCGGCTAC-1", 
"PDAC_AAACGAAGTCCAGGTC-1", "PDAC_AAAGAACAGCAAGTGC-1", "PDAC_AAAGAACAGGATGAGA-1", 
"PDAC_AAAGTGACATCCGTTC-1", "PDAC_AACAACCAGGAAGTAG-1"), class = "data.frame")
$\endgroup$
3
  • $\begingroup$ You can start with prop.table() to get the percentages. $\endgroup$ – haci Jan 28 '20 at 8:13
  • $\begingroup$ @haci So I would do prop.table(all@meta.data) $\endgroup$ – mmpp Jan 28 '20 at 23:55
  • $\begingroup$ prop.table() requires a table as input so you can try something like prop.table(table(object@meta.data$column1, object@meta.data$column2). $\endgroup$ – haci Jan 30 '20 at 9:11
0
$\begingroup$

You can make use of table() and prop.table() to calculate the number and proportions of your cell types with respect to tissue of origin.

prop.table() requires a table as input so you can start with something like prop.table(table(object@meta.data$cell_type, object@meta.data$tissue). This will create an objet of class table.

Next step is to create a data frame with as.data.frame(your_table_object) and then to convert it to "long format", for example with tidyr::pivot_longer(). You can feed these data in long format to ggplot().

$\endgroup$
0
$\begingroup$

If I understand your question correctly and you have dplyr and ggplot installed, you could try something like (if a is your data.frame)

a %>% 
dplyr::group_by(orig.ident, old.ident) %>% 
dplyr::summarise(count  = n()) %>% 
ggplot(aes(x=orig.ident, y=count)) + geom_bar(stat='identity', aes(group=old.ident), position='dodge')
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.