Consistently center ggplot title across PANEL not PLOT
默认对齐方式是使ggplot标题与plot.background元素左对齐。其他人注意到,您可以使用plot.title = element_text(hjust = 0.5)将标题居中。
-
如何居中ggplot情节标题
-
ggplot2中的中心图标题
但是,我想使标题在整个面板中居中,而不是只针对情节。我过去通过修改hjust来推动标题以使其居中显示来完成此操作,但是hjust的值取决于标题的长度,这使得在我批量处理时进行设置非常繁琐产生图。
是否可以一致地将ggplot的标题元素设置为在panel.background中居中?
1 2 3 4 5 6 7 8 9 10 11
| library(reprex)
library(tidyverse)
data(mtcars)
mtcars %>%
rownames_to_column(var ="model") %>%
top_n(8,wt) %>%
ggplot(aes(x =model, y = wt))+
geom_col()+
coord_flip()+
labs(title="This title is left-aligned to the plot") |

1 2 3 4 5 6 7 8
| mtcars %>%
rownames_to_column(var ="model") %>%
top_n(8,wt) %>%
ggplot(aes(x =model, y = wt))+
geom_col()+
coord_flip()+
labs(title="This title is center-aligned to the plot width.")+
theme(plot.title = element_text(hjust = 0.5)) |

1 2 3 4 5 6 7 8
| mtcars %>%
rownames_to_column(var ="model") %>%
top_n(8,wt) %>%
ggplot(aes(x =model, y = wt))+
geom_col()+
coord_flip()+
labs(title="Short title, still works")+
theme(plot.title = element_text(hjust = 0.5)) |

1 2 3 4 5 6 7 8
| mtcars %>%
rownames_to_column(var ="model") %>%
top_n(8,wt) %>%
ggplot(aes(x =model, y = wt))+
geom_col()+
coord_flip()+
labs(title="This title is roughly center-aligned to panel")+
theme(plot.title = element_text(hjust = 0.37)) # I know I can adjust this, but it would require a manual fix each time |

1 2 3 4 5 6 7 8 9 10
| p <- mtcars %>%
rownames_to_column(var ="model") %>%
top_n(8,wt) %>%
ggplot(aes(x =model, y = wt))+
geom_col()+
coord_flip()+
#labs(title="Short title, still works")+
theme(plot.title = element_text(hjust = 0.5))
gridExtra::grid.arrange( top=grid::textGrob("This title is center-aligned to panel"),p ) |

- 一种方法是用图的标题包住textGrob。这是使用grid.arrange为您完成工作的方法gridExtra::grid.arrange( top=grid::textGrob("This title is roughly center-aligned to panel"), p )
-
@ user20650,这看起来不错,我之前看过Grid.arrange,但不是这样。如果我为字幕堆叠另一个textGrob,是否有一个参数可以让我摆脱填充?如果我使用您的gridExtra::grid.arrange(top=grid::textGrob("This title is roughly center-aligned to panel"), grid::textGrob("subtitle"), p ),则文本框肯定在整个面板上居中,但垂直间距很大。有什么办法可以减少这种情况?理想情况下,我想将标题和副标题文本分开,以便为它们设置不同的样式。
-
grid.arrange( top=grobTree(textGrob("This title"), textGrob("\
\
This subtitle", gp=gpar(cex=0.8))), p, padding=unit(2,"line") ):不确定标题字体大小等增加时的鲁棒性。
-
请参阅stackoverflow.com/questions/31630020/…
-
@ user20650,谢谢!现在,我想我会同意你的建议。由于标题和字幕字体大小在所有图形中均已标准化,因此实际上效果很好,因此一旦我确定要在字幕文本中放入正确数量的新行,就无需更改。我将用您的代码更新我的问题。
-
这回答了你的问题了吗?如何居中ggplot情节标题
一种方法是更改??标题组的位置,使其从左边缘开始:
1 2 3 4 5 6 7 8 9 10 11 12 13
| p <- mtcars %>%
rownames_to_column(var ="model") %>%
top_n(8,wt) %>%
ggplot(aes(x =model, y = wt))+
geom_col()+
coord_flip()+
labs(title="This title is left-aligned to the plot") +
theme(plot.title = element_text(hjust = 0.5))
library(gridExtra)
g <- ggplotGrob(p)
g$layout$l[g$layout$name =="title"] <- 1
grid::grid.draw(g) |
- 我喜欢此解决方案,因为它允许我继续使用labs参数,但是我担心另存为png吗?创建图形后,我的工作流程是使用ggsave保存为png。似乎使用ggplotGrob不是ggsave接受的接受对象之一吗?
-
ggsave("test.png")对我有用,因为它默认保存显示的最后一个绘图。也许是ggsave("test.png", arrangeGrob(g)),根据此处的讨论stackoverflow.com/questions/17059099/…。
另一个选择是使用patchwork库,它是plot_annotation,因此您将拥有:
1 2 3 4 5 6 7 8
| mtcars %>%
rownames_to_column(var ="model") %>%
top_n(8,wt) %>%
ggplot(aes(x =model, y = wt))+
geom_col()+
coord_flip()+
patchwork::plot_annotation(title="This title is very long and centered on the whole plot",
theme = theme(plot.title = element_text(hjust = 0.5))) |