ggrepel:在点上方绘制标签

ggrepel: plot labels above points

我想尽可能精确地在点上方绘制标签(无重叠),但 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")) 

您当然可以减少 force 参数,但是标签在 重叠时不会相互排斥。

也许你可以改变nudge_y和nudge_x来调整label.If你想改变的每个点,也许你可以建立一个新的数据框来支持标签'坐标.

据我所知,您可以通过为 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)

[编辑]
我想 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"))