tidyverse / ggplot2

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

Move coord clipping responsibility from facet to coord #5953

Open teunbrand opened 2 weeks ago

teunbrand commented 2 weeks ago

This PR aims to fix #5951 and fix #5952.

Briefly, the solution here is that facets no longer clip panels to their rectangular cells in the gtable, but coords set the appropriate viewport on their panels. This also allows custom behaviour for coord_radial() to use a clipping path instead of rectangle.

Reprex from issue; note that the points in upper quarter are appropriately clipped.

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

ggplot(mpg, aes(displ, hwy)) +
  geom_point() +
  scale_x_continuous(limits = c(3, 6), oob = scales::oob_keep) +
  coord_radial(
    start = 0.25 * pi, end = 1.75 * pi, 
    clip = "on"
  )

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

Of note, non-rectangular clipping paths are a newer feature in R graphics and are silently ignored prior to R4.1.0.

teunbrand commented 1 week ago

As #5952 was code-adjacent to this PR, i've included a fix for that issue as well. The fix is to set theme(strip.clip = "on") by default and always use that instead of coord clip settings in the strips.