使用ggplot2在一个画布中使用多个图形

Jul*_*iaz 31 r ggplot2

我试图基于此表将两个ggplot2图合并为一个:

   Type    RatingA  RatingB
1  One     3        36
2  Two     5        53
3  One     5        57
4  One     7        74
5  Three   4        38
6  Three   8        83
Run Code Online (Sandbox Code Playgroud)

我想制作两个散点图,其中y轴的等级平均值,x轴上的类型.

这是我创建每个图形的方式:

p1 <- ggplot(test, aes(x=reorder(Type, RatingA, mean), y=RatingA)) +
        stat_summary(fun.y="mean", geom="point")

p2 <- ggplot(test, aes(x=reorder(Type, RatingB, mean), y=RatingB)) + 
        stat_summary(fun.y="mean", geom="point")
Run Code Online (Sandbox Code Playgroud)

由于p1和p2具有相同的x轴,我希望它们可以垂直排序.我看着facet_align,但我找不到能做到这一点的东西.

Ist*_*sta 46

您可以grid.arrange()在gridExtra包中使用如下:

grid.arrange(p1, p2)
Run Code Online (Sandbox Code Playgroud)

  • 您可以添加更多信息吗?该函数从哪里来?如何导入呢?另外,看起来该函数没有绘制到同一画布上,而是连接了两个图。 (2认同)

Cha*_*ase 14

胡里奥

你提到p1和p2具有相同的x轴,但你基于均值进行的重新排序并不能使它们相同.p1的轴是"一 - >二 - >三",而p2轴是"两 - >一 - >三".这是故意的吗?

无论如何,ggplot提供了一些其他的解决方案,这些地块合并成一个,即colourfaceting(你可能已经试过?).其中任何一个的第一步是将meltdata.frame改为长格式.我们将识别id变量"Type"并melt假设其余列为melted.

test.m <- melt(test, id.var = "Type")
Run Code Online (Sandbox Code Playgroud)

快速检查新对象的结构表明大多数都是一致的,除了类型的级别有点不明显:

> str(test.m)
'data.frame':   12 obs. of  3 variables:
 $ Type    : Factor w/ 3 levels "One","Three",..: 1 3 1 1 2 2 1 3 1 1 ...
 $ variable: Factor w/ 2 levels "RatingA","RatingB": 1 1 1 1 1 1 2 2 2 2 ...
 $ value   : int  3 5 5 7 4 8 36 53 57 74 ...
Run Code Online (Sandbox Code Playgroud)

所以让我们重温水平:

test.m$Type <- factor(test.m$Type, c("One", "Three", "Two"), c("One", "Two", "Three"))
Run Code Online (Sandbox Code Playgroud)

现在为了密谋.有颜色:

ggplot(test.m, aes(x = Type, y = value, group = variable, colour = variable)) + 
stat_summary(fun.y = "mean", geom = "point") 
Run Code Online (Sandbox Code Playgroud)

或与方面:

ggplot(test.m, aes(x = Type, y = value, group = variable)) + 
stat_summary(fun.y = "mean", geom = "point") +
facet_grid(variable ~ ., scales = "free")
Run Code Online (Sandbox Code Playgroud)

注意我scales = "free"在分面中使用了参数,因此每个图都有自己的比例.如果那不是您想要的效果,只需删除该参数即可.


And*_*usZ 8

这是一个老问题,但我最近找到了multiplot功能,让他的工作做得很好.

multiplot功能来自Cookbook for R:

它本身的功能是:

# Multiple plot function
#
# ggplot objects can be passed in ..., or to plotlist (as a list of ggplot objects)
# - cols:   Number of columns in layout
# - layout: A matrix specifying the layout. If present, 'cols' is ignored.
#
# If the layout is something like matrix(c(1,2,3,3), nrow=2, byrow=TRUE),
# then plot 1 will go in the upper left, 2 will go in the upper right, and
# 3 will go all the way across the bottom.
#
multiplot <- function(..., plotlist=NULL, file, cols=1, layout=NULL) {
  require(grid)

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

  numPlots = length(plots)

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

 if (numPlots==1) {
    print(plots[[1]])

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

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

      print(plots[[i]], vp = viewport(layout.pos.row = matchidx$row,
                                      layout.pos.col = matchidx$col))
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

您只需要将此函数提供给您的脚本即可.