问题
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