我正在为出版物生成图表,我希望能够在 ggplot 本身中标记图形的面板(而不是导出到出版商等),以便它们在最终文档中整齐地组合在一起。我打算尝试通过在标题中添加一个字母(“A”)来实现这一点,但我希望我的标题居中并且我希望字母位于左上角。# base graph:
ggplot(iris, aes(x = Sepal.Length, y = Sepal.Width, color = Species, shape = Species))+
geom_jitter(size = 6.5)+
ggtitle("A \n \n The Actual Long, Normal Title of Titliness")+
theme(plot.title = element_text(hjust = 0.5, face = "bold", size = 30),
axis.ticks = element_blank(),
legend.text = element_text(size = 25),
axis.title = element_text(size = 25, face = "bold"),
axis.text = element_text(size = 25, vjust = 0.05),
legend.position = "bottom")
现在,如果我愿意通过手动间隔每个标题来“伪造”它,我可以让它工作,但这似乎是时间密集和粗糙的。
# sloppy solution
ggplot(iris, aes(x = Sepal.Length, y = Sepal.Width, color = Species, shape = Species))+
geom_jitter(size = 6.5)+
ggtitle("A \n \n The Actual Long, Normal Title of Titliness")+
theme(plot.title = element_text(hjust = 0,face = "bold", size = 30),
axis.ticks = element_blank(),
legend.text = element_text(size = 25),
axis.title = element_text(size = 25, face = "bold"),
axis.text = element_text(size = 25, vjust = 0.05),
legend.position = "bottom")
有没有办法单独调用标题的每一“行”以获得它自己的 hjust 值?
还有其他创造性的解决方案吗?
此外,我看到了 mtext ( Splitting axis labels with expressions ) 的潜力,但无法弄清楚如何使用 ggplot2 (vs base plot function.. 似乎它们不兼容) 实现它。
这篇文章很有趣(Multi-line ggplot Title With Different Font Size, Face, etc),但我还是 R 的新手,我不知道如何编辑这个聪明的东西来改变 压痕 .
谢谢!
最佳答案
更新:从 ggplot2 3.0.0 开始,现在原生支持绘图标签,see this answer.
这是我将如何做到这一点,使用我专门为此目的编写的 cowplot 包。请注意,您会获得一个干净的主题作为奖励。
library(cowplot)
p <- ggplot(iris, aes(x = Sepal.Length, y = Sepal.Width, color = Species, shape = Species)) +
geom_jitter(size = 3.5) +
ggtitle("The Actual Long, Normal Title of Titliness")
# add one label to one plot
ggdraw(p) + draw_plot_label("A")
# place multiple plots into a grid, with labels
plot_grid(p, p, p, p, labels = "AUTO")
然后您想使用
save_plot
函数来保存绘图而不是 ggsave
, 因为 save_plot
具有默认值和参数,可帮助您获得相对于主题的缩放比例,特别是对于网格中的绘图。
关于r - ggplot2 多行标题,不同的缩进,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47523389/