0

This question is related to this post, but distinct.

Instead of using alpha, or color or shape, I would like to use something akin to ggrepel. Instead of text labels, the points would be moved away from their original location, and an arrow or line segment would point to the original location of the point.

For example, in this plot from @Claus-Wilke :

ggplot(mpg, aes(displ, cty, colour = drv, fill = drv)) +
  geom_point(position=position_jitter(h=0.1, w=0.1),
             shape = 21, alpha = 0.5, size = 3) +
  scale_color_manual(values=linecolors) +
  scale_fill_manual(values=fillcolors) +
  theme_bw()

Instead of jittering, overlapping points would be repelled from each other, and arrows/segments would start at the point, and end at the point's original location. This would get very busy with a lot of overlaps, but would be bearable if there were only a few. Any suggestions?

Thanks!

Alex Krohn
  • 71
  • 5
  • 2
    If you want to use arrows to show the origin of your point, will also not become too crowded? – Quinten Apr 03 '23 at 17:21
  • 2
    I find the notion of `geom_point_repel` somewhat appealing, can you put in a feature-request? Oh wait ... it was already requested seven years ago :-( https://github.com/slowkow/ggrepel/issues/20 (It has seen recent discussion, though) – r2evans Apr 03 '23 at 17:31
  • @Quinten In this example, maybe. But in my actual example there are only a small subset of points that are actually overlapping, and thus only a small subset of points that would require repelling. – Alex Krohn Apr 03 '23 at 18:57

1 Answers1

2

This is the closest I could get using ggrepel::geom_label_repel(), though I don't think the result is a useful visualization and wouldn't use it myself.

library(ggplot2)
library(ggrepel)

linecolors <- c("#714C02", "#01587A", "#024E37")
fillcolors <- c("#9D6C06", "#077DAA", "#026D4E")

ggplot(mpg, aes(displ, cty, colour = drv, fill = drv)) +
  geom_label_repel(
    aes(label = " "), 
    size = 0.35,
    label.r = 0.25, 
    alpha = 0.5, 
    max.overlaps = Inf, 
    min.segment.length = 0,
    force = 5,
    seed = 13
  ) +
  scale_color_manual(values=linecolors) +
  scale_fill_manual(values=fillcolors) +
  coord_cartesian(xlim = c(0, 8), ylim = c(0, 40)) +
  theme_bw()

zephryl
  • 14,633
  • 3
  • 11
  • 30