ggrepel icon indicating copy to clipboard operation
ggrepel copied to clipboard

Variable pointsize

Open teunbrand opened this issue 5 months ago • 5 comments

This PR aims o fix #83.

Briefly, it takes Paul's advice here to cast everything to an absolute unit as soon as possible, and then refuse to deal with other types of units ever again (unless absolutely needed). This should make the segments attach nicely to the normally sized circle point (shapes 1, 16, 19, 21, but not 20). This is also stable upon resizing the device window.

Reprex from https://github.com/slowkow/ggrepel/issues/83#issuecomment-536123752:

devtools::load_all("~/packages/ggrepel")
#> ℹ Loading ggrepel
#> Loading required package: ggplot2

set.seed(42)
d <- data.frame(
  x = c(1, 2, 2, 3),
  y = c(1, 2, 3, 1),
  pointsize = c(0, 2, 1, 0),
  label = sprintf("label%s", 1:4)
)

p <- ggplot(d, aes(x, y)) +
  geom_text_repel(
    min.segment.length = 0.1,
    box.padding = 0.2,
    aes(label = label, point.size = pointsize),
    size = 5, max.iter = 1e5, max.time = 1
  ) +
  continuous_scale(
    aesthetics = c("size", "point.size"), 
    palette = scales::area_pal(c(2, 50)), guide = "none"
  )
  
p + geom_point(aes(size = pointsize), color = "grey50")

Shape = 20 has a smaller diameter, so the segment doesn't attach for that shape.

p + geom_point(aes(size = pointsize), color = "grey50", shape = 20)

Created on 2024-01-17 with reprex v2.1.0

I had to update 1 test that didn't draw any segments by default. Visual tests needed updating due to tiny changes in location of some of the labels, usually something like 0.01 unit in a scale of 100s. I presume these are rounding errors and otherwise harmless.

Please have a go and play around a bit yourself as I hadn't considered every option under the sun for this.

teunbrand avatar Jan 17 '24 20:01 teunbrand