2017-08-27 54 views
2

我有一個用例相似的地方,我創建多個地塊,並使用gridExtra到最後它保存爲ggsave一個PDF安排他們到一些頁面佈局如下:在做arrangeGrob時可以裁剪一塊地塊嗎?

p1 <- generate_ggplot1(...) 
p2 <- generate_ggplot2(...) 

final <- gridExtra::arrangeGrob(p1, p2, ...) 
ggplot2::ggsave(filename=output.file,plot=final, ...) 

是否有可能有陰謀p2在與arrangeGrob排列在頁面佈局時裁剪?問題是,p2有很多額外的空間,我想擺脫它的頂部和底部,因爲cropping似乎不可行使用ggplot2只有我想也許有可能在裁剪時裁剪它...

UPDATE這裏是我的用例的自成一體的例子,我想擺脫以任何標註的紅色區域表示:

library(ggplot2); library(dplyr); library(stringr); library(gridExtra) 

df <- data.frame(group = c("Cars", "Trucks", "Motorbikes"),n = c(25, 25, 50), 
       label2=c("Cars are blah blah blah", "Trucks some of the best in town", "Motorbikes are great if you ...")) 
df$ymax = cumsum(df$n) 
df$ymin = cumsum(df$n)-df$n 
df$ypos = df$ymin+df$n/2 
df$hjust = c(0,0,1) 

p1 <- ggplot(mtcars,aes(x=1:nrow(mtcars),y=mpg)) + geom_point() 

p2 <- ggplot(df %>% 
     mutate(label2 = str_wrap(label2, width = 10)), #change width to adjust width of annotations 
     aes(x="", y=n, fill=group)) + 
    geom_rect(aes_string(ymax="ymax", ymin="ymin", xmax="2.5", xmin="2.0")) + 
    expand_limits(x = c(2, 4)) + #change x-axis range limits here 
    # no change to theme 
    theme(axis.title=element_blank(),axis.text=element_blank(), 
     panel.background = element_rect(fill = "white", colour = "grey50"), 
     panel.grid=element_blank(), 
     axis.ticks.length=unit(0,"cm"),axis.ticks.margin=unit(0,"cm"), 
     legend.position="none",panel.spacing=unit(0,"lines"), 
     plot.margin=unit(c(0,0,0,0),"lines"),complete=TRUE) + 
    geom_text(aes_string(label="label2",x="3",y="ypos",hjust="hjust")) + 
    coord_polar("y", start=0) + 
    scale_x_discrete() 

final <- arrangeGrob(p1,p2,layout_matrix = rbind(c(1),c(2)), 
        widths=c(4),heights=c(4,4), padding=0.0, 
        respect=TRUE, clip="on") 
plot(final) 

,輸出爲:

arrangeGrob output

+0

在安排'arrangeGrob'之前,用'theme'修改繪圖邊距怎麼樣? – mikeck

+0

我知道,但這還不夠,看到這個問題的答案有問題出現在哪裏:https://stackoverflow.com/questions/45817032/how-to-fit-custom-long-annotations-geom-text-inside -plot-area-for-a-donuts-plot我已經嘗試了所有我能想到的方法來擺脫垂直空間,但目前爲止沒有運氣。 –

+0

我認爲,也許gridextra有一些負面的邊緣,以便在佈局中放置某個區域時,或者爲此操作視口。 –

回答

1

爲此,需要多部分解決方案。

首先,爲了改變單個圖形中的邊距,在theme()中使用plot.margin是一種方便的方法。然而,如果目標是將多個地塊組合起來,這本身並不能解決問題。

要做到這一點,您需要結合plot.margin和arrangeGrob()中的特定繪圖順序。你需要一個特定的訂單,因爲地塊是按照你所說的順序打印的,因此,更改分散在其他地塊後面的地塊的邊緣將更容易,而不是在地塊前面。我們可以將它想象爲覆蓋我們想要縮小的地塊邊際,方法是將該地塊擴大到我們想要縮小的地方。請參閱下面的圖表來加以說明:

之前plot.margin設置:

enter image description here

#Main code for 2nd graph: 
ggplot(df %>% 
      mutate(label2 = str_wrap(label2, width = 10)), 
        aes(x="", y=n, fill=group)) + 
    geom_rect(aes_string(ymax="ymax", ymin="ymin", xmax="2.5", xmin="2.0")) + 
    geom_text(aes_string(label="label2",x="3",y="ypos",hjust="hjust")) + 
    coord_polar(theta='y') + 
    expand_limits(x = c(2, 4)) + 
    guides(fill=guide_legend(override.aes=list(colour=NA))) + 
    theme(axis.line = element_blank(), 
     axis.ticks=element_blank(), 
     axis.title=element_blank(), 
     axis.text.y=element_blank(), 
     axis.text.x=element_blank(), 
     panel.border = element_blank(), 
     panel.grid.major = element_blank(), 
     panel.grid.minor = element_blank(), 
     panel.background = element_rect(fill = "white"), 
     plot.margin = unit(c(-2, 0, -2, -.1), "cm"), 
     legend.position = "none") + 
scale_x_discrete(limits=c(0, 1)) 

enter image description here

#Main code for the 1st graph can be found in the original question. 

plot.margin設置後

結合plot.margin設置和arrangeGrob()重新排序後:

enter image description here

#Main code for 3rd graph: 
p1 <- ggplot(mtcars,aes(x=1:nrow(mtcars),y=mpg)) + geom_point() 

p2 <- ggplot(df %>% 
       mutate(label2 = str_wrap(label2, width = 10)), #change width to adjust width of annotations 
         aes(x="", y=n, fill=group)) + 
     geom_rect(aes_string(ymax="ymax", ymin="ymin", xmax="2.5", xmin="2.0")) + 
     geom_text(aes_string(label="label2",x="3",y="ypos",hjust="hjust")) + 
     coord_polar(theta='y') + 
     expand_limits(x = c(2, 4)) + #change x-axis range limits here 
     guides(fill=guide_legend(override.aes=list(colour=NA))) + 
     theme(axis.line = element_blank(), 
       axis.ticks=element_blank(), 
       axis.title=element_blank(), 
       axis.text.y=element_blank(), 
       axis.text.x=element_blank(), 
       panel.border = element_blank(), 
       panel.grid.major = element_blank(), 
       panel.grid.minor = element_blank(), 
       panel.background = element_rect(fill = "white"), 
       plot.margin = unit(c(-2, 0, -2, -.1), "cm"), 
       legend.position = "none") + 
     scale_x_discrete(limits=c(0, 1)) 

final <- arrangeGrob(p2,p1,layout_matrix = rbind(c(1),c(2)), 
        widths=c(4),heights=c(2.5,4),respect=TRUE) 

注意的是,在最後的代碼,我翻轉你從P1的arrangeGrob有秩序,P2到P2,P1。然後我調整了繪製的第一個對象的高度,這是我們想縮小的那個。此調整允許較早的plot.margin調整生效,並且在生效時,最後按順序打印的圖表即P1,將開始佔用P2的邊距的空間。如果您將其中一項調整與其他調整一起進行,則解決方案將無法工作。上述每個步驟對產生最終結果都很重要。

+0

優秀答案謝謝! –

+0

@GiovanniAzua - 不客氣。樂意效勞! – www

+0

我怕我得弄髒我的手,叉子'gridExtra'和家人:D –