Sunday, June 1, 2014

How to create a multi-panelled line graph with error bars in ggplot2

#Below you will find some code that I cobbled together from various sources to create a multi-panelled line graph with error bars
#first I need to load the file and the ggplot2 package
#FYI I am using ggplot v 0.8.9 on R 2.13.1 (I know, I should upgrade!)
#if you look at the file 
# you will see the mean (and sd), and median scores on a standardized student survey for each of the last 3 years of my BI393 Biostatistics class (useful data for preparing a tenure application file). As the QUESTION column is a bit long & unweildy, I will force it to break every 70th character (helps with the formatting of the strip text in gglplot)
BI393$groupwrap = unlist(lapply(strwrap(BI393$QUESTION, width=70, simplify=FALSE), paste, collapse="\n"))
#now, to business: I want to plot the mean scores for each question by year
printing<- ggplot(BI393,aes(x=as.factor(YEAR),y=MEAN,colour=QUESTION))
#and add on some error bars
#connect the points with colour-coded lines
+geom_line(aes(group = QUESTION))
#and make the points clear to see
+geom_point(shape=21, fill="white")
#adjust the range of the y-axis, and customize the x-label
+ylim(1,7.5)+xlab("Academic Year")
#change the background colours
#wrap the scores by the questions
+facet_wrap(~ groupwrap,ncol=3)
#and get rid of a pesky legend (as the question is in the strip text)
#make an informative y-axis label
+ylab(expression(paste("BI393 Scale Grade (Mean" %+-% "1SD)")))
#and now print it off...
ggsave(printing, file="396.png", width=10, height=10)
Created by Pretty R at

No comments:

Post a Comment