r - 如何在 geom_label_repel 中将覆盖其他标签的箭头发送到后面?
问题描述
这看起来应该相当简单,但我找不到任何论据来做到这一点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")
解决方案
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")
推荐阅读
- java - 将文本文件的内容添加到哈希集中
- java - Spring Boot - 如何在单个 POST 请求中创建实体和嵌套实体
- html - 选取框未使用 css3 完成循环
- azure - Microsoft Azure 机器人类型选择
- node.js - BigQuery:getTables 不返回表名 (Node.js)
- android - 使用 RangeSeekBar 在两个拇指之间播放一段音频
- function - Golang文档中“interface{}”之前的“a”和“...”是什么意思?
- android - Xamarin Android BulletSpan 半径丢失
- sql-server - sql server 中的 GeoArea.STAsText()
- java - Java 的 CompletableFuture 和线程