ggplot2多行标题,不同的缩进

猫造成错别字

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

更好的间距但草率的代码

有没有一种方法可以分别调用标题的每个“行”以获取其自身的正当值?

还有其他创意解决方案吗?

Also, I saw potential in mtext (Splitting axis labels with expressions), but couldn't figure out how to implement it with ggplot2 (vs base plot function.. it seems like they're not compatible). This post was all sorts of interesting (Multi-line ggplot Title With Different Font Size, Face, etc), but I am still new to R and I couldn't figure out how to edit this clever stuff to change indentation.

Thanks!

Claus Wilke

Update: Since ggplot2 3.0.0 there is now native support for plot labels, see this answer.

Here is how I would do it, using the cowplot package that I wrote specifically for this purpose. Note that you get a clean theme as a bonus.

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具有默认值和参数,可以帮助您相对于主题正确地缩放比例,特别是对于网格图中的图。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章