这是问题的后续工作,如何在Donuts图的图区域内适合自定义长注释geom_text?。看到已接受的答案,可以理解,生成的图在顶部和底部都有额外的空白区域。我如何摆脱那些多余的空白区域?我看了一下,theme aspect.ratio但这不是我想要的,尽管它可以完成工作但会使情节变形。我将地块从正方形裁剪为景观形式之后。
我怎样才能做到这一点?
更新这是我的用例的一个独立示例:
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()
Run Code Online (Sandbox Code Playgroud)
这就是我想要找到解决这些带注释的结果空白的答案的结果:
这是一个多部分的解决方案,可以回答此问题以及您发布的其他相关问题。
首先,为了更改单个图形中的边距,@ Keith_H位于正确的轨道上;在theme()中使用plot.margin是一种方便的方法。但是,如前所述,如果目标是合并多个绘图,那么仅此方法并不能解决问题,就像上面链接的其他问题一样。
要做到这一点,您需要将plot.margin和一个特定的绘图顺序组合在一起,并放置在rangingGrob()中。您将需要一个特定的顺序,因为以按照您所谓的顺序打印地块,因此更改在其他地块后面而不是在地块前面的地块边距会更容易。我们可以认为它就像在要缩小的地块之上扩展地块来覆盖我们要缩小的地块边距。请参见以下图表以获取插图:
在plot.margin设置之前:
#Main code for the 1st graph can be found in the original question.
Run Code Online (Sandbox Code Playgroud)
在plot.margin设置之后:
#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))
Run Code Online (Sandbox Code Playgroud)
结合plot.margin设置和rangingGrob()重新排序后:
#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)
Run Code Online (Sandbox Code Playgroud)
请注意,在最后的代码中,我将您在rangingGrob中的顺序从p1,p2反转为p2,p1。然后,我调整了绘制的第一个对象的高度,这是我们要缩小的对象。此调整允许较早的plot.margin调整生效,并且该调整生效后,最后按顺序打印的图形P1将开始占用P2的空白空间。如果您不进行其他任何一种调整,则该解决方案将无法工作。这些步骤中的每一个对于产生上述最终结果都很重要。
| 归档时间: |
|
| 查看次数: |
1065 次 |
| 最近记录: |