我正在尝试安排使用创建的 2 个图,ggplot2
并且希望这些图的大小为正方形,一个相邻,旁边有常见的图例,以便图像很好地适合纵向样式页面。问题是当我grid.arrange
用来排列 Grobs 时,标签和图例变得很小,而且情节空间很大。
关于 grid.arrange 包,我一定缺少一些东西,比如正确设置所有比率的命令,但是我尝试了很多不同的文本大小组合并尝试设置图的大小,但似乎没有任何效果. 你能帮我么?
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()