2017-08-27 53 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 –