error bar in bar plot ggplot2
3
0
Entering edit mode
7.0 years ago
1769mkc ★ 1.2k

This is my code for bar plot in ggplot2 , i want to add error bar into can it be done from the function inside the ggplot2?

library(tidyr)
library(dplyr)
library(ggplot2)
dat = read.csv('barplt.csv')    
head(dat)
library(reshape2) 
melted <- melt(data = dat, id.vars = 1)
head(melted)
names(melted) <- c("gene", "variable", "value")
melted$gene <- as.factor(melted$gene) 
ggplot(melted, aes(x=gene, y=variable, fill=variable)) + 
  geom_bar(position="dodge", stat="identity") 






gene    HSC3    CMP1    Gran1
EPC1    6.434   8.15016 8.061448
TNNI3   6.113719    3.411156    1.473778
SAP30   7.351675    9.420427    4.221936
IFNA14 6.902798 5.601736    5.436589
WDR77 6.743138  8.244245    2.151724
SCML2 3.819099  6.146827    2.017991
NCOA3 1.948252  3.977396    1.473778
HNF1A 2.986199  3.880271    3.383814
SUPT7L 4.071272 7.869862    3.25319
PRR14 6.996987  8.99135 8.860416
CIT 1.612293    1.443656    1.473778

This is my sample data file any help or suggestion would be highly appreciated

R • 4.5k views
ADD COMMENT
4
Entering edit mode

If you're going to add error bars (rather than present the empirical data quantiles and outliers of a box plot), could you refrain from calling it a box plot.

ADD REPLY
0
Entering edit mode

How should the error values be derived?

ADD REPLY
0
Entering edit mode

@Devon thats my question how do i get the error bar values should i calculate separately or i ca include it from within ggplot only ?

ADD REPLY
1
Entering edit mode

Since you're using stat="identity" ggplot has no way to compute any reasonable error bars unless you want to do something weird like specify that they're some percentage of the bar height (might make sense if you have Poisson distributed values, but otherwise...).

ADD REPLY
0
Entering edit mode

I edited the title as your code specifies a bar plot, not a boxplot.

ADD REPLY
0
Entering edit mode

if you want to have a boxplot, are you aware that ggplot2 has a function called geom_boxplot?

ADD REPLY
0
Entering edit mode

Bar plots are most often not an appropriate choice for visualization. Better choices are (probably) a violin plot or box plot.

Bar bar plots: https://www.kickstarter.com/projects/1474588473/barbarplots

ADD REPLY
4
Entering edit mode
ADD COMMENT
3
Entering edit mode
7.0 years ago

Try adding following line to ggplot code for box plots:

stat_boxplot(geom="errorbar", width=.5) +
ADD COMMENT
3
Entering edit mode
7.0 years ago
Neilfws 49k

You need to define what you mean by "error bars". You also need to think about what it is that you want to visualize and the most effective way to do that.

I am assuming that for each gene, you want to calculate a summary statistic - the mean? - of the values for HSC3, CMP1 and Gran1, and that "error bar" means something like the standard error of the mean. The "bars + error" visualization is sometimes called a dynamite plot. This kind of plot is often criticized because it hides essential features of the data, such as the number of observations and their distribution.

Given that you have only 3 data points per gene, I would simply plot all of them as points and indicate the mean using a different symbol. Something like this:

library(tidyverse)

dat %>% 
  gather(variable, value, -gene) %>% 
  ggplot(aes(gene, value)) + 
    geom_point(aes(color = variable)) + 
    stat_summary(fun.y = "mean", size = 3, geom = "point", shape = 5) + 
    theme_bw()

enter image description here

ADD COMMENT

Login before adding your answer.

Traffic: 1432 users visited in the last hour
Help About
FAQ
Access RSS
API
Stats

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.

Powered by the version 2.3.6