6

How can I avoid that these 2 layers in ggplot2 overlap? I try to display the text so that they are not laying above the points.

check_overlap does a great job with avoiding that the text overlaps itself, but not with other layers.

I also tried the library geom_text_repel, but this library does not support check_overlap and shows the text for every data point.

But I need to not have the text for every point, like check_overlap does.

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)
Z.Lin
  • 28,055
  • 6
  • 54
  • 94
Max
  • 61
  • 1
  • 2
  • 5
    [`ggrepel`](https://cran.r-project.org/web/packages/ggrepel/index.html) – hrbrmstr Oct 04 '17 at 01:00
  • Can you remove the repeated labels at each data point, & then use `geom_text_repel` from ggrepel? – Z.Lin Oct 04 '17 at 02:12
  • i have tried `geom_text_repel` but it shows a label or text for every datapoint in the `geom_point`, what i dont want. – Max Oct 04 '17 at 02:29

1 Answers1

2

geom_text_repel will not create text labels for the empty string "". However, the text labels will repel away from the unlabeled data points.

Try this:

# 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))
missuse
  • 19,056
  • 3
  • 25
  • 47
Kamil Slowikowski
  • 4,184
  • 3
  • 31
  • 39