楼主: kk22boy
3071 1

[程序分享] Multiple graphs on one page (ggplot2) [推广有奖]

  • 8关注
  • 95粉丝

一叶知秋

已卖:9095份资源

学科带头人

51%

还不是VIP/贵宾

-

TA的文库  其他...

计量经济与统计

SSCI、权威 论文写作及投稿经验

威望
1
论坛币
57240 个
通用积分
130.1721
学术水平
437 点
热心指数
492 点
信用等级
393 点
经验
68023 点
帖子
1586
精华
0
在线时间
2192 小时
注册时间
2005-3-10
最后登录
2025-5-25

初级热心勋章 初级信用勋章

楼主
kk22boy 发表于 2012-12-12 10:50:11 |AI写论文

+2 论坛币
k人 参与回答

经管之家送您一份

应届毕业生专属福利!

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

经管之家联合CDA

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

感谢您参与论坛问题回答

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

+2 论坛币

Problem

You want to put multiple graphs on one page.

  1. library(ggplot2)

  2. # This example uses the ChickWeight dataset, which comes with ggplot2
  3. # First plot
  4. p1 <-
  5.     ggplot(ChickWeight, aes(x=Time, y=weight, colour=Diet, group=Chick)) +
  6.     geom_line() +
  7.     ggtitle("Growth curve for individual chicks")

  8. # Second plot
  9. p2 <-
  10.     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. # Third plot
  15. p3 <-
  16.     ggplot(subset(ChickWeight, Time==21), aes(x=weight, colour=Diet)) +
  17.     geom_density() +
  18.     ggtitle("Final weight, by diet")

  19. # Fourth plot
  20. p4 <-
  21.     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")        # No legend (redundant in this graph)   
复制代码

Once the plot objects are set up, we can render them with multiplot. This will make two columns of graphs:

  1. ultiplot(p1, p2, p3, p4, cols=2)
复制代码

multiplot function

  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.   require(grid)

  13.   # Make a list from the ... arguments and plotlist
  14.   plots <- c(list(...), plotlist)

  15.   numPlots = length(plots)

  16.   # If layout is NULL, then use 'cols' to determine layout
  17.   if (is.null(layout)) {
  18.     # Make the panel
  19.     # ncol: Number of columns of plots
  20.     # nrow: Number of rows needed, calculated from # of cols
  21.     layout <- matrix(seq(1, cols * ceiling(numPlots/cols)),
  22.                     ncol = cols, nrow = ceiling(numPlots/cols))
  23.   }

  24. if (numPlots==1) {
  25.     print(plots[[1]])

  26.   } else {
  27.     # Set up the page
  28.     grid.newpage()
  29.     pushViewport(viewport(layout = grid.layout(nrow(layout), ncol(layout))))

  30.     # Make each plot, in the correct location
  31.     for (i in 1:numPlots) {
  32.       # Get the i,j matrix positions of the regions that contain this subplot
  33.       matchidx <- as.data.frame(which(layout == i, arr.ind = TRUE))

  34.       print(plots[[i]], vp = viewport(layout.pos.row = matchidx$row,
  35.                                       layout.pos.col = matchidx$col))
  36.     }
  37.   }
  38. }
复制代码

二维码

扫码加我 拉你入群

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

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

关键词:Multiple ggplot2 multip Graphs gplot one function recently multiple

已有 2 人评分经验 学术水平 热心指数 信用等级 收起 理由
风敲竹 + 100 + 5 + 5 + 5 精彩帖子
epoh + 3 + 3 + 3 精彩帖子

总评分: 经验 + 100  学术水平 + 8  热心指数 + 8  信用等级 + 8   查看全部评分

如果该贴对您有些许帮助,希望你能回复一下或者评一下热心指数!谢谢!

沙发
coddz 发表于 2012-12-12 14:05:40
非常实用的函数,谢谢LZ分享!

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

本版微信群
加好友,备注cda
拉您进交流群
GMT+8, 2026-1-1 22:16