我想在可能的情况下将标签精确地绘制在点上方(没有重叠),但 ggrepel 似乎坚持在略高于或略低于点的情况下绘制。例如
require(ggplot); require(ggrepel)
ggplot(iris[1:10,], aes(Sepal.Length, Sepal.Width)) +
geom_point(pch=1, size=8) +
geom_text_repel(aes(label=Species), segment.color=NA,
point.padding=unit(0, "lines"))
Run Code Online (Sandbox Code Playgroud)
您当然可以减少force争论,但是标签在重叠时不会相互排斥。
据我所知,您可以通过提供point.padding与geom_point(size). 你可以通过给出nudge_y一个非常小的加值来定义上面的位置。
library(dplyr)
## an example data (delete duplicated data)
iris2 <- iris %>% distinct(Sepal.Length, Sepal.Width, .keep_all = T)
g <- ggplot(iris2[1:20,], aes(Sepal.Length, Sepal.Width)) + geom_point(pch=1, size=8)
g + geom_text_repel(aes(label=Species), segment.color=NA,
point.padding = unit(8, "points"), nudge_y = 1.0E-6)
Run Code Online (Sandbox Code Playgroud)
[已编辑]
我猜box.padding = unit(-0.5, "lines")给了标签点位置(但它可能基于大写字母)。
iris2$Species <- as.character(iris2$Species)
iris2[7,5] <- "ABCDEFG"
g2 <- ggplot(iris2[1:20,], aes(Sepal.Length, Sepal.Width)) + geom_point(pch = 1, size = 8)
g2 + geom_text_repel(aes(label=Species), segment.color=NA, box.padding = unit(-0.5, "lines"))
Run Code Online (Sandbox Code Playgroud)