4

I want to plot labels precisely above points where possible (no overlap), but ggrepel seems to insist on plotting slightly above or below. E.g.

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")) 

enter image description here

You can of course reduce the force argument, but then labels don't repel each other when they do overlap.

geotheory
  • 22,624
  • 29
  • 119
  • 196

2 Answers2

5

As far as I can see, you can avoid overlapping by giving point.padding the same value as geom_point(size). And you can define above position by giving nudge_y a very small plus value.

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)

[EDITED]
I guess box.padding = unit(-0.5, "lines") gives the labels the points positions (but it probably bases on Capital letters).

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"))

enter image description here

cuttlefish44
  • 6,586
  • 2
  • 17
  • 34
  • Thanks. Well it is a step forward I guess, although the labels still not plotting at the exact point positions. – geotheory Oct 09 '16 at 19:31
  • Nice. So `nudge_y` pushes them all up and `point.padding` pulls them back down? – geotheory Oct 09 '16 at 20:48
  • 1
    @geotheory; I'd missed the more smart method, I edited. I think so about `nudge_y`, and interpret `point.padding` is distance from the point position to the bottom of labels (if it is upperside). But they become unnecessary... – cuttlefish44 Oct 09 '16 at 21:12
0

Maybe you can change nudge_y and nudge_x to adjust the label.If you want to change each of points,maybe you can set up a new data frame to shore the labels' coordinate.

Vida Wang
  • 406
  • 2
  • 7