gpt4 book ai didi

r - ggplot2:如何裁剪出绘图顶部和底部的空白区域?

转载 作者:行者123 更新时间:2023-12-02 03:16:45 27 4
gpt4 key购买 nike

这是问题 How to fit custom long annotations geom_text inside plot area for a Donuts plot? 的后续问题。查看已接受的答案,可以理解,生成的图在顶部和底部有额外的空白区域。我怎样才能摆脱那些多余的空白区域?我查看了 theme aspect.ratio 但这不是我想要的,尽管它完成了工作但扭曲了情节。我正在将地 block 从正方形裁剪为横向形式。

我怎样才能做到这一点?

更新这是我的用例的一个独立示例:

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

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)

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

这就是我想找到答案来修复这些带注释的空白空格的结果:

Donuts plot

最佳答案

这是一个由多部分组成的解决方案来回答这个问题和 the other related question您已发布。

首先,对于更改单个图表中的边距,@Keith_H 的做法是正确的;在 theme() 内使用plot.margin是一种方便的方法。但是,如前所述,如果目标是组合多个图(如上面链接的其他问题的情况),仅此并不能解决问题。

为此,您需要结合使用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)

请注意,在最终代码中,我颠倒了排列Grob 中的顺序,从 p1,p2 到 p2,p1。然后我调整了绘制的第一个对象的高度,这就是我们想要缩小的对象。此调整允许之前的plot.margin调整生效,并且当该调整生效时,按顺序最后打印的图形(即P1)将开始占据P2的边距空间。如果您仅进行其中一项调整而不进行其他调整,则该解决方案将不起作用。每个步骤对于产生上述最终结果都很重要。

关于r - ggplot2:如何裁剪出绘图顶部和底部的空白区域?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45823595/

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