我有一个用例相似的地方,我创建多个地块,并使用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'之前,用'theme'修改绘图边距怎么样? – mikeck
我知道,但这还不够,看到这个问题的答案有问题出现在哪里:https://stackoverflow.com/questions/45817032/how-to-fit-custom-long-annotations-geom-text-inside -plot-area-for-a-donuts-plot我已经尝试了所有我能想到的方法来摆脱垂直空间,但目前为止没有运气。 –
我认为,也许gridextra有一些负面的边缘,以便在布局中放置某个区域时,或者为此操作视口。 –