2013-02-17 60 views
7

我花了很多时间试图在一个地块中安装11个图表并使用gridExtra来排列它们,但是我失败了,所以我希望你能帮上忙。使用gridExtra排列很多地块

我的钻石(称之为size1)等11个类别(size2)11个分类,我想通过增加size2绘制平均价格为每台增加size1和增加clarity(从1到6)如何变化钻石,并在同一图表中绘制所有11个图。 我尝试使用gridExtra建议在其他职位,但传说是远离右侧,所有图形都挤在左边,请你帮我弄清楚如何在gridExtra传说的“宽度”必须指定?我找不到任何好的解释。非常感谢你的帮助,我真的很感激它...

我一直在试图找到一个很好的例子来重新创建我的数据框,但也失败了。我希望这个数据框有助于理解我正在尝试做什么,我无法使其工作并与我的工作一样,并且一些地块没有足够的数据,但重要的部分是使用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 2013-02-18 18:59:05

+0

谢谢baptiste,这个作品也是! – user971102 2013-02-22 16:52:17

回答

3

我不得不稍微改变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) 

preview here

编辑(16/07/2015):与gridExtra> = 2.0.0,main参数已更名为top

+0

嗨hrbrmstr,非常感谢你的帮助。传说现在处于正确的位置。如果可能的话,你能解释一下代码中的“宽度”在做什么? – user971102 2013-02-18 13:37:41

+0

这些值表示 - “分”表示每列将占用的空间量。所以这些地块有很多空间,而传说的空间也很小。我现在也可以粘贴代码和图片,你会看到我已经修复了代码,使所有的图都能正常工作。 – hrbrmstr 2014-04-07 01:59:17