在网格图形中生成单个图和条件图 [英] Produce single plot and conditioning plot in trellis graphics
问题描述
你好:我正在为我正在教的课程使用``提示''数据集.我想生成一个.png文件,该文件在绘图设备的顶行(最好在窗口的中心顶部)中具有作为大小函数的tip的图,而底行是的条件图提示是大小的函数,该大小由从数据集中包含的total_bill变量重新编码的分类变量分组.我对ggplot2环境更加熟悉,尽管我也不太清楚如何在其中执行此操作. 谢谢!
Hi there: I'm playing around with the ``tips'' data set for a course that I'm teaching. I'd like to produce one .png file that has the plot of tip as a function of size on the top row of the plotting device (ideally in the center-top, of the window) and the bottom row being the conditioning plot of tip as a function of size grouped by a categorical variable recoded from the total_bill variable contained in the data set. I'm much more familiar with the ggplot2 environment, although I can't quite figure out how to do this there, either. Thanks!
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个面板排成一行.您可以通过
By the way, lattice
might not always arrange your second plot as 3 panels in one row, depending on the shape of your graphics window. You can force this by
p2 <- xyplot(tip~size|bill2, data=tips, type=c('r', 'p'),layout=c(3,1))
这篇关于在网格图形中生成单个图和条件图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!