<p>Regarding <a href="https://bioinformatics.stackexchange.com/a/19551/650">the other answer</a> with the below code:</p>
<pre class="lang-r prettyprint-override"><code>data.df &lt;- object@meta.data[, c(&quot;celltypes&quot;, &quot;condition&quot;)]
data.df[&quot;value&quot;] &lt;- object<span class="math-container">$RNA@data["geneX", ]
data.df &lt;- data.df %&gt;% mutate(group = paste(celltypes, condition, sep=":"))
data.df &lt;- data.df %&gt;% filter(value &gt; 0)
data.df.2 &lt;- data.df %&gt;% group_by(group) %&gt;% summarise(sd=sd(value),value=mean(value))
X &lt;- str_split(data.df.2$</span>'group', &quot;:&quot;, 2, simplify=TRUE)

data.df.2<span class="math-container">$'celltypes' &lt;- X[, 1]
data.df.2$</span>'condition' &lt;- X[, 2]

ggplot(data.df.2, aes(fill=condition, y=value, x=celltypes)) + 
    geom_bar(color=&quot;black&quot;, position=&quot;dodge&quot;, stat=&quot;identity&quot;) +
    geom_errorbar(
        aes(ymin=value, ymax=value+sd), width=.2,
        position=position_dodge(.9)
    )
</code></pre>
<p>I find the plot matches more the results of Seurat differential expression if you DO NOT perform the step of removing the cells with 0 counts (<code>data.df &lt;- data.df %&gt;% filter(value &gt; 0)</code>). To avoid having huge error bars you can use standard error of the mean instead of sd.</p>

CC BY-SA 4.0
2024-02-14T13:25:34.367Z
2024-02-15T15:33:02.407Z
2024-02-15T15:33:02.407Z
Ruggero Belluomo
<p>Following the answer of @<em>Ajay</em>.</p>
<p>You can add <code>+ geom_boxplot()</code> after the command to turn the violin plot to a box plot.</p>
<p>As for the barplot, you can do it with these:</p>
<pre><code># the object is your seurat object
data.df &lt;- object@meta.data[, c(&quot;celltypes&quot;, &quot;condition&quot;)]
data.df[&quot;value&quot;] &lt;- object$RNA@data[&quot;geneX&quot;, ]
data.df &lt;- data.df %&gt;% mutate(group = paste(celltypes, condition, sep=&quot;:&quot;))

# remove cells that do not express the gene
data.df &lt;- data.df %&gt;% filter(value &gt; 0)

data.df.2 &lt;- data.df %&gt;% group_by(group) %&gt;% summarise(sd=sd(value),value=mean(value))
X &lt;- str_split(data.df.2<span class="math-container">$'group', ":", 2, simplify=TRUE)
data.df.2$</span>'celltypes' &lt;- X[, 1]
data.df.2$'condition' &lt;- X[, 2]

ggplot(data.df.2, aes(fill=condition, y=value, x=celltypes)) + 
    geom_bar(color=&quot;black&quot;, position=&quot;dodge&quot;, stat=&quot;identity&quot;) +
    geom_errorbar(
        aes(ymin=value, ymax=value+sd), width=.2,
        position=position_dodge(.9)
    )
</code></pre>
<p>Dropout is very usual in single-cell data. If you really want to show the expression with barplot, I would suggest removing the zero values or using denoised data for better visualisation.</p>

CC BY-SA 4.0
2022-08-16T12:54:47.100Z
2022-08-16T13:43:18.550Z
2022-08-16T13:43:18.550Z
Janus
<p>In single-cell data a bargraph with error bar is not usually shown since the gene expression does not have a normal distribution due to drop-outs. A violin-plot is commonly used. In your case, first set the active.ident to cell types</p>
<pre><code>seuratObj &lt;- SetIdent(seuratObj , value = &quot;Cell.Types&quot;)
</code></pre>
<p>Then call the violin plot</p>
<pre><code>VlnPlot(seuratObj , features = &quot;geneX&quot;, split.by = &quot;condition&quot;)
</code></pre>
<p>If you still specifically need bargraph, you can get the output of the violin plot as a dataframe and then draw the barplot.</p>

CC BY-SA 4.0
2022-08-13T17:29:21.230Z
2022-08-13T17:29:21.230Z
Ajay
<p>I am looking for a way to present some single-cell RNA sequencing data on expression of a certain gene. My dataset has 2 variables, cell type and condition. I'm looking to create a grouped aligned scatter plot bar graph of the expression of the gene, where the y-axis is expression levels, while X axis is the different cell types, with each cell type having 2 bars (1 for healthy, 1 for diseased). My dataset has 3 healthy and 3 diseased samples, but all of the data is integrated into a Seurat object. To first create an aligned scatter plot bar graph, what I did was generate a DotPlot for the expression of gene X in each sample, split by cell-type. Then I realized that DotPlot() prints out a data table with values including avg.exp, pct.exp and avg.scaled.exp. I used the avg.exp metric to plot the data, but I was posting this question to see if there are other ways I can access and present expression data from a Seurat object. Is the way I've presented accurate? I've posted a sample bar graph of what I'm looking for, and the main question is how I can gather the expression levels across cell types AND diseased or healthy condition.</p>
<p>Thanks!
<a href="https://i.sstatic.net/xqVRw.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/xqVRw.png" alt="Example Bar Graph" /></a></p>

<r><scrnaseq><seurat>
Bar Graph of Expression Data from Seurat Object
# Bar Graph of Expression Data from Seurat Object

(Asked by: username_0 on 2022-08-10)

I am looking for a way to present some single-cell RNA sequencing data on expression of a certain gene. My dataset has 2 variables, cell type and condition. I'm looking to create a grouped aligned scatter plot bar graph of the expression of the gene, where the y-axis is expression levels, while X axis is the different cell types, with each cell type having 2 bars (1 for healthy, 1 for diseased). My dataset has 3 healthy and 3 diseased samples, but all of the data is integrated into a Seurat object. To first create an aligned scatter plot bar graph, what I did was generate a DotPlot for the expression of gene X in each sample, split by cell-type. Then I realized that DotPlot() prints out a data table with values including avg.exp, pct.exp and avg.scaled.exp. I used the avg.exp metric to plot the data, but I was posting this question to see if there are other ways I can access and present expression data from a Seurat object. Is the way I've presented accurate? I've posted a sample bar graph of what I'm looking for, and the main question is how I can gather the expression levels across cell types AND diseased or healthy condition.

Thanks!
[![Example Bar Graph](https://i.sstatic.net/xqVRw.png)](https://i.sstatic.net/xqVRw.png)

**Tags:** r, scrnaseq, seurat

---

## 3 answers

### Answer by username_1 on 2024-02-14. Score: 2

Regarding the other answer with the below code:
data.df <- object@meta.data[, c("celltypes", "condition")]
data.df["value"] <- object$RNA@data["geneX", ]
data.df <- data.df %>% mutate(group = paste(celltypes, condition, sep=":"))
data.df <- data.df %>% filter(value > 0)
data.df.2 <- data.df %>% group_by(group) %>% summarise(sd=sd(value),value=mean(value))
X <- str_split(data.df.2$'group', ":", 2, simplify=TRUE)

data.df.2$'celltypes' <- X[, 1]
data.df.2$'condition' <- X[, 2]

ggplot(data.df.2, aes(fill=condition, y=value, x=celltypes)) + 
    geom_bar(color="black", position="dodge", stat="identity") +
    geom_errorbar(
        aes(ymin=value, ymax=value+sd), width=.2,
        position=position_dodge(.9)
    )

I find the plot matches more the results of Seurat differential expression if you DO NOT perform the step of removing the cells with 0 counts (data.df <- data.df %>% filter(value > 0)). To avoid having huge error bars you can use standard error of the mean instead of sd.


---

### Answer by username_2 on 2022-08-16. Score: 1

Following the answer of @Ajay.
You can add + geom_boxplot() after the command to turn the violin plot to a box plot.
As for the barplot, you can do it with these:
# the object is your seurat object
data.df <- object@meta.data[, c("celltypes", "condition")]
data.df["value"] <- object$RNA@data["geneX", ]
data.df <- data.df %>% mutate(group = paste(celltypes, condition, sep=":"))

# remove cells that do not express the gene
data.df <- data.df %>% filter(value > 0)

data.df.2 <- data.df %>% group_by(group) %>% summarise(sd=sd(value),value=mean(value))
X <- str_split(data.df.2$'group', ":", 2, simplify=TRUE)
data.df.2$'celltypes' <- X[, 1]
data.df.2$'condition' <- X[, 2]

ggplot(data.df.2, aes(fill=condition, y=value, x=celltypes)) + 
    geom_bar(color="black", position="dodge", stat="identity") +
    geom_errorbar(
        aes(ymin=value, ymax=value+sd), width=.2,
        position=position_dodge(.9)
    )

Dropout is very usual in single-cell data. If you really want to show the expression with barplot, I would suggest removing the zero values or using denoised data for better visualisation.


---

### Answer by username_3 on 2022-08-13. Score: 1

In single-cell data a bargraph with error bar is not usually shown since the gene expression does not have a normal distribution due to drop-outs. A violin-plot is commonly used. In your case, first set the active.ident to cell types
seuratObj <- SetIdent(seuratObj , value = "Cell.Types")

Then call the violin plot
VlnPlot(seuratObj , features = "geneX", split.by = "condition")

If you still specifically need bargraph, you can get the output of the violin plot as a dataframe and then draw the barplot.


---

