################ #Interaction plot for 2x2 designs ################ #read in the data d=read.csv("results.no.outliers.csv") #load some packages library(ggplot2) library(dplyr) #I like to define my own colors #These are from colorbrewer.org #NOTE: these same colorbrewer colors can be easily called from ggplot using the command scale_color_brewer(type="qual", palette=6) #But I also want to show you how to define them manually red="#e41a1c" blue="#377EB8" purple="#984ea3" green ="#4daf4a" orange ="#ff7f00" #specify the order of the levels of the factors we will be working with d$embeddedStructure=factor(d$embeddedStructure, levels=c("non","isl")) d$dependencyLength=factor(d$dependencyLength, levels=c("sh","lg")) #We can also change the names of levels if we want. This matters for plotting more than anything else, because R will use the level names in the plot. #To use the levels() function this way, you have to rename all of the levels in the factor. You can't rename just one. You have to use a different syntax to do that. levels(d$embeddedStructure) = c("non-island", "island") levels(d$dependencyLength) = c("short", "long") #The first thing we need to do is remove the fillers, because we aren't interested in them anymore #We could do this with a subset() function, but instead we will use the filter() function from dplyr, which makes things even easier d2 = filter(d, island =="wh") #The next thing we need to do is calculate the mean, standard deviation, and estimated standard error for each condition #Now we can use the summarize() function from dplyr to do the work. #Notice that we can calculate mutliple summaries in one summarize() function; and notice that those summaries can build on each other. We first calculate condition.stdev, then we use that to calculate condition.se plottable = d2 %>% group_by(condition, island, embeddedStructure, dependencyLength) %>% summarize(condition.mean = mean(zscores, na.rm=TRUE), condition.stdev = sd(zscores, na.rm=TRUE), condition.se = sd(zscores, na.rm=TRUE)/sqrt(n())) #Now we create an interaction plot, and use color to distinguish the structure factor p = ggplot(plottable, aes(x=dependencyLength, y=condition.mean, group = embeddedStructure, color=embeddedStructure)) + geom_line()+ geom_point()+ geom_errorbar(aes(ymin=condition.mean-condition.se, ymax=condition.mean+condition.se), width=.3)+ ylab("mean z-score judgment")+ xlab("dependency length")+ theme(axis.title.x = element_text(vjust=-0.5)) + theme(axis.title.y = element_text(vjust=.2))+ scale_color_manual(name="embedded structure", values=c(blue, red))+ scale_y_continuous(limits=c(-1.5,1.5)) + theme(legend.position="bottom") #We can also keep it black and white, and use linetype to signal different levels of the structure factor p.bw = ggplot(plottable, aes(x=dependencyLength, y=condition.mean, group = embeddedStructure, linetype=embeddedStructure)) + geom_line()+ geom_point()+ geom_errorbar(aes(ymin=condition.mean-condition.se, ymax=condition.mean+condition.se), width=.3)+ ylab("mean z-score judgment")+ xlab("dependency length")+ theme(axis.title.x = element_text(vjust=-0.5)) + theme(axis.title.y = element_text(vjust=.2))+ scale_y_continuous(limits=c(-1.5,1.5)) + theme(legend.position="bottom") #If you want to specify the size of the plot (e.g. for publication) you do that by specifying the size of the window that you are going to open. On a mac, this is done witht he quartz() function. On windows, it is the windows() function. #Pro tip: Plots look best if they are in a golden ratio. You can roughly get this by making the window a golden ratio (e.g, 10 x 6.18); however, there is stuff on the borders of graphs, so this won't be perfect. quartz( width=10, height=6.18 ) p.bw #You can save a ggplot to pdf using ggsave(). This will dump whatever is the most recent ggplot into a pdf. Note: This does not dump the active window, it dumps the last ggplot command. If you want to save from the active window, you can use the pdf() command. ggsave("interaction.plot.pdf")