gpt4 book ai didi

r - 如何将覆盖其他标签的箭头发送到 geom_label_repel 的后面?

转载 作者:行者123 更新时间:2023-12-01 16:15:01 24 4
gpt4 key购买 nike

这看起来应该相当简单,但我找不到任何参数来使用ggrepel::geom_label_repel()来做到这一点。

数据示例:

df <- structure(list(Athletename = c("Aries Merritt", "Damian Warner"
), Score = c(12.8, 13.44), Event = c("110m hurdles", "110m hurdles"
), Points = c(1135, 1048), Record = c("World Record", "Decathlon Record"
), score_and_points = c("12.8s, 1135pts", "13.44s, 1048pts")), row.names = c(NA,
-2L), class = c("tbl_df", "tbl", "data.frame"), .Names = c("Athletename",
"Score", "Event", "Points", "Record", "score_and_points"))

ggplot2代码:

ggplot(data = data.frame(x = 0), mapping = aes(x = x)) +
geom_point(data = df, aes(x=Score, y=Points, colour=Record)) +
geom_label_repel(data = df,
aes(x=Score, y=Points, label = Athletename),
direction = "x",
nudge_x = -10) +
geom_label_repel(data = df,
aes(x=Score, y=Points, label = score_and_points),
direction = "y",
nudge_y = -200) +
scale_y_continuous(name = "Points",
breaks = seq(0,1500,100),
limits = c(0,1500)) +
scale_x_reverse(name = "110m hurdles time (m)",
breaks = seq(29,12,-1),
limits=c(29,12)) +
theme(legend.title = element_blank(), legend.position = "top")

enter image description here

最佳答案

Hacky 但有效:添加 geom_label_repel 调用的副本,但添加 segment.alpha = 0。然后所有标签将位于所有箭头之上。

library(ggrepel)
ggplot(data = data.frame(x = 0), mapping = aes(x = x)) +
geom_point(data = df, aes(x=Score, y=Points, colour=Record)) +
geom_label_repel(data = df,
aes(x=Score, y=Points, label = Athletename),
direction = "x",
nudge_x = -10) +
geom_label_repel(data = df,
aes(x=Score, y=Points, label = score_and_points),
direction = "y",
nudge_y = -200, ) +
geom_label_repel(data = df,
aes(x=Score, y=Points, label = score_and_points),
direction = "y", segment.alpha = 0,
nudge_y = -200, ) +
scale_y_continuous(name = "Points",
breaks = seq(0,1500,100),
limits = c(0,1500)) +
scale_x_reverse(name = "110m hurdles time (m)",
breaks = seq(29,12,-1),
limits=c(29,12)) +
theme(legend.title = element_blank(), legend.position = "top")

enter image description here

关于r - 如何将覆盖其他标签的箭头发送到 geom_label_repel 的后面?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52824607/

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