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
3 answers
Check out geom_errorbar http://www.cookbook-r.com/Graphs/Plotting_means_and_error_bars_(ggplot2)/
Try adding following line to ggplot code for box plots:
stat_boxplot(geom="errorbar", width=.5) +
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()

Log in to answer this question.
How should the error values be derived?
@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 ?
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...).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.
I edited the title as your code specifies a bar plot, not a boxplot.
if you want to have a boxplot, are you aware that ggplot2 has a function called
geom_boxplot?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