I have two groups in my data, one of which is a fixed quantity and has no error. I have put the standard deviation for this group to be 0. The other group is obtained through experiment and has error. I am trying to get bars for the two groups side by side but only want to show error bars on one group.
Here is a subset of my data
> dput(miniDF)
structure(list(meanVal = c(0.141142140910031, 0.123590465669207,
0.107997372347094, 0.0942024597244465, 0.0820416407495106, 0.071353688041151,
0.0619844689599558, 0.0537895165007547, 0.0466354169758515, 0.0404003706604038,
0.158461397159066, 0.153053253758612, 0.138918096515527, 0.125843921455302,
0.110345396211753, 0.0787109795360732, 0.0618074624835807, 0.0449341121897361,
0.0363718842110094, 0.0291184151867377), sdVal = c(0, 2.86100521766632e-09,
4.69695882560463e-09, 5.79859596041863e-09, 6.38047156294103e-09,
6.60009718207142e-09, 6.57241274422449e-09, 6.3807059656363e-09,
6.08482995661066e-09, 5.72736892254484e-09, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0), week = structure(c(1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L,
9L, 10L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L), .Label = c("Week 1",
"Week 2", "Week 3", "Week 4", "Week 5", "Week 6", "Week 7", "Week 8",
"Week 9", "Week 10", "Week 11", "Week 12", "Week 13", "Week 14",
"Week 15", "Week 16", "Week 17", "Week 18", "Week 19", "Week 20",
"Week 21"), class = "factor"), group = structure(c(2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L), .Label = c("q", "qOpt"), class = "factor"), cluster = structure(c(1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L), .Label = c("1", "2", "3", "4", "5", "6", "7", "8",
"9", "10"), class = "factor")), .Names = c("meanVal", "sdVal",
"week", "group", "cluster"), row.names = c(1L, 2L, 3L, 4L, 5L,
6L, 7L, 8L, 9L, 10L, 211L, 212L, 213L, 214L, 215L, 216L, 217L,
218L, 219L, 220L), class = "data.frame")
The code for the plot is,
limits <- aes(ymax = meanVal + sdVal, ymin = meanVal - sdVal)
dodge <- position_dodge(width=0.9)
p <- ggplot(miniDF, aes(fill=group, y=meanVal, x=cluster)) +
geom_bar(position=dodge, stat="identity") +
geom_errorbar(position=dodge,limits, width=0.25) +
facet_wrap(~week,ncol=5)
What I want is to get the error bar only on the group qOpt (blue color) and NOT the group q. I tried using only the subset with group qOpt in the geom_errorbar function but it messes up the position of the errorbar and I am not able to center it on the blue bars. I appreciate any suggestions. Thanks.
The simplest solution I could find is just
miniDF$sdVal[miniDF$group == 'q'] <- NA
p
Workaround is to use subset of data inside the geom_errorbar()
and then set x value to 1.225 (it calculated as 1 + 0.9/4 (width of dodging)).
dodge <- position_dodge(width=0.9)
ggplot(miniDF, aes(fill=group, y=meanVal, x=cluster)) +
geom_bar(stat="identity",position=dodge) +
geom_errorbar(data=subset(miniDF, group=="qOpt"),
aes(x=1.225,ymax = meanVal + sdVal, ymin = meanVal - sdVal),width=0.25) +
facet_wrap(~week,ncol=5)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.