Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I use ggrepepl to avoid all geoms?

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.

like image 655
rockcop Avatar asked Nov 17 '22 08:11

rockcop


1 Answers

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.

like image 172
jan-glx Avatar answered Dec 10 '22 13:12

jan-glx