resolution-dependent gaps in geom_rect with narrow rectangles
Closed this issue · 2 comments
The latest version (v4.0.0) has introduced a resolution-dependent issue with narrow rectangles in geom_rect.
My data contains a lot of thin rectangles (used to provide manual control over a gradient at a specified precision level), e.g.
xmin xmax
1 0.000 0.001
2 0.001 0.002
3 0.002 0.003
4 0.003 0.004
5 0.004 0.005
6 0.005 0.006
When rendered at specific widths and resolutions, some of the rectangles disappear:
This was not an issue in the previous version of ggplot prior to v4.0.0
I tested with slightly different plot margins and slightly different export sizes and found that this effect was incredibly sensitive to horizontal size, but not at all sensitive to vertical size.
For example:
library(ggplot2)
precision <- 1000
x_scale <- seq(0, 1-1/precision, 1/precision)
scale_data <- data.frame(xmin = x_scale, xmax = x_scale + 1/precision)
## theme and margin settings are required to get the exact right size to see the issue
ggplot(scale_data) +
geom_rect(aes(xmin = .data$xmin, xmax = .data$xmax, ymin = 0, ymax = 1), fill = "red") +
coord_cartesian(expand = F) +
theme(axis.ticks = element_blank(), axis.text = element_blank(),
plot.margin = grid::unit(c(0.05, 0.15, 0.05, 0.15), "inches")) +
guides(fill = "none")ggsave("testing_outputs/250915_broken_rectangle_1.png", dpi = 300, width = 5, height = 1.25, device = ragg::agg_png, create.dir = T)
#> ✔ Created directory: 'testing_outputs'.
ggsave("testing_outputs/250915_broken_rectangle_2.png", dpi = 300, width = 5, height = 1.5, device = ragg::agg_png, create.dir = T)
ggsave("testing_outputs/250915_broken_rectangle_3.png", dpi = 300, width = 6, height = 1.25, device = ragg::agg_png, create.dir = T)Created on 2025-09-15 with reprex v2.1.1
ggsaved images in order:
increased height (still problematic):

increased width (no longer problematic):

For now I can probably work around this by changing the margin slightly to prevent the issue at the sizes I want to render, but wanted to raise the issue.
I'm actually not sure that this is new to 4.0.0
In the previous version my bars were rendering like this:
And are now like this in 4.0.0:
So, 4.0.0 slightly increased the width of the rectangles at identical margin settings, which may have hit the perfect size to see an existing issue.
(see ejade42/ggDNAvis@21d0ed8)
Thanks for the report! I can reproduce that your examples using 3.5.2 do not have 'missing pixels columns' and 4.0.0 does.
One change we've made in 4.0.0 is that empty ticks no longer take up space. This makes the plot area slightly wider in your examples, because you set axis.ticks = element_blank().
Indeed, using 3.5.2 and setting axis.ticks.length.y = unit(0, "cm") also shows the missing pixel columns.
From the ggplot2 side of things, you can try geom_raster() or setting the colour aesthetic as well. In addition, using geom_area()/geom_ribbon() will display a polygon with a solid fill (when the fill is constant) or a gradient (when the fill is not constant).
Ultimately, I think this is a device issue and @thomasp85 might be able to advice on this some more. But how pixels are rendered are two steps removed from ggplot2's control (via grid and graphics device), so there isn't anything we'll be able to do. For that reason, I'll close this issue here.

