【问题标题】:Arrange many plots using gridExtra使用 gridExtra 排列多个绘图
【发布时间】:2013-02-17 11:40:09
【问题描述】:

我花了很多时间试图将 11 个图放在一个图中并使用 gridExtra 排列它们,但我失败了,所以我求助于你,希望你能提供帮助。

我有 11 种钻石分类(称为size1)和其他 11 种分类(size2),我想绘制每个增加size1 和增加clarity(从 1 到 6)的中位数价格) 通过增加size2 的菱形而变化,并在同一张图中绘制所有 11 个图。 我尝试按照其他帖子中的建议使用gridExtra,但图例离右边很远,所有图表都向左挤压,你能帮我弄清楚gridExtra 中图例的“宽度”如何要指定?我找不到任何好的解释。非常感谢您的帮助,我真的很感激...

我一直在尝试找到一个很好的示例来重新创建我的数据框,但也失败了。我希望这个数据框有助于理解我正在尝试做的事情,我无法让它工作并且与我的相同,并且有些地块没有足够的数据,但重要的部分是使用 @987654329 来处理图表@(虽然如果您在其他部分有其他 cmets 请告诉我):

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)

【问题讨论】:

  • mylegendwidth 是一个向量,你可能想要 sum(mylegend$width) 代替。
  • 谢谢你 baptiste,这也有效!

标签: r ggplot2 gridextra


【解决方案1】:

我不得不稍微更改 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,非常感谢您的帮助。图例现在处于正确的位置。如果可能,您能否解释一下您的代码中的“宽度”在做什么?
  • 这些值表示 - “点”每列将占用的空间量。所以有大量的空间用于情节,而较小的空间用于图例。我现在也可以粘贴代码和图像,你会看到我已经修复了代码,使所有绘图也能正常工作。
  • 如果你有时间可以看看这个问题吗? stackoverflow.com/questions/65832784/… 谢谢!
猜你喜欢
  • 2021-04-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-09-01
  • 2018-11-07
  • 1970-01-01
  • 1970-01-01
  • 2013-08-27
相关资源
最近更新 更多