gpt4 book ai didi

r - 在 R 中使用 geom_label_repel 将标签放在前面,将引线放在后面

转载 作者:行者123 更新时间:2023-12-03 16:47:36 25 4
gpt4 key购买 nike

这里是新的,对 R 来说是半新的,所以希望我能正确地提出这个问题。
我有看起来像这样的数据(最后是完整的数据集):

Peak     Length    Height    Class   Label
Peak 16 16.542 1128.60 Class 2 Peak 16
Peak 10 16.592 74909.84 Class 4 Peak 10
Peak 6 16.930 21747.10 Class 2 Peak 6
Peak 9 17.177 98380.79 Class 2 Peak 9
Peak 20 17.235 168036.36 Class 4 Peak 20
Peak 11 17.264 85770.79 Class 5 Peak 11
我正在使用 geom_label_repel 制作带有标签的情节,代码如下:
library(ggplot)
library(ggrepel)

ggplot(data = df, aes(x = Length, y = Height, color = Class)) +
geom_col()+
geom_label_repel(aes(label = Label),
fill = "white",
color = "black",
nudge_x = 0.2,
force= 2,
segment.alpha = 0.5)
结果图很好,但我试图找出一种方法来确保峰值标签最终出现在任何引导线之前。
enter image description here
也许您可以看到“Peak 17”标签与“Peak 3”的引导线重叠。我已经使用了 nudge_x、nudge_y 和 force 来移动标签,但我的完整数据集有更多的峰值和标签,所以我最终总是得到标签上的领导者。
任何帮助将不胜感激,很高兴我找到了这个网站!
完整数据:
df <- structure(list(Peak = structure(c(8L, 2L, 18L, 21L, 13L, 3L, 
1L, 4L, 14L, 6L, 17L, 16L, 12L, 5L, 7L, 10L, 9L, 15L, 20L, 5L,
11L, 19L), .Label = c("Peak 1", "Peak 10", "Peak 11", "Peak 12",
"Peak 13", "Peak 14", "Peak 15", "Peak 16", "Peak 17", "Peak 18",
"Peak 19", "Peak 2", "Peak 20", "Peak 21", "Peak 3", "Peak 4",
"Peak 5", "Peak 6", "Peak 7", "Peak 8", "Peak 9"), class = "factor"),
Length = c(16.542, 16.592, 16.93, 17.177, 17.235, 17.264,
12.065, 12.33, 12.478, 13.844, 13.848, 14.565, 14.836, 14.903,
15.425, 15.789, 16.542, 16.592, 16.93, 17.177, 17.235, 17.264
), Height = c(1128.6, 74909.84, 21747.1, 98380.79, 168036.36,
85770.79, 77156.06, 204716.44, 146063.31, 126574.23, 387495.94,
407584.38, 103778.66, 134615.45, 166271.98, 35626.38, 8076.28,
14417.26, 6311.36, 1578.34, 408.42, 2920.28), Class = structure(c(2L,
4L, 2L, 2L, 4L, 5L, 1L, 3L, 5L, 5L, 2L, 2L, 1L, 5L, 3L, 5L,
5L, 1L, 3L, 5L, 3L, 3L), .Label = c("Class 1", "Class 2",
"Class 3", "Class 4", "Class 5"), class = "factor"), Label = structure(c(8L,
2L, 18L, 21L, 13L, 3L, 1L, 4L, 14L, 6L, 17L, 16L, 12L, 5L,
7L, 10L, 9L, 15L, 20L, 5L, 11L, 19L), .Label = c("Peak 1",
"Peak 10", "Peak 11", "Peak 12", "Peak 13", "Peak 14", "Peak 15",
"Peak 16", "Peak 17", "Peak 18", "Peak 19", "Peak 2", "Peak 20",
"Peak 21", "Peak 3", "Peak 4", "Peak 5", "Peak 6", "Peak 7",
"Peak 8", "Peak 9"), class = "factor")), class = "data.frame", row.names = c(NA,
-22L))

最佳答案

看看这对你有用吗?

ggplot(data = df,
aes(x = Length, y = Height, label = Label)) +

# segment layer (alpha = 1 for segment & 0 for label)
geom_label_repel(nudge_x = 0.2, force = 2,
segment.alpha = 1, alpha = 0,
seed = 555) +

# barchart layer sandwiched in between
geom_col(aes(color = Class)) +

# label layer (alpha = 0 for segment & 1 for label, same seed as before)
geom_label_repel(nudge_x = 0.2, force = 2,
segment.alpha = 0, alpha = 1,
seed = 555)
plot

关于r - 在 R 中使用 geom_label_repel 将标签放在前面,将引线放在后面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64884545/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com