ggplot2多行标题,不同的缩进 [英] ggplot2 multiline title, different indentations

查看:92
本文介绍了ggplot2多行标题,不同的缩进的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为出版物生成图形,我希望能够在ggplot本身中标记图形的面板(而不是导出到发布者等),以便它们可以整齐地放置在最终文档中.我试图通过在标题中添加字母("A")来实现此目的,但我希望标题居中,并且希望在左上角添加字母.

I am generating graphs for a publication and I'd like to be able to label the panels of a figure in ggplot itself (as opposed to exporting to publisher, etc) so that they just fit neatly together in the final document. I was going to try to do this by adding a letter ("A") into the title, but I'd like my titles centered and I'd like the letter in the upper lefthand corner.

# 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")

现在,如果我愿意通过手动间隔每个标题来伪造"它,我可以使其正常工作,但这似乎很耗时且很粗糙.

Now, if I'm willing to just "fake it" by hand-spacing each title, I can sort of get it to work, but this seems time intensive and crude.

# 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")

有没有一种方法可以单独调用标题的每个行"以获取其自身的正值?

Is there a way to call each 'line' of the title individually for an hjust value of its own?

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

Any other creative solutions?

此外,我看到了mtext的潜力(用表达式分割轴标签),但无法弄清楚如何使用ggplot2(vs基本绘图功能..好像它们不兼容)来实现它. 这篇文章各种各样有趣(多行ggplot标题(具有不同的字体大小,字体等),但是我对R还是陌生的,我不知道如何编辑这个聪明的东西来更改缩进.

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.

谢谢!

推荐答案

更新:自ggplot2 3.0.0以来,现在对绘图标签提供了本机支持,

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

在这里,我将使用我为此专门编写的Cowplot包来做到这一点.请注意,您将获得一个干净的主题作为奖励.

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

You then want to use the save_plot function to save plots instead of ggsave, because save_plot has defaults and parameters that help you get the scaling right relative to the theme, in particular for plots in a grid.

这篇关于ggplot2多行标题,不同的缩进的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆