在网格图形中生成单图和条件图

Produce single plot and conditioning plot in trellis graphics

您好:我正在研究我正在教授的课程的“提示”数据集。我想生成一个 .png 文件,该文件在绘图设备的顶行(理想情况下位于 window 的中心顶部)和底行是作为大小函数的尖端图作为大小函数的 tip 条件图由数据集中包含的 total_bill 变量重新编码的分类变量分组。我对 ggplot2 环境更加熟悉,尽管我也不太清楚如何在那里做到这一点。 谢谢!

library(reshape2)
library(grid)
library(lattice)
data(tips)
tips$bill2<-cut(tips$total_bill, breaks=3, labels=c('low', 'medium', 'high'))
#Create one plot window with this plot on the top row, ideally in the center
xyplot(tip~size, data=tips,type=c('r', 'p'))
#With this plot in the second row
xyplot(tip~size|bill2, data=tips, type=c('r', 'p'))

您可以将 split 参数用于 print

p1 <- xyplot(tip~size, data=tips,type=c('r', 'p'))
p2 <- xyplot(tip~size|bill2, data=tips, type=c('r', 'p'))

print(p1,split=c(1,1,1,2),more=TRUE)
print(p2,split=c(1,2,1,2),more=FALSE)

?print.trellis

更新:调整大小

那也在?print.trellis.

print(p1,split=c(1,1,1,2),more=TRUE,position=c(.3,0,.7,1))
print(p2,split=c(1,2,1,2),more=FALSE)

如果您愿意,可以调整 position

顺便说一下,lattice 可能不会总是将您的第二个图安排为一行 3 个面板,这取决于图形的形状 window。您可以通过

强制执行此操作
p2 <- xyplot(tip~size|bill2, data=tips, type=c('r', 'p'),layout=c(3,1))