gpt4 book ai didi

r - 做 arrangeGrob 时是否可以裁剪图?

转载 作者:行者123 更新时间:2023-12-04 03:10:15 25 4
gpt4 key购买 nike

我有一个类似于下面的用例,我创建了多个图并使用 gridExtra 将它们排列到一些页面布局中,最后使用 ggsave 将其保存为 PDF :

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

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

是否可以在使用 arrangeGrob 将绘图 p2 安排到页面布局中时对其进行裁剪?问题是 p2 在它的顶部和底部有很多额外的空间,我想摆脱它,因为 cropping 似乎不可行使用ggplot2 只是我在想也许可以在安排它的时候裁剪它......

更新这是我的用例的一个独立示例,我想通过任何方式摆脱红色注释的区域:

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

最佳答案

为此,需要一个多部分解决方案。

首先,要更改单个图形中的边距,在 theme() 中使用 plot.margin 是一种方便的方法。但是,如果目标是合并多个地 block ,仅此一项并不能解决问题。

为此,您需要结合使用 plot.margin 和 arrangeGrob() 中的特定绘图顺序。您需要一个特定的顺序,因为图是按照您调用它们的顺序打印的,因此,更改位于其他图后面而不是图前面的图的边距会更容易。我们可以把它想象成通过在我们想要缩小的图上扩展图来覆盖我们想要缩小的图边距。请参见下图:

在 plot.margin 设置之前:

enter image description here

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

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))

结合 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)

请注意,在最后的代码中,我将 arrangeGrob 中的顺序从 p1、p2 反转为 p2、p1。然后我调整了绘制的第一个对象的高度,这是我们想要缩小的对象。此调整允许较早的 plot.margin 调整生效,并且随着该调整生效,按顺序最后打印的图形 P1 将开始占用 P2 的边距空间。如果您只进行其中一项调整而不进行其他调整,则解决方案将不起作用。这些步骤中的每一个对于产生上述最终结果都很重要。

关于r - 做 arrangeGrob 时是否可以裁剪图?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45904230/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com