使用gridExtra排列许多图 [英] Arrange many plots using gridExtra
问题描述
我花了很多时间试图在一个阴谋中安装11张图表,并使用 gridExtra
来安排它们,但我失败了,所以我希望你能帮上忙。
我有11个钻石分类(称之为 size1
)和其他11个分类( size2
),我想绘制每个增加的 size1
和增加净度
(从1到6)因钻石的
我尝试过使用 gridExtra
按其他帖子中的建议,但图例离右边很远,所有图形都挤在左边,能否请你帮我找出如何指定 gridExtra
中图例的宽度?我找不到任何好的解释。非常感谢您的帮助,我真的很感激它......
我一直在试图找到一个很好的例子来重新创建我的数据框,但是也失败了。我希望这个数据框有助于理解我想要做的事情,我无法让它工作并与我的工作一样,并且一些地块没有足够的数据,但重要的部分是使用 gridExtra
(尽管如果您对其他部分有其他评论,请让我知道):
<$ c $ (样本(1:1000,100,替换= TRUE),ncol = 1)(数据库(ggplot2)
库(gridExtra)
df< - data.frame ))
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 $净度,中断= 6)
#为每个图创建一个图size1,绘制中位数价格与size2之间的清晰度:
(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),位数);
名称(mydf)[1] ='size2'
名称(mydf)[2] ='净化'
名称(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 $清晰度),geom =line,color = as.factor(mydf $ clarity),xlab =样本数量,ylab =中值价格,main = paste(区域编号为,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)))
}
#无法取得一些工作,所以使用:
p5 = p4
p6 = p4
p7 = p4
p8 = p4
p9 = p4
#使用gridExtra排列11个图:
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 =主标题,
left =),mylegend,
widths = unit.c(unit(1,npc) - mylegend $ width,mylegend $ width),nrow = 1)
我必须稍微改变 qplot
循环调用。把这些因素放在数据框中),因为它会抛出一个不匹配的大小错误。我不包括那一点,因为那部分显然是在你的环境中工作,或者它是一个错误的粘贴。
尝试调整你的宽度
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 $清晰度= cut(df $净度,中断= 6)
#为每个尺寸1创建一张图,绘制中间价与清晰度的大小2相比:
(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 $清晰度),中位数);
名称(mydf)[1] ='size2'
名称(mydf)[2] ='净化'
名称(mydf)[3] ='median_price'
mydf $ clear< - factor(mydf $ clarity)
assign(paste(p,c,sep =),
qplot(data = mydf,
x = as.numeric(size2),
y = median_price,
group =净度,
geom =line,color =净度,
xlab =样本数量,
ylab =中位价,
main =粘贴(地区编号为,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 =
axis.text.x = element_text(size = 8),
axis.text.y = element_text(size = 8)))
}
#使用gridExtra排列11个图:
g_legend< -function(a.gplot){
tmp< - ggplot_gtable(ggplot_build(a.gplot))
leg < - 其中(sapply(tmp $ grobs,function(x)x $ name)==guide-box)
legend< - tmp $ grobs [[leg]]
return )}
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 =主标题,
left =),mylegend,
widths =单位(c(1000,50) ,pt),nrow = 1)
编辑(16/07/2015):with gridExtra
> = 2.0.0, main
参数已被重命名为 top
。
I have spent many hours trying to fit 11 graphs in one plot and arrange them using gridExtra
but I have failed miserably, so I turn to you hoping you can help.
I have 11 classifications of diamonds (call it size1
) and other 11 classifications (size2
) and I want to plot how the median price for each increasing size1
and increasing clarity
(from 1 to 6) varies by increasing size2
of the diamonds, and plot all the 11 plots in the same graph.
I tried using gridExtra
as suggested in other posts but the legend is far away to the right and all the graphs are squished to the left, can you please help me figure out how the "widths" for the legend in gridExtra
has to be specified? I cannot find any good explanations. Thank you very much for your help, I really appreciate it...
I have been trying to find a good example to recreate my data frame but failed in this as well. I hope this data frame helps understand what I am trying to do, I could not get it to work and be the same as mine and some plots don't have enough data, but the important part is the disposition of the graphs using gridExtra
(although if you have other comments on other parts please let me know):
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)
I had to change the qplot
loop call slightly (i.e. put the factors in the data frame) as it was throwing a mismatched size error. I'm not including that bit since that part is obviously working in your environment or it was an errant paste.
Try adjusting your widths
units like this:
widths=unit(c(1000,50),"pt")
And you'll get something a bit closer to what you were probably expecting:
And, I can paste code now a few months later :-)
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)
Edit (16/07/2015): with gridExtra
>= 2.0.0, the main
parameter has been renamed top
.
这篇关于使用gridExtra排列许多图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!