Reputation: 29
I have found a lot of questions that deal with repositioning error bars in ggplot2
, but none that have my particular problem. I hope this isn't a duplicate!
I have a faceted barplot, where I am trying to add in error bars for confidence intervals that have already been calculated. The arguments stat
and position
don't seem to be having any effect, whether they are in the aes()
argument or just with geom_errorbar()
. Here is what I am working with:
> dput(anthro.bp)
structure(list(Source = structure(c(1L, 1L, 1L, 1L, 2L, 2L, 2L,
2L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L), .Label = c("PED", "RES"), class = "factor"),
Response = structure(c(1L, 2L, 3L, 4L, 1L, 2L, 3L, 4L, 1L,
2L, 3L, 4L, 1L, 2L, 3L, 4L), .Label = c("A", "B", "C", "D"
), class = "factor"), Value = c(0.5043315, 0.03813694, 0.20757498,
0.249956615, 0.9232598, 0.0142572, 0.0537258, 0.008757155,
0.897265, 0.03153401, 0.06610772, 0.005093254, 0.8360081,
0.03893782, 0.0370325, 0.088021559), Distance = structure(c(1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L
), .Label = c("Near", "Far"), class = "factor"), UCI = c(0.5853133,
0.07247573, 0.27357566, 0.32335335, 0.9744858, 0.03844421,
0.08841988, 0.04262752, 0.9422062, 0.0540748, 0.09600908,
0.03348959, 1.2445932, 0.11196198, 0.10133358, 0.52272511
), LCI = c(0.4233497, 0.003798153, 0.1415743, 0.17655988,
0.8720338, -0.009929805, 0.01903172, -0.02511321, 0.8523238,
0.008993231, 0.03620636, -0.02330308, 0.427423, -0.034086335,
-0.02726858, -0.34668199)), .Names = c("Source", "Response",
"Value", "Distance", "UCI", "LCI"), row.names = c(NA, -16L), class = "data.frame")
anthro.bp[,4]<-factor(anthro.bp[,4], levels=c("Near","Far"))
bp <- ggplot(anthro.bp, aes(Value, fill=Response))
bp + geom_bar(aes(x=Source,y=Value), stat="identity", position="dodge") +
geom_errorbar(aes(ymin=LCI,ymax=UCI), stat="identity", position="dodge",width=0.25) +
facet_wrap(~Distance) +
labs(x="Disturbance Source", y="Mean Probability")
I have also tried to use position=position_dodge(width=1)
, again both within the aes()
argument and outside of that in the geom_errorbar()
command. My graph is as follows in the link (I don't have a high enough reputation to embed images yet, apologies!).
I'm also getting two error messages:
Warning messages:
1: In loop_apply(n, do.ply) :
position_dodge requires non-overlapping x intervals
2: In loop_apply(n, do.ply) :
position_dodge requires non-overlapping x intervals
This is the first time that I have used ggplot2 outside of a classroom environment, so constructive criticism is highly encouraged.
Upvotes: 0
Views: 845
Reputation: 4416
For some reason which I am not clear on, ggplot2 is dodging your bars and error bars by different values. I got around this by manually specifying the dodging width. Also you've set the y and x aesthetics only geom_bar. Note where they are placed now. Lastly, stat='identity' is not needed for the geom_errorbar call.
bp <- ggplot(anthro.bp, aes(x=Source,y=Value, fill=Response))
bp + geom_bar(stat="identity", position = position_dodge(width = 0.90)) +
geom_errorbar(aes(ymin=LCI,ymax=UCI), position = position_dodge(width = 0.90),width=0.25) +
facet_wrap(~Distance) +
labs(x="Disturbance Source", y="Mean Probability")
Upvotes: 2
Reputation: 76
I believe your main problem was the undefined x value (Source) in the aes of bp.
bp <- ggplot(anthro.bp, aes(x=Source,y=Value, fill=Response))
bp + geom_bar(stat="identity", position=position_dodge()) +
geom_errorbar(aes(ymin=LCI,ymax=UCI),width=0.25, stat="identity", position=position_dodge(0.9)) +
facet_wrap(~Distance) +
labs(x="Disturbance Source", y="Mean Probability")
Upvotes: 0
Reputation: 9886
according to How to make dodge in geom_bar agree with dodge in geom_errorbar, geom_point
is it what you wanted?
anthro.bp$dmin <- anthro.bp$Value - anthro.bp$LCI
anthro.bp$dmax <- anthro.bp$UCI - anthro.bp$Value
ggplot(data=anthro.bp, aes(x=Source, ymin=Value-dmin, ymax=Value+dmax, fill=Response)) +
geom_bar(position=position_dodge(), aes(y=Value), stat="identity") +
geom_errorbar(position=position_dodge(width=0.9), colour="black") + facet_wrap(~Distance) + labs(x="Disturbance Source", y="Mean Probability")
Upvotes: 0