我正在为出版物生成图表,我希望能够在ggplot本身标注图形的面板(而不是导出到出版商等),以便它们在最终文档中整齐地整合在一起 . 我打算通过在 Headers 中添加一个字母(“A”)来尝试这样做,但我希望我的 Headers 居中,我想要左上角的字母 .
# 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")
现在,如果我愿意通过手工间隔每个 Headers “伪造它”,我可以让它起作用,但这似乎是时间密集和粗糙的 .
# 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")
有没有办法单独调用 Headers 的每个“行”以获得自己的hjust值?
还有其他创意解决方案
另外,我在mtext(Splitting axis labels with expressions)中看到了潜力,但却无法兼容) . 这篇文章各种各样有趣(Multi-line ggplot Title With Different Font Size, Face, etc),但我还是R的新手,我无法弄清楚如何编辑这个聪明的东西来改变 indentation .
谢谢!
5 回答
Update: Since ggplot2 3.0.0 there is now native support for plot labels, see this answer.
以下是我将如何使用我专门为此目的编写的cowplot包 . 请注意,您获得了一个干净的主题作为奖励 .
然后,您希望使用
save_plot
函数来保存绘图而不是ggsave
,因为save_plot
具有默认值和参数,可帮助您获得相对于主题的缩放,特别是对于网格中的绘图 .自
ggplot2 3.0.0
,there is a native way to do this, using the tag label:最简单的方法可能是使用 Headers 和副 Headers ,
你可以通过操纵Grobs来做到这一点 . 在
ggplot2
之上使用gtable
和grid
.您可以使用包
grid
中的grid.text
,在绘图中添加文本 . 首先在 Headers 上方添加一些空格plot.margin
.