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

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

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

CC BY-SA 4.0
2020-01-30T09:21:41.813Z
2020-01-30T09:21:41.813Z
haci
<p>If I understand your question correctly and you have <code>dplyr</code> and <code>ggplot</code> installed, you could try something like (if a is your data.frame)</p>

<pre><code>a %&gt;% 
dplyr::group_by(orig.ident, old.ident) %&gt;% 
dplyr::summarise(count  = n()) %&gt;% 
ggplot(aes(x=orig.ident, y=count)) + geom_bar(stat='identity', aes(group=old.ident), position='dodge')
</code></pre>

CC BY-SA 4.0
2020-02-03T11:11:19.227Z
2020-02-03T11:11:19.227Z
user438383
<p>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.</p>

<p>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?</p>

<p><strong>dput(head(all@meta.data))</strong></p>

<pre><code>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")
</code></pre>

CC BY-SA 4.0
2020-01-28T08:13:29.133Z
You can start with `prop.table()` to get the percentages.
haci
CC BY-SA 4.0
2020-01-28T23:55:36.143Z
@haci So I would do `prop.table(all@meta.data)`
mmpp
CC BY-SA 4.0
2020-01-30T09:11:46.620Z
`prop.table()` requires a table as input so you can try something like `prop.table(table(object@meta.data$column1, object@meta.data$column2)`.
haci
<r><ggplot2>
Clustered column chart for single cell data
# Clustered column chart for single cell data

(Asked by: username_0 on 2020-01-27)

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

**Tags:** r, ggplot2
> **Comments:**
> **username_1**: You can start with `prop.table()` to get the percentages.  
> **username_0**: @haci So I would do `prop.table(all@meta.data)`  
> **username_1**: `prop.table()` requires a table as input so you can try something like `prop.table(table(object@meta.data$column1, object@meta.data$column2)`.  

---

## 2 answers

### Answer by username_1 on 2020-01-30. Score: 1 | Accepted answer

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()`.


---

### Answer by username_2 on 2020-02-03. Score: 0

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


---

