2013-04-30 38 views
2

我想安排使用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() 
+0

+1的代碼是有點長讀通過,但良好的可重複的例子。 – 2013-04-30 11:31:24

+0

您的PDF文件是20x30英寸大,所以默認的字體大小(通常約12分)將使文本無法讀取。你需要這麼大的輸出嗎? – baptiste 2013-04-30 11:48:16

+0

嗨巴蒂斯特,我不需要這麼大的輸出(我只是需要適應圖像的默認頁面),但我需要這兩個情節是方形的,它看起來像調整PDF大小正在幫助...是否有一個簡單的方法來做到這一點,我沒有看到?感謝您的幫助。 – user971102 2013-04-30 12:31:24

回答

4

添加theme()每個情節和設置要更改的元素的大小,例如

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

enter image description here

您可以嘗試使用pushViewport到網格在頁面上安排的事情像這樣手動設置視口,但是您可能需要稍微玩弄一下,才能得到您想要的圖像:

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() 
+0

嗨SimonO101,謝謝你的回覆,但這仍然無法正常工作,grobArrange內的圖像看起來是一樣的... – user971102 2013-04-30 12:29:33

+0

@ user971102我添加了一些代碼,你可以玩周圍推動情節到特定veiewports你可以手動設置大小的。 – 2013-04-30 12:55:01

+0

我從來沒有使用pushViewport,所以我不知道如何爲這兩個情節例如或如果這是可能的,但我會研究這個,如果沒有辦法做grid.arrange ...我想這將是一個簡單的錯誤從我的部分使用grid.arrange ... – user971102 2013-04-30 13:08:41

2

我不知道你期待輸出什麼,但是試試這個,

g = arrangeGrob(
    arrangeGrob(p1 + coord_fixed() + theme(legend.position="none"), 
       p2 + coord_fixed() +theme(legend.position="none"), 
       main = textGrob("The main figure title is also small and hard to align left", x=0, hjust =0, vjust=1, gp = gpar(fontsize = 22)), nrow=1), 
      mylegend, widths=unit.c(unit(1, "npc") - sum(mylegend$width), 
            sum(mylegend$width)), nrow=1) 

ggsave("layout.pdf", g, width=12, height=5) 
+0

是的!我認爲這是奇怪的,這是不可能的與arrangeGrob!比例仍然有些偏差,所以我想最後我會用另一種方法,但是感謝你發佈這個解決方案,我瘋了! – user971102 2013-04-30 17:28:08

+2

'arrangeGrob'只是一個圍繞網格視口的薄包裝,在這裏您的問題最有可能知道如何爲'ggplot2'設置固定長寬比並設置設備大小。 – baptiste 2013-04-30 17:32:33