ggplot2 一页多图

来源:互联网 发布:python中 变量类型 编辑:程序博客网 时间:2024/05/07 01:48

本案例文本来自于公众号:数据小魔方

载入包

library(ggplot2)
library(grid)

定义图表

chart1 <- ggplot(mtcars,aes(mpg,wt,colour=factor(cyl)))+geom_point()
chart2 <- ggplot(diamonds, aes(carat,depth,colour=color))+geom_point()
chart3 <- ggplot(diamonds,aes(carat,depth,colour=color))+geom_point()+
facet_grid(.~color)

  • 图三表示以color进行分面

新建图片版面

grid.newpage()

版面分成2*2矩阵

pushViewport(viewport(layout=grid.layout(2,2)))

定义图表的布局规则

vplayout <- function(x,y){viewport(layout.pos.row = x,layout.pos.col = y)}

  • 制定画布的行显示图表的x轴,列显示图表的y轴

画图

print(chart3,vp=vplayout(1,1:2))
print(chart2,vp=vplayout(2,1))
print(chart1,vp=vplayout(2,2))

  • 将(1,1) (1,2)的位置画上图chart3
  • 将(2,1)的位置画上图chart2
  • 将(2,2)的位置画上图chart1
原创粉丝点击