# Error bars represent standard error of the meanggplot(Tooth, aes(x=dose, y=len, fill=supp)) +geom_bar(position=position_dodge(.9), stat="identity") +geom_errorbar(aes(ymin=len-se, ymax=len+se),
width=.2, # Width of the error barsposition=position_dodge(.9))
# Use 95% confidence intervals instead of SEMggplot(Tooth, aes(x=dose, y=len, fill=supp)) +geom_bar(position=position_dodge(), stat="identity") +geom_errorbar(aes(ymin=len-ci, ymax=len+ci),
width=.2, # Width of the error barsposition=position_dodge(.9))
ggplot(Tooth, aes(x=dose, y=len, fill=supp)) +geom_bar(position=position_dodge(), stat="identity",
colour="black", # Use black outlines,size=.3) +# Thinner linesgeom_errorbar(aes(ymin=len-se, ymax=len+se),
size=.3, # Thinner lineswidth=.2,
position=position_dodge(.9)) +xlab("Dose (mg)") +ylab("Tooth length") +scale_fill_hue(name="Supplement type", # Legend label, use darker colorsbreaks=c("OJ", "VC"),
labels=c("Orange juice", "Ascorbic acid")) +ggtitle("The Effect of Vitamin C on\nTooth Growth in Guinea Pigs") +scale_y_continuous(breaks=0:20*4) +theme_bw()
Line graphs
sd
The errorbars overlapped, so use position_dodge to move them horizontally
pd <-position_dodge(0.1) # move them .1 to the left and rightggplot(Tooth, aes(x=dose, y=len, colour=supp)) +geom_errorbar(aes(ymin=len -se, ymax=len +se), width=.1,
position=pd) +geom_point(position=pd)
ggplot(Tooth, aes(x=dose, y=len, colour=supp)) +geom_errorbar(aes(ymin=len-se, ymax=len+se,colour = supp),
width=.1, position=pd) +geom_point(position=pd, size=3, shape=21, fill="white") +# 21 is filled circlexlab("Dose (mg)") +ylab("Tooth length") +scale_colour_hue(name="Supplement type", # Legend label, use darker colorsbreaks=c("OJ", "VC"),
labels=c("Orange juice", "Ascorbic acid"),
l=40) +# Use darker colors, lightness=40ggtitle("The Effect of Vitamin C on\nTooth Growth in Guinea Pigs") +expand_limits(y=0) +# Expand y rangescale_y_continuous(breaks=0:20*4) +# Set tick every 4theme_bw() +theme(legend.justification=c(1,0),
legend.position=c(1,0)) # Position legend in bottom right
Within-subjects variables
When all variables are between-subjects, it is straightforward to plot standard error or confidence intervals.
However, when there are within-subjects variables (repeated measures), plotting the standard error or regular confidence intervals may be misleading for making inferences about differences between conditions.
# Use a consistent y range
ymax <-max(dfw_long$value)
ymin <-min(dfw_long$value)
# Plot the individualsggplot(dfw_long, aes(x=condition, y=value,
colour=factor(subject), group=factor(subject))) +geom_line() +geom_point(shape=21, fill="white") +ylim(ymin,ymax)
# Create the normed version of the data
dfwNorm.long <-normDataWithin(data=dfw_long, idvar="subject", measurevar="value")
# Plot the normed individualsggplot(dfwNorm.long, aes(x=condition, y=value_norm, colour=factor(subject), group=factor(subject))) +geom_line() +geom_point(shape=21, fill="white") +ylim(ymin,ymax)
The differences in the error bars for the regular (between-subject) method and the within-subject method are shown here. The regular error bars are in red, and the within-subject error bars are in black.
dfwc_between <-summarySE(data=dfw_long, measurevar="value", groupvars="condition", na.rm=FALSE, conf.interval=.95)
# condition N value sd se ci# 1 pretest 10 47.7 8.60 2.72 6.15# 2 posttest 10 51.4 7.25 2.29 5.19# Show the between-S CI's in red, and the within-S CI's in black
dfwc <-summarySEwithin(dfw_long,
measurevar="value", withinvars="condition",
idvar="subject",
na.rm=FALSE,
conf.interval=.95)
ggplot(dfwc_between,
aes(x=condition, y=value, group=1)) +geom_line() +geom_errorbar(width=.1,
aes(ymin=value-ci, ymax=value+ci), colour="red") +geom_errorbar(width=.1,
aes(ymin=value-ci, ymax=value+ci), data=dfwc) +geom_point(shape=21, size=3, fill="white") +ylim(ymin,ymax)
The normed means are calculated so that means of each between-subject group are the same. These values can diverge when there are between-subject variables.
dat <-read.table(header=TRUE, text='id trial gender dv A 0 male 2 A 1 male 4 B 0 male 6 B 1 male 8 C 0 female 22 C 1 female 24 D 0 female 26 D 1 female 28')
# normed and un-normed means are differentsummarySEwithin(dat, measurevar="number", withinvars="trial", betweenvars="gender",
idvar="id")
## gender trial N dv dv_norm sd se ci
## 1 female 0 2 24 14 0 0 0
## 2 female 1 2 26 16 0 0 0
## 3 male 0 2 4 14 0 0 0
## 4 male 1 2 6 16 0 0 0
# Automatically converting the following non-factors to factors: trial# gender trial N dv dv_norm sd se ci# 1 female 0 2 24 14 0 0 0# 2 female 1 2 26 16 0 0 0# 3 male 0 2 4 14 0 0 0# 4 male 1 2 6 16 0 0 0