I have tried to draw the error bars on my stacked bars.
I have two data matrix, one is the mean the and the other one is the standard dev.
> m_mean
TP' TP* FP
high / 60 5.445 6.162 1.673
high / 120 5.436 5.098 0.942
high / 180 5.060 3.576 0.242
high / 210 4.705 2.709 0.105
medium high / 60 4.915 6.462 2.086
medium high / 120 4.605 5.652 1.178
medium high / 180 3.940 4.219 0.479
medium high / 210 3.440 3.412 0.208
medium / 60 3.690 7.330 2.290
medium / 120 3.021 6.375 1.611
medium / 180 2.634 4.712 0.659
medium / 210 2.134 3.800 0.482
low / 60 1.626 7.060 3.652
low / 120 1.268 5.626 2.464
low / 180 0.960 3.946 1.379
low / 210 0.703 2.942 1.084
> m_devs
TP' TP* FP
high / 60 1.7291515 1.738614 1.5577219
high / 120 1.6925628 1.718531 1.0429399
high / 180 1.6664865 1.610844 0.4976783
high / 210 1.5704908 1.434705 0.3347941
medium high / 60 1.7125150 1.785986 1.7854388
medium high / 120 1.6774352 1.762953 1.1303069
medium high / 180 1.5921479 1.666078 0.7484111
medium high / 210 1.5845854 1.490798 0.4847380
medium / 60 1.6830724 1.751619 1.7569824
medium / 120 1.4651641 1.806000 1.2865980
medium / 180 1.4373970 1.672679 0.8375085
medium / 210 1.2765867 1.527198 0.7498255
low / 60 1.2568626 1.793772 2.0845242
low / 120 1.0475081 1.878204 1.5630570
low / 180 0.9794689 1.666692 1.1818442
low / 210 0.7882972 1.518862 1.0059602
I changed for both matrix the format from WIDE
to LONG
library(reshape2)
table2 <- melt(m_mean)
table3 <- melt(m_devs)
colnames(table2) <- c("Combination", "Legend", "Mean")
colnames(table3) <- c("Combination", "Legend", "SD")
attach(table2)
attach(table3)
I have drawn the graph using ggplot2
library(ggplot2)
gg<-ggplot(table2, aes(x = Combination, y = Mean, fill = Legend)) +
geom_bar(stat = "identity")+ scale_fill_grey()+
theme_light()+
labs(title = "MAIN TITLE")+
theme(axis.text.y = element_text(face="bold", vjust=0.5, size=10),
axis.text.x = element_text(face="bold", vjust=1, size=10,angle = 30, hjust = 1))+
scale_y_continuous(limits=c(0, 14),breaks = seq(0,14,2))
I have tried to draw the error bars following some useful answers for example: https://stackoverflow.com/search?q=stacked+bars+errors+ggplot but I do not understand why I do not draw the error bars with the correct value and the correct position
library(ggplot2)
gg<-ggplot(table2, aes(x = Combination, y = Mean, fill = Legend)) +
geom_bar(stat = "identity")+ scale_fill_grey()+
theme_light()+
toString(lunghezza),sep=""))+
theme(axis.text.y = element_text(face="bold", vjust=0.5, size=10),
axis.text.x = element_text(face="bold", vjust=1, size=10,angle = 30, hjust = 1))+
scale_y_continuous(limits=c(0, 14),breaks = seq(0,14,2))
# Define the top and bottom of the errorbars
gg <- gg + geom_errorbar(aes(ymax = Mean + SD, ymin=Mean - SD),
position = "identity", width = 1)
As I commented, you can directly compute new y_position for each errorbar :
library(plyr)
set.seed(1)
df = data.frame(group = sample(LETTERS[1:3], 100,replace = T),
var1 = rnorm(100,10,3),
var2 = rnorm(100,20,5),
var3 = rnorm(100,50,10))
m_mean = ddply(df, "group", summarize, var1 = mean(var1), var2 = mean(var2), var3 = mean(var3))
m_devs = ddply(df, "group", summarize, var1 = sd(var1), var2 = sd(var2), var3 = sd(var3))
table2 = melt(m_mean)
table3 = melt(m_devs)
colnames(table2)[3] = "Mean"
colnames(table3)[3] = "SD"
table4 = merge(table2, table3)
ggplot(table4, aes(group, Mean, fill = variable)) +
geom_bar(stat = "identity") +
geom_errorbar(aes(ymax = Mean + SD, ymin=Mean - SD), position = "identity", width = 1)
Here is a similar plot with random data:
And here is a solution, computing y position before plotting:
# solution
table4$y_pos = NA
table4$y_pos[table4$variable == "var1"] = table4$Mean[table4$variable == "var1"]
table4$y_pos[table4$variable == "var2"] = table4$Mean[table4$variable == "var1"] +
table4$Mean[table4$variable == "var2"]
table4$y_pos[table4$variable == "var3"] = table4$y_pos[table4$variable == "var2"] +
table4$Mean[table4$variable == "var3"]
ggplot(table4, aes(group, Mean, fill = variable)) +
geom_bar(stat = "identity") +
geom_errorbar(aes(ymax = y_pos + SD, ymin = y_pos - SD), position = "identity", width = 1)