我想安排使用ggplot2創建的2個圖,並希望繪圖是一個正方形的大小和一個接一個,用它們旁邊的常見圖例,以便該圖像很好地適合於肖像樣式頁面。 問題是,當我使用grid.arrange來安排Groes時,標籤和圖例變得很小,並且劇情空間很大。保留或設置文本標籤和網格大小的比例.arrange
必須有些東西我缺少關於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()
+1的代碼是有點長讀通過,但良好的可重複的例子。 – 2013-04-30 11:31:24
您的PDF文件是20x30英寸大,所以默認的字體大小(通常約12分)將使文本無法讀取。你需要這麼大的輸出嗎? – baptiste 2013-04-30 11:48:16
嗨巴蒂斯特,我不需要這麼大的輸出(我只是需要適應圖像的默認頁面),但我需要這兩個情節是方形的,它看起來像調整PDF大小正在幫助...是否有一個簡單的方法來做到這一點,我沒有看到?感謝您的幫助。 – user971102 2013-04-30 12:31:24