8

I'm trying to use ggrepel to create text labels for charts that I'm working on using R and ggplot2. I'm finding it very useful for repelling away from a single point, but I often run into a problem where it overlaps some other plot objects.

Here is a plot that describes the problem.

I'm trying to add it to the plot like so:

plot + ggrepel::geom_text_repel(aes(y = Ratio, label = Ratio), direction = "y")

Is there some way that I can tell ggrepel to avoid everything on the ggplot? I've tried searching and coming up with something for this but I'm stuck.

I hope my question is clear enough, thank you.

r2evans
  • 141,215
  • 6
  • 77
  • 149
rockcop
  • 126
  • 4
  • 1
    The [vignette](https://cran.r-project.org/web/packages/ggrepel/vignettes/ggrepel.html) provides some examples of constraining the labels to a region or aligning them on an edge. Perhaps that will allow you to shift the text (optionally with arrows) away from the lines. Otherwise, I believe `ggrepel` is mainly (only?) for preventing overlap with their specific data points and other text/labels, not other geoms within the plot. – r2evans Jun 27 '18 at 14:49
  • Another alternative, though, would be to assign empty strings to the other geoms. In the vignette, look at "Hide some of the labels", and think about creating a frame with all (relevant) points with empty labels. If you provide sample data (for a truly [reproducible question](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example)), I might be able to show what I mean here. – r2evans Jun 27 '18 at 14:50

1 Answers1

3

ggrepel does not allow avoiding all geoms.

In your case, as a workaround, you can use nudge_y = 0.1 in to shift all labels up. Often, in such cases, you would want more space for the labels. This can be achieved with e.g. + scale_y_continous(expand = scales::expansion(mult = c(0.05, 0.2)))

ggrepel will not label, but repel from, points with a empty ("") label. So in general, as a workaround, you can try to generate data that would cover the other geoms and include that data with empty labels in your geom_text_repel call.

jan-glx
  • 7,611
  • 2
  • 43
  • 63