繁体   English   中英

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

[英]How to draw connecting line(s) between text box label and data point on ggplot2?

我想在文本框 label (例如下图中的 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

以下是我使用的 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"))

如果有人能告诉我一种在此 R plot 上添加连接线的便捷方法,我将不胜感激

这是一个建议,让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"))

带有标签和线到点的ggplot2

我从强制位置(在您的代码中)转移到允许它使用原始数据,并将 label 移离该点。 我对data = ~ subset( )的使用使用了rlang风格的波浪号函数和基础 R 的subset ,即. 子集调用中是当时有效的数据。 如果您已经加载dplyr并且更喜欢它,您也可以使用dplyr::filter 如果您愿意,您也可以直接指定data=df[...] ,尽管我经常在第一次调用ggplot(.)之前找到一些 dplyr-pipe ,在这种情况下,原始df可能不是数据的样子ggplot2 使用data=~subset(...)使其透明/一致。

注意: ggrepel使用随机过程来优化排斥文本/标签。 这意味着如果您有多个标签,它们可能会从一个情节转移到另一个情节。 您可以使用许多“控件”,例如一些鼓励方向(左/右)的能力。

我认为使用ggrepel的强大之处在于您不再需要考虑将东西放在哪里。 没有它,您所需要的只是略有不同的数据,您的硬编码 label 位置可能都需要更改。


或者,在点和一个 label 之间绘制多条线:

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

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

从这里开始,我们使用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"))

ggplot 有两条线段,但放错了位置

如您所见,线的放置是基于 label 框的默认偏移量,以 label 为中心。 我们可以调整它。 此外,我们需要删除一个额外的图例,因此我们将添加一个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"))

现在给了我们

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

你可能需要更多地调整一些东西。 我希望您使用这种方法考虑的最大收获:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM