Skip to content

resolution-dependent gaps in geom_rect with narrow rectangles #6613

@ejade42

Description

@ejade42

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:

Image

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:

problematic dimensions:
Image

increased height (still problematic):
Image

increased width (no longer problematic):
Image

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions