在grid.arrange中保留或设置文本标签与绘图大小之间的比率 [英] Keep or set the ratio between text labels and size of plot in grid.arrange

查看:318
本文介绍了在grid.arrange中保留或设置文本标签与绘图大小之间的比率的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图安排使用ggplot2创建的2个地块,并希望地块成为正方形的一个,另一个紧挨着另一个,并在旁边有常见的图例,以便该图像很好地适合于肖像样式页。
问题是,当我使用grid.arrange来排列Groes时,标签和图例变得很小,并且剧情空间很大。

必须有一些我对grid.arrange软件包缺少了一些信息,比如正确设置所有比率的命令,但我已经尝试过很多不同的文本大小组合并试图设置剧情的大小,但似乎没有任何工作。
你能帮我吗?

  x1 < -  c(10,20,30,40,50,60,70,80)
(20,40,60,80)
y1 < - c(10,30,100,30,20,40,20,10)
y2 < - c(20, (1,1,1,2,2,2,2,2)
z2 < - c(2,2,1,2, 1,2,1,2)
df1 < - data.frame(x = x1,y = y1,z = z1)
df2 < - data.frame(x = x2,y = y2,z = z1)

p1 < - ggplot(df1,aes(x = x,
y = y,
group = z,
color = z) )+
geom_point()+ theme_bw()+
xlab(这个尺寸很小,使用grid.arrange)+ ylab(尺寸很小,使用grid.arrange)+ ggtitle(A)+ theme(panel.grid.major = element_blank(),panel.grid.minor = element_blank(),plot.title = element_text(size = 16,hjust = 0),axis.title = element_text size = 12),axis.text = element_text(size = 12),legend.text = element_text(size = 12),legend.title = element_text(size = 12))+
geom_abline(截距= 0,斜率= 1)+
xlim(0,100)+ ylim(0,100)+ guides(color = guide_legend(\\\
这是一个\ (a)(x = x,
y = y,$ b,n多行传说标题),override.aes = list(size = 10)))

p2 < - ggplot $ b group = z,
color = z))+
geom_point()+ theme_bw()+
xlab(这个尺寸很小,使用grid.arrange)+ ylab 这个尺寸很小,使用grid.arrange)+ ggtitle(B)+ theme(panel.grid.major = element_blank(),panel.grid.minor = element_blank(),plot.title = element_text(size = size = 12),legend.title = element_text(size = 12) ))+
geom_abline(截距= 0,slope = 1)+
xlim(0,100)+ ylim(0,100)+ guides(color = guide_legend(\\\
这是一个\\ \
多线图例标题,override.aes =列表(大小= 10)))

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

mylegend< -g_legend(p1)


pdf(my.figure.pdf ,height = 20,width = 30)##我已经尝试了许多不同的值,这也是

grid.arrange(arrangeGrob(p1 + theme(legend.position =none),#widths (20,cm),高度=单位(20,cm),
p2 +主题(legend.position =none),#widths =单位(20,cm heights = unit(20,cm),
main = textGrob(主图标题也很小并且很难对齐左边,hjust = 0.6,gp = gpar(fontsize = 22)),nrow = 1,heights = rep(10,10)),#widths = unit(10,cm),heights = unit(4,cm)),
mylegend,widths = unit.c(unit 1,npc) - sum(mylegend $ width),sum(mylegend $ width)),nrow = 1)

dev.off()


解决方案

为每个图添加一个主题()设置你想改变的元素的大小,例如

 主题(axis.text.x = element_text(size = 20) ,axis.title.x = element_text(size = 20,c olour =red))


您可以尝试使用 pushViewport

code>手动设置视口,但是您可能需要仔细研究一下,才能得到您想要的样子:

  grid.newpage()
#创建视口的网格排列,将图块放置到...
#宽度和高度是标准化的父坐标,从0到1,其中1是图纸页面的整个宽度
#尊重参数强制宽度和高度相互尊重,因为它们被设置
pushViewport(viewport(layout = grid.layout(2,3,heights =单位(c(0.02,0.45),npc),宽度=单位(c(0.4,0.4,0.1),npc),尊重=矩阵(rep(1,6),2))))
#我们在由layout.pos.row和layout.pos.col $ b $指定的页面上打印特定单元格 b print(p1 + theme(legend.position =none),vp = viewport(layout.pos.row = 2,layout.pos.col = 1))
print(p2 + theme(legend.position =none),vp = viewport(layout.pos.row = 2,layout.pos.col = 2))
#grid.text被输出到第一行,并在列1:2
grid.text(主要图形标题也很小且很难对齐左侧,只是=左,x =单位(0.01,npc),y =单位(0.5,npc), vp = viewport(layout.pos.row = 1,layout.pos.col = 1:2))
#我们使用upViewport上升到父视口的水平
upViewport(0)
#然后,我们为传说中的表格grob定义一个新的视口。
#我对这个问题有困难,所以我们设置x和y坐标,并使其变窄但高(0.1 npc宽度和0.75 noc高度)
vp3 < - viewport(width = unit(0.1,然后激活这个视口
pushViewport(vp3)
#我们输出这个视口传说这是一个tableGrob到这个视口
grid.draw(mylegend)
popViewport()


I am trying to arrange 2 plots created using ggplot2 and would like the plots to be the size of a square and one next to the other, with the common legend next to them, so that the image fits nicely in a portrait style page. The problem is when I use grid.arrange to arrange the Grobs the labels and legends become tiny, and the plot space huge.

There must be something I am missing about the grid.arrange package, like a command to set all the ratios correctly, but I have tried so many different combinations of text sizes and have tried setting the sizes of the plots, but nothing seems to work. Can you please help me?

x1 <- c(10,20,30,40,50,60,70,80)
x2 <- c(20,40,60,80)
y1 <- c(10,30,100,30,20,40,20,10)
y2 <- c(20,60,20,30)
z1 <- c(1,1,1,2,2,2,2,2)
z2 <- c(2,2,1,2,1,2,1,2)
df1 <- data.frame(x=x1,y=y1, z=z1)
df2 <- data.frame(x=x2,y=y2, z=z1)

p1<-  ggplot(df1, aes(x=x,
     y=y,
     group=z,
     colour=z)) +
     geom_point() + theme_bw() +
     xlab("The size of this is tiny using grid.arrange")  +ylab("The size of this is tiny using grid.arrange") + ggtitle("A") + theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank(), plot.title=element_text(size=16, hjust=0), axis.title = element_text(size=14), axis.text= element_text(size=12), legend.text= element_text(size = 12), legend.title = element_text(size = 12)) +
     geom_abline(intercept = 0, slope = 1) +
     xlim(0, 100) + ylim(0, 100) + guides(colour = guide_legend("\n This is a \n multi-line legend title", override.aes = list(size = 10)))

p2<-  ggplot(df2, aes(x=x,
     y=y,
     group=z,
     colour=z)) +
     geom_point() + theme_bw() +
     xlab("The size of this is tiny using grid.arrange")  +ylab("The size of this is tiny using grid.arrange") + ggtitle("B") + theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank(), plot.title=element_text(size=16, hjust=0), axis.title = element_text(size=14), axis.text= element_text(size=12), legend.text= element_text(size = 12), legend.title = element_text(size = 12)) +
     geom_abline(intercept = 0, slope = 1) +
     xlim(0, 100) + ylim(0, 100) + guides(colour = guide_legend("\n This is a \n multi-line legend title", override.aes = list(size = 10)))

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)


pdf("my.figure.pdf", height = 20, width = 30) ## I have tried many different values for this too

grid.arrange(arrangeGrob(p1 + theme(legend.position="none"), # widths = unit(20, "cm"), heights = unit(20, "cm"),
p2 + theme(legend.position="none"), # widths = unit(20, "cm"),heights = unit(20, "cm"),
main = textGrob("The main figure title is also small and hard to align left",  hjust =0.6, gp = gpar(fontsize = 22)), nrow=1, heights=rep(10,10)), # widths = unit(10, "cm"),heights = unit(4, "cm")),
mylegend, widths=unit.c(unit(1, "npc") - sum(mylegend$width), sum(mylegend$width)), nrow=1)

dev.off()

解决方案

Add a theme() to each plot and set the size of the elements you want to change, e.g

theme( axis.text.x=element_text(size=20) ,  axis.title.x = element_text(size = 20 , colour = "red" ) )

You can try arranging things on a grid page using pushViewport to manually set the viewports like so, but you will probably have to play around with it a bit to get it exactly how you want:

grid.newpage()
# Create a grid arangement of viewports to 'put' the plots into...
# widths and heights are normalised parent coordinates which scale from 0 to 1 with 1 being the entire width of the plot page
# The respect argument forces widths and heights to respect each other as they are set
pushViewport( viewport( layout = grid.layout( 2 , 3 , heights = unit( c( 0.02 , 0.45 ) , "npc" ) , widths = unit( c( 0.4 , 0.4 , 0.1 ) , "npc" ) , respect = matrix(rep(1,6),2) ) ) ) 
# We print plots to particular 'cells' on our page specified by the layout.pos.row and layout.pos.col
print( p1 + theme(legend.position="none") , vp = viewport( layout.pos.row = 2 , layout.pos.col = 1 ) )
print( p2 + theme(legend.position="none") , vp = viewport( layout.pos.row = 2, layout.pos.col = 2 ) )
# The grid.text is output to row one, and breaks across columns 1:2
grid.text("The main figure title is also small and hard to align left"  , just = "left" , x = unit(0.01, "npc"), y = unit(0.5, "npc"), vp = viewport( layout.pos.row = 1, layout.pos.col = 1:2) )
# We use upViewport to go up a level to the parent viewport
upViewport(0)
# We then define a new viewport for the legend which is a table grob.
# I had difficulty with this one so we set x and y coordinates and make it narrow but tall (0.1 npc width and 0.75 noc height)
vp3 <- viewport( width = unit(0.1,"npc") , height = unit(0.75 ,"npc") , x = 0.93, y = .5)
# We then activate this viewport
pushViewport(vp3)
# we output the legend which is a tableGrob to this viewport
grid.draw( mylegend )
popViewport()

这篇关于在grid.arrange中保留或设置文本标签与绘图大小之间的比率的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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