R Resources‎ > ‎

### Interaction Plots

 Here is a set of codes to create an interaction plot with standard error bars:`#set up example dataset``response=c(rnorm(10,mean=10,sd=3),rnorm(10,mean=4,sd=2),rnorm(10,mean=6,sd=2), rnorm(10,mean=12,sd=3)) #create response variables``type=c(rep("a",20),rep("b",20)) #create "subject type" ``treatment=c(rep("X",10),rep("Y",10),rep("X",10),rep("Y",10)) #create "treatment" ``dat=data.frame(response,type,treatment) #this is now the example dataset ``#calculate means for each type x treatment means=by(dat\$response,list(dat\$type,dat\$treatment),mean) ``# a function for calculating standard error ``se=function(x) sqrt(var(x)/length(x)) ``#now calculate standard error for each type x treatment ``ses=by(dat\$response,list(dat\$type,dat\$treatment),se) ``#plot means as interaction plot; type="b" means plot both symbols and lines; pch=c(21,19) are the two symbol types; ylim is the y axis minimum & maximum values; las=1 makes the y-axis numbers horizontal ``interaction.plot(dat\$treatment,dat\$type,dat\$response,type="b",pch=c(21,19), ylim=c(0,15),las=1,ylab="Response",xlab="Treatment") ``#now add the standard error lines (means plus/minus standard error) ``lines(c(1,1),c(means-ses,means+ses)) ``lines(c(1,1),c(means-ses,means+ses)) ``lines(c(2,2),c(means-ses,means+ses)) ``lines(c(2,2),c(means-ses,means+ses))`Here is the resulting plot:
Comments