如何在ggplot2上绘制文本框标签和数据点之间的连接线?

Rod*_*hew 2 r ggplot2

我想在文本框标签(例如下图中的 Journal H 文本框)和相应的数据点之间绘制连接线,但除了完成它之外,无法找出在 R 上执行此操作的方法在 PowerPoint/Illustrator 上。我的图像看起来像这样我的 R 图

我的输入数据如下所示:

Journal Year Impact_Factor
1  Journal A 2010          1.91
2  Journal B 2010          9.18
3  Journal C 2012          1.65
4  Journal D 2012          2.40
5  Journal E 2012          3.68
6  Journal B 2013          9.18
7  Journal F 2013          0.79
8  Journal G 2014          1.99
9  Journal H 2016         15.54
10 Journal I 2017          3.82
11 Journal H 2017         15.54
12 Journal B 2019          9.18
13 Journal J 2019          6.78
14 Journal K 2019          3.22
15 Journal L 2020          4.26
16 Journal M 2020         11.08
17 Journal N 2021          4.62
Run Code Online (Sandbox Code Playgroud)

下面是我使用的 R 代码:

library(ggplot2)
library(ggthemes)
library(RColorBrewer)
nb.cols <- 16
mycolors <- colorRampPalette(brewer.pal(8, "Set2"))(nb.cols)
df <- read.csv("Test_publications_list.csv", header=TRUE)
ggplot(df, aes(x=Year, y=Impact_Factor, color=Journal)) + 
  geom_point(color= "black", shape= 21, size= 5, stroke= 1, aes(fill = Journal)) + 
  scale_fill_manual(values = mycolors) + 
  theme_gdocs() + 
  geom_label(aes(x = 2017, y = 14, label = "Journal H", fontface= 2), color = "black", fill= NA) + 
  theme(plot.margin = unit(c(1,1,1,1), "cm")) + 
  ggtitle("Summary plot showing my publications\n in academic journals") + 
  theme(plot.title = element_text(color= "black", size = 10, face = "bold"))
Run Code Online (Sandbox Code Playgroud)

如果有人能让我知道在这个 R 图上添加连接线的便捷方法,我将非常感激

r2e*_*ans 5

这里有一个建议,让我们来ggrepel控制推开的距离。

library(ggrepel)
ggplot(df, aes(x=Year, y=Impact_Factor, color=Journal)) + 
  geom_point(color= "black", shape= 21, size= 5, stroke= 1, aes(fill = Journal)) + 
  scale_fill_manual(values = mycolors) + 
  # theme_gdocs() +  # I don't have ggthemes installed
  geom_label_repel(
    aes(label = Journal),
    data = ~ subset(., Journal == "Journal H" & Year == 2017),
    color = "black", fill= NA, box.padding = 1.5
  ) + 
  theme(plot.margin = unit(c(1,1,1,1), "cm")) + 
  ggtitle("Summary plot showing my publications\n in academic journals") + 
  theme(plot.title = element_text(color= "black", size = 10, face = "bold"))
Run Code Online (Sandbox Code Playgroud)

ggplot2 带有标签和线到点

我从强制位置(在代码中)转向允许它使用原始数据,并将标签移离该点。我使用的data = ~ subset( )rlang-style tilde-function 和 base R's subset,子.集中的调用是当时有效的数据。dplyr::filter如果您已经加载并且喜欢它,也可以使用dplyr。如果您愿意,也可以data=df[...]直接指定,尽管我经常在第一次调用 之前找到一些 dplyr-pipe ggplot(.),在这种情况下,原始df数据可能不是数据看起来的样子ggplot2。使用data=~subset(...)使之变得透明/一致。

注意:ggrepel使用随机过程来优化排斥文本/标签。这意味着如果您有多个标签,它们可能会在图之间发生变化。您可以使用许多“控件”,例如某些鼓励方向(左/右)的功能。

我认为使用的力量ggrepel在于你不再需要考虑把东西放在哪里。如果没有它,您所需要的只是略有不同的数据,并且您的硬编码标签位置可能都需要更改。


另一种方法是在点和一个标签之间绘制多条线:

我们首先为标签生成一个框架:

lbls <- data.frame(Journal = "Journal H", xend = 2017, yend = 14)
merge(lbls, df, by = "Journal")
#     Journal xend yend Year Impact_Factor
# 1 Journal H 2017   14 2017         15.54
# 2 Journal H 2017   14 2016         15.54
Run Code Online (Sandbox Code Playgroud)

从这里开始,我们使用geom_segment和调整后的geom_label

ggplot(df, aes(x=Year, y=Impact_Factor, color=Journal)) + 
  geom_point(color= "black", shape= 21, size= 5, stroke= 1, aes(fill = Journal)) + 
  scale_fill_manual(values = mycolors) +
  # theme_gdocs() + 
  geom_segment(aes(xend = xend, yend = yend),
               data = merge(lbls, df, by = "Journal")) +
  geom_label(aes(x = xend, y = yend, label = Journal),
             data = lbls, color = "black", fontface = 2, fill = NA) +
  theme(plot.margin = unit(c(1,1,1,1), "cm")) + 
  ggtitle("Summary plot showing my publications\n in academic journals") + 
  theme(plot.title = element_text(color= "black", size = 10, face = "bold"))
Run Code Online (Sandbox Code Playgroud)

ggplot 有两条线段,但位置错误

如您所见,根据标签框的默认偏移量,线条的位置位于标签中心。我们可以对此进行调整。另外,我们需要删除一个额外的图例,因此我们将添加一个scale_*.

lbls2 <- data.frame(Journal = "Journal H", xend = 2017, yend = 14, hjust = 0, vjust = 1)
ggplot(df, aes(x=Year, y=Impact_Factor, color=Journal)) + 
  geom_point(color= "black", shape= 21, size= 5, stroke= 1, aes(fill = Journal)) + 
  scale_fill_manual(values = mycolors) + 
  # theme_gdocs() + 
  geom_segment(aes(xend = xend, yend = yend),
               data = merge(lbls, df, by = "Journal")) +
  scale_color_discrete(guide = FALSE) +
  geom_label(aes(x = xend, y = yend, label = Journal, hjust = hjust, vjust = vjust),
             data = lbls2, color = "black", fontface = 2, fill = NA) +
  theme(plot.margin = unit(c(1,1,1,1), "cm")) + 
  ggtitle("Summary plot showing my publications\n in academic journals") + 
  theme(plot.title = element_text(color= "black", size = 10, face = "bold"))
Run Code Online (Sandbox Code Playgroud)

现在给我们

ggplot,行移动,删除额外的图例

您可能需要进行更多调整。我希望您考虑这种方法的最大收获:

  • x=而不是像您那样硬编码y=,使用框架,它更具可扩展性;
  • geom_segment让我们添加从点“A”到点“B”的单独线段(在这种情况下,对于特定期刊,“B”是不变的,但这只是为了方便);
  • 使用框架概念,可以轻松地与原始数据合并,以便填充每个段的x=y=(绑定到我们用于标签放置的xend=和;以及yend=
  • 任何其他按期刊定制的内容都可以在lbls框架中解决。