<p>Here is a solution using <code>dplyr</code> and <code>ggplot2</code>:</p>
<pre><code>library(Seurat) 
library(dplyr)
library(ggplot2)

meta.data &lt;- pbmc_small[[]]

# create random classifications for the sake of this example
meta.data$condition &lt;- sample(c('A', 'B', 'C'), nrow(meta.data), replace = TRUE)

counts &lt;- group_by(meta.data, condition, res.1) %&gt;% summarise(count = n())

ggplot(counts, aes(res.1, count, fill = condition)) +
  geom_bar(stat = 'identity')
</code></pre>
<p><a href="https://i.sstatic.net/LL3B5.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/LL3B5.png" alt="enter image description here" /></a></p>

CC BY-SA 4.0
2019-02-06T14:35:44.000Z
2021-02-01T17:27:33.083Z
2021-02-01T17:27:33.083Z
TimStuart
<p>I have 2151 cells, I clustered them by Seurat to 5 clusters. With the code below, I am able to have the number of cells per cluster and per condition:</p>

<pre><code>number_perCluster&lt;- table(object@meta.data<span class="math-container">$conditions, 
                          object@meta.data$</span>clusterID)
</code></pre>

<p>Could I please get a hint on how I could proceed to produce a figure where I say: X% of cells treated with condition "Y" are located in cluster "C" and so forth?</p>

<p>Thank you in advance.</p>

CC BY-SA 4.0
2019-02-06T13:39:36.430Z
Have a look at `prop.table`, also if you want a figure, please describe it a bit more. This is usually better displayed as a table or heatmap, but perhaps you want something else...
llrs
CC BY-SA 4.0
2019-02-06T14:40:29.027Z
Please don't also create a github issue on the Seurat page each time you ask a question here, there is no need
TimStuart
<r><scrnaseq><seurat><ggplot2>
Percentage distribution of cells in all clusters based on their treatment condition?
# Percentage distribution of cells in all clusters based on their treatment condition?

(Asked by: username_0 on 2019-02-06)

I have 2151 cells, I clustered them by Seurat to 5 clusters. With the code below, I am able to have the number of cells per cluster and per condition:
number_perCluster<- table(object@meta.data$conditions, 
                          object@meta.data$clusterID)

Could I please get a hint on how I could proceed to produce a figure where I say: X% of cells treated with condition "Y" are located in cluster "C" and so forth?
Thank you in advance.

**Tags:** r, scrnaseq, seurat, ggplot2
> **Comments:**
> **username_1**: Have a look at `prop.table`, also if you want a figure, please describe it a bit more. This is usually better displayed as a table or heatmap, but perhaps you want something else...  
> **username_2**: Please don't also create a github issue on the Seurat page each time you ask a question here, there is no need  

---

## 1 answers

### Answer by username_2 on 2019-02-06. Score: 4 | Accepted answer

Here is a solution using dplyr and ggplot2:
library(Seurat) 
library(dplyr)
library(ggplot2)

meta.data <- pbmc_small[[]]

# create random classifications for the sake of this example
meta.data$condition <- sample(c('A', 'B', 'C'), nrow(meta.data), replace = TRUE)

counts <- group_by(meta.data, condition, res.1) %>% summarise(count = n())

ggplot(counts, aes(res.1, count, fill = condition)) +
  geom_bar(stat = 'identity')


---

