Question

Can someone please show me how to plot with ggplot the interaction effect below (code generated by using this tool):

xx <- c(40,65)   
yy <- c(26.77,37.15)
x <- c(40,65)   #  <-- x-coords for lines
y1 <- c(28.5,37.15)
y2 <- c(30.345,35.895)
y3 <- c(32.19,34.64)
plot(xx,yy,type='n',font=2,font.lab=2,xlab='x1',ylab='Y',main='2-Way Interaction Plot')
lines(x,y1,lwd=3,lty=1,col=1)
lines(x,y2,lwd=3,lty=5,col=2)
lines(x,y3,lwd=3,lty=6,col=3)
points(x,y1,col=1,pch=16)
points(x,y2,col=1,pch=16)
points(x,y3,col=1,pch=16)                                                      
legend(leg[1],leg[2],legend=c('W1(1)','W1(2)','W1(3)'),lwd=c(3,3,3),lty=c(1,5,6),col=c(1,2,3))

y1, y2, y3 are the points/conditional slope values for the moderator.

Was it helpful?

Solution

You need a dataframe in long format:

dfrm <- data.frame(x=rep(x,3), y=c(y1,y2,y3), 
                   group=rep(c("y1","y2","y3") ,each=2) ) 
ggplot(data=dfrm, aes(x=x,y=y,group=group) ) +
                      geom_point()+geom_line(aes(col=group))

(The legend appears automagically.) There are also xlim and ylim functions if you wanted to enforce the 'xx' and yy limits that you applied to the blank plot framework in your base graphics template.

enter image description here

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top