2011年10月16日星期日

plots side by side - R

http://www.quantumforest.com/2011/10/setting-plots-side-by-side/

library(ggplot2)
 
# Create first plot and assign it to variable
p1 = qplot(wt, mpg, data = mtcars,
           xlab = 'Car weight', ylab = 'Mileage')
 
# Create second plot and assign it to variable
p2 = qplot(wt, mpg, color = factor(cyl), data = mtcars,
           geom = c('point', 'smooth'),
           xlab = 'Car weight', ylab = 'Mileage')
 
# Define grid layout to locate plots and print each graph
pushViewport(viewport(layout = grid.layout(1, 2)))
print(p1, vp = viewport(layout.pos.row = 1, layout.pos.col = 1))
print(p2, vp = viewport(layout.pos.row = 1, layout.pos.col = 2))

没有评论:

发表评论