tidyverse / ggplot2

An implementation of the Grammar of Graphics in R
https://ggplot2.tidyverse.org
Other
6.48k stars 2.02k forks source link

Internal function `find_x_overlaps()` has bug #5938

Closed teunbrand closed 2 months ago

teunbrand commented 3 months ago

In the example below, ranges 1, 2, 3 all overlap and hence should get the same overlap identifier from find_x_overlaps(). However, range 3 is counted separately, despite having 100% overlap with range 1.

library(ggplot2)

f <- function(x, xend) ggplot2:::find_x_overlaps(data.frame(xmin = x, xmax = xend))

df <- data.frame(
  xmin = c(1, 2, 3, 5),
  xmax = c(4, 3, 4, 6),
  group = factor(1:4)
)

ggplot(df, aes(x = xmin, xend = xmax, y = group)) +
  geom_segment(aes(colour = factor(f(xmin, xmax))))

Consequentially, position_dodge2() does not actually dodge range 3 from 1 & 2:

ggplot(df, aes(xmin = xmin, xmax = xmax)) +
  geom_rect(aes(ymin = 0, ymax = 1, fill = group),
            alpha = 0.75,
            position = position_dodge2())

Created on 2024-06-07 with reprex v2.1.0