避免在 ggplot2 中重叠 geom_point 和 geom_text

Max*_*Max 5 r scatter-plot ggplot2 geom-text

如何避免 ggplot2 中的这两层重叠?我尝试显示文本,以便它们不会位于点上方。

check_overlap 在避免文本与自身重叠但不与其他图层重叠方面做得很好。

我也尝试过 library geom_text_repel,但是这个 library 不支持check_overlap并显示每个数据点的文本。

但我不需要每个点都有文字,就像check_overlap那样。

ggplot(dat, aes(x = CPI, y = HDI)) +
  geom_point(aes(color = Region), shape=21, size=4, position = "identity") +
  geom_text(data = dat, aes(label = Country), size=4, check_overlap = TRUE)
Run Code Online (Sandbox Code Playgroud)

Kam*_*ski 2

geom_text_repel不会为空字符串创建文本标签""。然而,文本标签将排斥未标记的数据点。

尝试这个:

# Hide text labels for the first 3 data points
idx <- c(1,2,3)

dat$CountryLabel      <- dat$Country
dat$CountryLabel[idx] <- ""

library(ggrepel)
ggplot(...) + geom_text_repel(data = dat, aes(label = CountryLabel))
Run Code Online (Sandbox Code Playgroud)