按自定义比例和间距排列ggplots [英] Arrange ggplots together in custom ratios and spacing

查看:194
本文介绍了按自定义比例和间距排列ggplots的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图将n个barlot和一个共同的标签绘制在底部。我的问题是,grid.arrange将这两个地块组合在50%-50%之内。我正在寻找类似布局矩阵的东西,您可以指定4个插槽,前3个由第一个绘图和第二个绘图的最后一个插槽占用。根据图的数量自定义。





我从我正在尝试的代码中获取示例代码:

  #load libraries 
require(ggplot2)
require(reshape)
require(grid)
require(gridExtra)

#data创建
#DATA
temp< -data.frame(var1 = sample(0:100,100,replace = T))
temp $ var2< -100-temp $ var1
temp $ type< -factor(c(rep(S1,50),rep(S2,50)))
temp $ label< -factor(rep(1:50,2))
temp1< -melt(temp,id.var = c(type,label))
#LABELS
labs1 <-data.frame(pos = c(1,8,22, 45,50))
labs2< -data.frame(pos1 = round((diff(labs1 $ pos)/ 2)+ labs1 $ pos [1:length(labs1 $ pos)-1],1),
lab = c(A,B,D,E))

#plots
plot1< -ggplot(data = temp1)+
geom_bar(aes(x = label,y = value,fill = variable),stat =identity,space = 0,width = 1)+
facet_grid(type〜。)+ theme_bw()+实验室(X = NULL,Y = NUL L)+
scale_y_continuous(expand = c(0,0))+
theme(legend.position =none,axis.text = element_blank(),axis.ticks = element_blank())
plot2 <-ggplot()+
geom_line(data = labs1,aes(x = pos,y = -0.05),size = 0.6)+
geom_point(data = labs1,aes(x = pos,y = -0.05))+ labs(x = NULL,y = NULL)+
geom_text(data = labs2,aes(x = pos1,y = -0.1,label = lab))+
theme_bw()+ scale_x_continuous(expand = c(0,0))+ scale_y_continuous(limit = c(-0.5,0))+
theme(legend.position =none,axis.text = element_blank (),axis.ticks = element_blank())

plot3< -grid.arrange(plot1,plot2)
#也许有一种方法可以让plot1占用1/3的地块面积。

两张图不完全一致。但那是另一个问题。这是有用的
我如何使用grid.arrange来安排任意数量的ggplots?
我可以逐一绘制每一个,但我想要有facet标签。

解决方案

grid.arrange(plot1,plot2,widths = c(0.7,0.3),ncol = 2)

抱歉,您的示例:

  grid.arrange(plot1,plot2,heights = c(0.7,0.3),nrow = 2)



  blank< -rectGrob(gp = gpar( 
grid.arrange(plot1,blank,plot2,heights = c(0.7,0.05,0.25),nrow = 3)


I am trying to combine n number of barplots with one common label plot at the bottom. My problem is that grid.arrange combines the two plots in 50%-50%. I am looking for something like the layout matrix where you can specify 4 slots and first 3 to be taken up by first plot and last slot by second plot. And customise similarly depending on number of plots.

I have a sample code here from what I am trying:

#load libraries
require(ggplot2)
require(reshape)
require(grid)
require(gridExtra)

#data creation
#DATA
temp<-data.frame(var1=sample(0:100,100,replace=T))
temp$var2<-100-temp$var1
temp$type<-factor(c(rep("S1",50),rep("S2",50)))
temp$label<-factor(rep(1:50,2))
temp1<-melt(temp,id.var=c("type","label"))
#LABELS
labs1<-data.frame(pos=c(1,8,22,45,50))
labs2<-data.frame(pos1=round((diff(labs1$pos)/2)+labs1$pos[1:length(labs1$pos)-1],1),
         lab=c("A","B","D","E"))

#plots
plot1<-ggplot(data=temp1)+
geom_bar(aes(x=label,y=value,fill=variable),stat="identity",space=0,width=1)+
facet_grid(type~.)+theme_bw()+labs(x=NULL,y=NULL)+
scale_y_continuous(expand=c(0,0))+
theme(legend.position="none",axis.text=element_blank(),axis.ticks=element_blank())
plot2<-ggplot()+
geom_line(data=labs1,aes(x=pos,y=-0.05),size=0.6)+
geom_point(data=labs1,aes(x=pos,y=-0.05))+labs(x=NULL,y=NULL)+
geom_text(data=labs2,aes(x=pos1,y=-0.1,label=lab))+
theme_bw()+scale_x_continuous(expand=c(0,0))+scale_y_continuous(limit=c(-0.5,0))+
theme(legend.position="none",axis.text=element_blank(),axis.ticks=element_blank())

plot3<-grid.arrange(plot1, plot2)
#here perhaps there is a way to say plot1 to take up 1/3 of the plot area.

The two plots don't align quite perfectly. But that's another issue. And this was useful How can I arrange an arbitrary number of ggplots using grid.arrange? I could plot each one by one, but I would like to have the facet labels.

解决方案

grid.arrange(plot1, plot2, widths=c(0.7, 0.3), ncol=2)

sorry for your example:

grid.arrange(plot1, plot2, heights=c(0.7, 0.3), nrow=2)

EDIT - for spacing between plots:

blank<-rectGrob(gp=gpar(col="white")) # make a white spacer grob
grid.arrange(plot1, blank, plot2, heights=c(0.7, 0.05, 0.25), nrow=3)

这篇关于按自定义比例和间距排列ggplots的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆