【问题标题】:How to convert a regression interaction plot to ggplot?如何将回归交互图转换为 ggplot?
【发布时间】:2013-08-23 01:43:21
【问题描述】:

谁能告诉我如何用ggplot绘制下面的交互效果(使用this工具生成的代码):

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 是主持人的点/条件斜率值。

【问题讨论】:

  • 欢迎使用交叉验证!但这个问题是关于编程的,在 Stack Overflow 上会更好。
  • 我希望 thisthis 的例子能让你开始。
  • 请检查您的语法。 leg[1]leg[2] 是什么?

标签: r ggplot2 regression interaction


【解决方案1】:

你需要一个长格式的数据框:

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))

(图例会自动出现。)如果您想强制执行在基本图形模板中应用于空白绘图框架的“xx”和 yy 限制,还有 xlimylim 函数。

【讨论】:

  • 感谢@DWin。如果其他人像我一样是 R 新手并试图复制:组是主持人“w”变量,上面的 y1,y2,y3 是 w(values) 处的条件值。数据框 AND ggplot 的代码是:'group= c("w(1)", "w(1)", "w(2)", "w(2)", "w(3)", "w(3)") x= c(40, 65, 40, 65, 40, 65) y= c(28.5, 37.15, 30.345, 35.895, 32.19, 34.64) mydata
猜你喜欢
  • 2021-05-07
  • 1970-01-01
  • 2021-02-04
  • 1970-01-01
  • 2016-04-06
  • 2021-01-08
  • 2015-10-23
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多