Produce single plot and conditioning plot in trellis graphics

时光怂恿深爱的人放手 提交于 2019-12-25 00:27:58

问题


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

回答1:


You can use the split argument to 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)

see ?print.trellis

Update: to adjust size

That also is in ?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)

Tweak the position if you like.

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


来源:https://stackoverflow.com/questions/29261189/produce-single-plot-and-conditioning-plot-in-trellis-graphics

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!