r - ggplot2 多行标题,不同的缩进

标签 r ggplot2 indentation figures

我正在为出版物生成图表,我希望能够在 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")

oh noes! both of them are centered over the graph

现在,如果我愿意通过手动间隔每个标题来“伪造”它,我可以让它工作,但这似乎是时间密集和粗糙的。
# 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")

better spacing but sloppy code

有没有办法单独调用标题的每一“行”以获得它自己的 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")

enter image description here
# place multiple plots into a grid, with labels
plot_grid(p, p, p, p, labels = "AUTO")

enter image description here

然后您想使用 save_plot函数来保存绘图而不是 ggsave , 因为 save_plot具有默认值和参数,可帮助您获得相对于主题的缩放比例,特别是对于网格中的绘图。

关于r - ggplot2 多行标题,不同的缩进,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47523389/

相关文章:

r - 直方图条件填充颜色

r - 在绘图标题中混合不同的字体大小/面孔

r - ggplot 条形图中的中心文本层

有效的 Clojure 压头

r - R 的 ccf 和 Julia 的 crosscor 中使用的方程是什么?

r - 根据子组中的一行生成子组的标识符

java - 根据日志级别缩进 log4j 消息

php - 如何在 Emacs 中使 switch 语句中的 case 缩进

R 创建 2*2 混淆矩阵的通用解决方案

r - 使用ggplot时无法在R中标记多面板图形