楼主: casey_c
3872 8

[程序分享] ggplot2 一页多图 [推广有奖]

  • 0关注
  • 10粉丝

博士生

92%

还不是VIP/贵宾

-

威望
0
论坛币
96 个
通用积分
2.0091
学术水平
2 点
热心指数
15 点
信用等级
2 点
经验
11502 点
帖子
278
精华
0
在线时间
94 小时
注册时间
2016-11-22
最后登录
2022-5-2

相似文件 换一批

+2 论坛币
k人 参与回答

经管之家送您一份

应届毕业生专属福利!

求职就业群
赵安豆老师微信:zhaoandou666

经管之家联合CDA

送您一个全额奖学金名额~ !

感谢您参与论坛问题回答

经管之家送您两个论坛币!

+2 论坛币

通过构建multiplot函数,能够很容易地做到一页多图,该函数的具体定义附在末尾,如果它并不能完全满足你的需求,可以复制它并在它的基础上进行修改。

首先,构建一系列图像,但不直接去渲染它们,图像的具体细节并不重要,我们只需要将这些图像对象全部存储为变量。

  1. library(ggplot2)
  2.    
  3.     # 下面的例子用到了ggplot2包中自带的示例数据集ChickWeight
  4.     # 首先创建图像,第一幅图像——折线图
  5.     p1 <- ggplot(ChickWeight, aes(x=Time, y=weight, colour=Diet, group=Chick)) +
  6.         geom_line() +
  7.         ggtitle("Growth curve for individual chicks")
  8.    
  9.     # 第二幅图像——密度分布图
  10.     p2 <- ggplot(ChickWeight, aes(x=Time, y=weight, colour=Diet)) +
  11.         geom_point(alpha=.3) +
  12.         geom_smooth(alpha=.2, size=1) +
  13.         ggtitle("Fitted growth curve per diet")
  14.    
  15.     # 第三幅图像——带拟合线的散点图
  16.     p3 <- ggplot(subset(ChickWeight, Time==21), aes(x=weight, colour=Diet)) +
  17.         geom_density() +
  18.         ggtitle("Final weight, by diet")
  19.    
  20.     # 第四幅图像——分面直方图
  21.     p4 <- ggplot(subset(ChickWeight, Time==21), aes(x=weight, fill=Diet)) +
  22.         geom_histogram(colour="black", binwidth=50) +
  23.         facet_grid(Diet ~ .) +
  24.         ggtitle("Final weight, by diet") +
  25.         theme(legend.position="none")        # 无图例(在这幅图中,图例显得太冗余了)
复制代码

接下来,我们可以用multiplot函数对创建的图像进行渲染,将它们展示为两行。

  1. multiplot(p1, p2, p3, p4, cols=2)
  2.     #> 载入需要grid包
  3.     #> geom_smooth: method="auto" and size of largest group is <1000, so using loess. Use 'method = x' to change the smoothing method.
复制代码

1.png

下面是multiplot函数的具体定义,你可以把任意数量的图像名作为其参数,或者构建一个图像列表作为函数中的plotlist。

  1. # Multiple plot function
  2.     #
  3.     # ggplot objects can be passed in ..., or to plotlist (as a list of ggplot objects)
  4.     # - cols:   Number of columns in layout
  5.     # - layout: A matrix specifying the layout. If present, 'cols' is ignored.
  6.     #
  7.     # If the layout is something like matrix(c(1,2,3,3), nrow=2, byrow=TRUE),
  8.     # then plot 1 will go in the upper left, 2 will go in the upper right, and
  9.     # 3 will go all the way across the bottom.
  10.     #
  11.     multiplot <- function(..., plotlist=NULL, file, cols=1, layout=NULL) {
  12.       library(grid)
  13.    
  14.       # Make a list from the ... arguments and plotlist
  15.       plots <- c(list(...), plotlist)
  16.    
  17.       numPlots = length(plots)
  18.    
  19.       # If layout is NULL, then use 'cols' to determine layout
  20.       if (is.null(layout)) {
  21.         # Make the panel
  22.         # ncol: Number of columns of plots
  23.         # nrow: Number of rows needed, calculated from # of cols
  24.         layout <- matrix(seq(1, cols * ceiling(numPlots/cols)),
  25.                         ncol = cols, nrow = ceiling(numPlots/cols))
  26.       }
  27.    
  28.      if (numPlots==1) {
  29.         print(plots[[1]])
  30.    
  31.       } else {
  32.         # Set up the page
  33.         grid.newpage()
  34.         pushViewport(viewport(layout = grid.layout(nrow(layout), ncol(layout))))
  35.    
  36.         # Make each plot, in the correct location
  37.         for (i in 1:numPlots) {
  38.           # Get the i,j matrix positions of the regions that contain this subplot
  39.           matchidx <- as.data.frame(which(layout == i, arr.ind = TRUE))
  40.    
  41.           print(plots[[i]], vp = viewport(layout.pos.row = matchidx$row,
  42.                                           layout.pos.col = matchidx$col))
  43.         }
  44.       }
  45.     }
复制代码

以上内容转自 数析学院 ,还有更多免费的资源,感兴趣的同学可以直接访问查看


二维码

扫码加我 拉你入群

请注明:姓名-公司-职位

以便审核进群资格,未注明则拒绝

关键词:ggplot2 gplot plot GPL Library library

沙发
cheetahfly 在职认证  发表于 2017-1-10 16:22:07 |只看作者 |坛友微信交流群

gridExtra package 也可以看看

使用道具

藤椅
casey_c 发表于 2017-1-10 16:32:09 |只看作者 |坛友微信交流群

使用道具

板凳
suzhzh 发表于 2017-1-11 11:52:34 |只看作者 |坛友微信交流群
Thanks for sharing with us this invaluable resource.

使用道具

报纸
casey_c 发表于 2017-1-11 13:09:07 |只看作者 |坛友微信交流群
suzhzh 发表于 2017-1-11 11:52
Thanks for sharing with us this invaluable resource.

使用道具

地板
kabuda1 发表于 2018-2-8 17:51:40 |只看作者 |坛友微信交流群
好好好好

使用道具

7
屋檐滴语 发表于 2018-2-8 23:45:26 |只看作者 |坛友微信交流群
r-bloggers上看到过,多谢楼主分享。

使用道具

8
123juan 发表于 2018-9-28 10:02:19 |只看作者 |坛友微信交流群
谢谢分享~~

使用道具

9
tianwk 发表于 2019-7-26 00:04:06 |只看作者 |坛友微信交流群
thanks for sharing

使用道具

您需要登录后才可以回帖 登录 | 我要注册

本版微信群
加好友,备注cda
拉您进交流群

京ICP备16021002-2号 京B2-20170662号 京公网安备 11010802022788号 论坛法律顾问:王进律师 知识产权保护声明   免责及隐私声明

GMT+8, 2024-4-19 22:02