Help with GGPLOT2 geom_errorbar function and confidence intervals

Dear all

I have a problem with geom_errorbar() and confidence intervals.

When I run this code with confidence intervals, the top error bar is mssing for 4,6 and 11 weeks:

plot <- ggplot(combo, aes(x=Week, y=mean, colour=group1, group=group1)) +
geom_errorbar(aes(ymin=mean-ci, ymax=mean+ci), colour="darkgrey", width=.1) +
geom_line() +
geom_point( size=2, shape=10, fill="white") + # 21 is filled circle
xlab("Week") +
ylab("Steps per day") +
scale_colour_hue(name="Group", # Legend label, use darker colors
breaks=c("intervention", "control"),
labels=c("PAM+MI Group", "PAM Group"),
l=53) + # Use darker colors, lightness=40
ggtitle("Mean daily step count throughout the 12 week intervention") +
expand_limits(y=0) + # Expand y range
scale_y_continuous(limits = c(4000, 8000)) +
theme_classic() +
theme(legend.justification=c(1,0),
legend.position=c(0.95,0.05)) + # Position legend in bottom right
scale_x_continuous(breaks = c(0,1,2,3,4,5,6,7,8,9,10,11,12))

When I run it with standard error as below, it works fine:

plot <- ggplot(combo, aes(x=Week, y=mean, colour=group1, group=group1)) +
geom_errorbar(aes(ymin=mean-se, ymax=mean+se), colour="darkgrey", width=.1) +
geom_line() +
geom_point( size=2, shape=10, fill="white") + # 21 is filled circle
xlab("Week") +
ylab("Steps per day") +
scale_colour_hue(name="Group", # Legend label, use darker colors
breaks=c("intervention", "control"),
labels=c("PAM+MI Group", "PAM Group"),
l=53) + # Use darker colors, lightness=40
ggtitle("Mean daily step count throughout the 12 week intervention") +
expand_limits(y=0) + # Expand y range
scale_y_continuous(limits = c(4000, 8000)) +
theme_classic() +
theme(legend.justification=c(1,0),
legend.position=c(0.95,0.05)) + # Position legend in bottom right
scale_x_continuous(breaks = c(0,1,2,3,4,5,6,7,8,9,10,11,12))

I cannot solve this. The data looks fine. Is it a bug in the package or?

The two variables can be seen below:

combo$ci
[1] 956.9092 1009.2986 960.5028 961.4319 1063.1450 1148.9696 1193.7623 973.8314 1005.1950 1136.2485
[11] 1085.6085 1027.7778 994.9311 1177.1768 1088.6076 1108.2578 1225.8499 1111.0246 1180.8890 914.7586
[21] 1077.0121 1139.3519 1153.6248 1095.5678 1127.8282 1136.2159

combo$se
[1] 488.2190 514.9483 490.0524 490.5265 542.4209 586.2090 609.0624 496.8527 512.8546 579.7186 553.8819
[12] 524.3764 507.6179 600.6004 555.4121 565.4377 625.4336 566.8493 602.4944 466.7136 549.4960 581.3020
[23] 588.5841 558.9631 575.4225 579.7020

Did you chop them off with scale_y_continuous(limits = c(4000, 8000))?

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

We've all done it......

2 Likes

Yes... Haha. Thank you!