Question: error bar in bar plot ggplot2
0
gravatar for krushnach80
15 months ago by
krushnach80470
krushnach80470 wrote:

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 • 1.6k views
ADD COMMENTlink modified 15 months ago by Neilfws48k • written 15 months ago by krushnach80470
4

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 REPLYlink written 15 months ago by russhh4.2k

How should the error values be derived?

ADD REPLYlink written 15 months ago by Devon Ryan88k

@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 REPLYlink written 15 months ago by krushnach80470
1

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 REPLYlink written 15 months ago by Devon Ryan88k

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

ADD REPLYlink written 15 months ago by Neilfws48k

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

ADD REPLYlink written 15 months ago by dago2.5k

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 REPLYlink written 15 months ago by WouterDeCoster37k
4
gravatar for Hussain Ather
15 months ago by
Hussain Ather910
National Institutes of Health, Bethesda, MD
Hussain Ather910 wrote:

Check out geom_errorbar http://www.cookbook-r.com/Graphs/Plotting_means_and_error_bars_(ggplot2)/

ADD COMMENTlink written 15 months ago by Hussain Ather910
3
gravatar for Neilfws
15 months ago by
Neilfws48k
Sydney, Australia
Neilfws48k wrote:

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 COMMENTlink written 15 months ago by Neilfws48k
2
gravatar for cpad0112
15 months ago by
cpad011211k
India
cpad011211k wrote:

Try adding following line to ggplot code for box plots:

stat_boxplot(geom="errorbar", width=.5) +
ADD COMMENTlink modified 15 months ago • written 15 months ago by cpad011211k
Please log in to add an answer.

Help
Access

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.
Powered by Biostar version 2.3.0
Traffic: 953 users visited in the last hour