【发布时间】:2020-04-06 10:47:57
【问题描述】:
我在弄清楚如何遍历数据框中的变量并使用 ggplot 绘制它们时遇到问题。 我的数据示例如下:
head(myData,2)
x1 x2 yhat x11 x3 yhat1 x12
1 -0.8523122 -2.737223 -6.562228 -0.8523122 -1.450288 0.464739 -0.8523122
2 -0.5649950 -2.737223 -6.562228 -0.5649950 -1.450288 0.464739 -0.5649950
x4 yhat2 x21 x31 yhat3
1 -1.267759 -4.624147 -2.737223 -1.450288 -0.6858007
2 -1.267759 -4.624147 -2.267001 -1.450288 -0.6858007
我要做的是使用geom_raster 绘制每对变量(即[x1,x2]、[x11,x3] 等)并使用相应的yhat 作为fill 值.
例如,如果我要手动绘制所有内容,我会执行以下操作:
p<-ggplot(myData, aes(x = x1, y = x2)) + geom_raster(aes(fill = yhat))
pp<-ggplot(myData, aes(x = x11, y = x3)) + geom_raster(aes(fill = yhat1))
ppp<-ggplot(myData, aes(x = x12, y = x4)) + geom_raster(aes(fill = yhat2))
pppp<-ggplot(myData, aes(x = x21, y = x31)) + geom_raster(aes(fill = yhat3))
grid.arrange(p, pp, ppp, pppp, ncol = 2)
但我正在尝试编写一个循环遍历数据框并绘制图表的函数。我试图从另一个问题 here 调整代码,但我无法让它为我工作。
关于如何为我的数据实现这一目标的任何建议?
【问题讨论】: