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
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.
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...).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