使用gridExtra排列多个图表

7
我花费了很多时间尝试将11个图形适配到一个图中,并使用gridExtra排列它们,但是我失败了,所以我求助于您希望您能帮忙。
我有11种钻石分类(称为size1),还有另外11个分类(size2),我想绘制每个增加的size1和增加的clarity(从1到6)的中位数价格随着钻石增加的size2变化的情况,并在同一图中绘制所有11个图。我尝试使用其他帖子中建议的gridExtra,但图例太远了,而且所有图都被挤到了左边,请问您能否帮我找出如何指定gridExtra中“widths”的好方法?我找不到任何好的解释。非常感谢您的帮助,我真的很感激...
我一直在尝试找到一个好的示例来重新创建我的数据框,但也失败了。我希望这个数据框可以帮助理解我要做什么,但我无法让它像我的数据框那样工作,并且一些图没有足够的数据,但重要的是使用gridExtra排列图形(如果您对其他部分有评论,请告诉我)。
library(ggplot2)
library(gridExtra)

df <- data.frame(price=matrix(sample(1:1000, 100, replace = TRUE), ncol = 1))

df$size1 = 1:nrow(df)
df$size1 = cut(df$size1, breaks=11)
df=df[sample(nrow(df)),]
df$size2 = 1:nrow(df)
df$size2 = cut(df$size2, breaks=11)
df=df[sample(nrow(df)),]
df$clarity = 1:nrow(df)
df$clarity = cut(df$clarity, breaks=6)

# Create one graph for each size1, plotting the median price vs. the size2 by clarity:
for (c in 1:length(table(df$size1))) {

  mydf = df[df$size1==names(table(df$size1))[c],]
  mydf = aggregate(mydf$price, by=list(mydf$size2, mydf$clarity),median);   
  names(mydf)[1] = 'size2'
  names(mydf)[2] = 'clarity'
  names(mydf)[3] = 'median_price'

  assign(paste("p", c, sep=""), qplot(data=mydf, x=as.numeric(mydf$size2), y=mydf$median_price, group=as.factor(mydf$clarity), geom="line", colour=as.factor(mydf$clarity), xlab = "number of samples", ylab = "median price", main = paste("region number is ",c, sep=''), plot.title=element_text(size=10)) + scale_colour_discrete(name = "clarity")  + theme_bw() + theme(axis.title.x=element_text(size = rel(0.8)), axis.title.y=element_text(size = rel(0.8)) , axis.text.x=element_text(size=8),axis.text.y=element_text(size=8) ))
  }

# Couldnt get some to work, so use:
p5=p4
p6=p4
p7=p4
p8=p4
p9=p4

# Use gridExtra to arrange the 11 plots:

g_legend<-function(a.gplot){
tmp <- ggplot_gtable(ggplot_build(a.gplot))
leg <- which(sapply(tmp$grobs, function(x) x$name) == "guide-box")
legend <- tmp$grobs[[leg]]
return(legend)}

mylegend<-g_legend(p1)


grid.arrange(arrangeGrob(p1 + theme(legend.position="none"),
                     p2 + theme(legend.position="none"),
                     p3 + theme(legend.position="none"),
                     p4 + theme(legend.position="none"),
                     p5 + theme(legend.position="none"),
                     p6 + theme(legend.position="none"),
                     p7 + theme(legend.position="none"),
                     p8 + theme(legend.position="none"),
                     p9 + theme(legend.position="none"),
                     p10 + theme(legend.position="none"),
                     p11 + theme(legend.position="none"),
                     main ="Main title",
                     left = ""), mylegend, 
                    widths=unit.c(unit(1, "npc") - mylegend$width, mylegend$width), nrow=1)

1
mylegendwidth是一个向量,你可能想要使用sum(mylegend$width) - baptiste
1个回答

4

我需要稍微调整qplot循环调用(即将因素放入数据框中),因为它会抛出不匹配的大小错误。我不包括那部分,因为在您的环境中,那部分显然是有效的,或者是一个错误的粘贴。

尝试像这样调整widths单位:

widths=unit(c(1000,50),"pt")

您可能想要的是以下内容:

现在我可以贴上几个月后的代码了 :-)

library(ggplot2)
library(gridExtra)

df <- data.frame(price=matrix(sample(1:1000, 100, replace = TRUE), ncol = 1))

df$size1 = 1:nrow(df)
df$size1 = cut(df$size1, breaks=11)
df=df[sample(nrow(df)),]
df$size2 = 1:nrow(df)
df$size2 = cut(df$size2, breaks=11)
df=df[sample(nrow(df)),]
df$clarity = 1:nrow(df)
df$clarity = cut(df$clarity, breaks=6)

# Create one graph for each size1, plotting the median price vs. the size2 by clarity:
for (c in 1:length(table(df$size1))) {

  mydf = df[df$size1==names(table(df$size1))[c],]
  mydf = aggregate(mydf$price, by=list(mydf$size2, mydf$clarity),median);   
  names(mydf)[1] = 'size2'
  names(mydf)[2] = 'clarity'
  names(mydf)[3] = 'median_price'
  mydf$clarity <- factor(mydf$clarity)

  assign(paste("p", c, sep=""), 
         qplot(data=mydf, 
               x=as.numeric(size2), 
               y=median_price, 
               group=clarity, 
               geom="line", colour=clarity, 
               xlab = "number of samples", 
               ylab = "median price", 
               main = paste("region number is ",c, sep=''), 
               plot.title=element_text(size=10)) + 
           scale_colour_discrete(name = "clarity") + 
           theme_bw() + theme(axis.title.x=element_text(size = rel(0.8)), 
                              axis.title.y=element_text(size = rel(0.8)), 
                              axis.text.x=element_text(size=8),
                              axis.text.y=element_text(size=8) ))
}

# Use gridExtra to arrange the 11 plots:

g_legend<-function(a.gplot){
  tmp <- ggplot_gtable(ggplot_build(a.gplot))
  leg <- which(sapply(tmp$grobs, function(x) x$name) == "guide-box")
  legend <- tmp$grobs[[leg]]
  return(legend)}

mylegend<-g_legend(p1)


grid.arrange(arrangeGrob(p1 + theme(legend.position="none"),
                         p2 + theme(legend.position="none"),
                         p3 + theme(legend.position="none"),
                         p4 + theme(legend.position="none"),
                         p5 + theme(legend.position="none"),
                         p6 + theme(legend.position="none"),
                         p7 + theme(legend.position="none"),
                         p8 + theme(legend.position="none"),
                         p9 + theme(legend.position="none"),
                         p10 + theme(legend.position="none"),
                         p11 + theme(legend.position="none"),
                         top ="Main title",
                         left = ""), mylegend, 
             widths=unit(c(1000,50),"pt"), nrow=1)

预览在这里

编辑 (16/07/2015): 从 gridExtra >= 2.0.0 版本开始,main 参数已更名为 top


嗨,hrbrmstr,非常感谢你的帮助。图例现在处于正确的位置。如果可能的话,您能解释一下您代码中的“widths”是做什么的吗? - user971102
这些值表达了“点数”,表示每列将占用的空间量。因此,绘图有大量的空间,图例有较少的空间。现在我也可以粘贴代码和图像,您会看到我已经修复了所有绘图的正确工作。 - hrbrmstr
如果您有时间,能否请看一下这个问题?https://stackoverflow.com/questions/65832784/r-arranging-multiple-plots-together-using-gridextra 谢谢! - stats_noob

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接