7

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

更好的间距但草率的代码

有没有办法单独调用标题的每一“行”以获得它自己的值?

还有其他创造性的解决方案吗?

此外,我在 mtext 中看到了潜力(使用表达式拆分轴标签),但无法弄清楚如何使用 ggplot2 来实现它(与基本绘图功能相比.. 似乎它们不兼容)。这篇文章很有趣(Multi-line ggplot Title With different Font Size, Face, etc),但我还是 R 新手,我不知道如何编辑这个聪明的东西来改变缩进

谢谢!

4

5 回答 5

14

更新:由于 ggplot2 3.0.0 现在有对绘图标签的本机支持,请参阅此答案。

下面是我的做法,使用我专门为此目的编写的 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具有默认值和参数,可以帮助您获得相对于主题的正确缩放,特别是对于网格中的绘图。

于 2017-11-28T14:17:44.853 回答
10

因为ggplot2 3.0.0有一种本地方法可以做到这一点,使用tag标签

ggplot(iris, aes(x = Sepal.Length, y = Sepal.Width, color = Species, shape = Species)) +
  geom_point() +
  labs(title = "The Actual Long, Normal Title of Titliness",
       tag = "A")

ggplot_tag

于 2018-07-23T13:58:31.967 回答
3

最简单的方法可能是使用标题和副标题,

ggplot(iris, aes(x = Sepal.Length, y = Sepal.Width, color = Species, shape = Species))+
  geom_jitter(size = 6.5) +
  labs(title = "A", subtitle = "The Actual Long, Normal Title of Titliness") +
  theme(plot.subtitle = element_text(hjust = 0.5))
于 2017-11-28T04:11:22.133 回答
2

您可以通过操纵 Grobs 来做到这一点。在. gtable_ grid_ggplot2

library("ggplot2")
library("gtable")
library("grid")

p <- ggplot(iris, aes(x = Sepal.Length, y = Sepal.Width, color = Species, shape = Species))+
  geom_jitter(size = 6.5)+
  ggtitle("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",
        plot.margin = unit(c(4, 1, 1, 4), "lines"))  # We need bigger top/left margins to show the letter

gt <- ggplot_gtable(ggplot_build(p))  # Building a gtable from the ggplot object
# Adding the textGrob for the panel letter
panel_letter = textGrob("A", x = 0.5, y = .9, 
                        just = c("left", "top"), 
                        gp = gpar(fontsize = 40, col =  "black", face="bold"))
gt <- gtable_add_grob(gt, panel_letter, t=1, l=1, r=1)  # Append the Grob to the table

p <- grid.draw(gt)  # Display the plot
ggsave("plot.png",gt, width=10, height=10)  # If you want to save it.

我的尝试

于 2017-11-28T04:34:08.390 回答
2

您可以使用grid.textfrom package grid,将文本添加到您的情节中。首先在您的标题 use 上方添加一些空间plot.margin

library(grid)
p <- ggplot(iris, aes(x = Sepal.Length, y = Sepal.Width, color = Species, shape = Species))+
geom_jitter(size = 6.5)+
ggtitle("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", 
      plot.margin = unit(c(4, 3, 1, 1), "lines"))
p
grid.text('A', x = unit(0.1, 'npc'), y = unit(.90, 'npc'), gp = gpar(fontsize=28))

在此处输入图像描述

于 2017-11-28T04:01:05.490 回答