Skip to content

geom_raster() fallback for non-linear Cartesian coordinates #6383

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
May 20, 2025

Conversation

teunbrand
Copy link
Collaborator

This PR aims to fix #6382 and amends #5627.

It kicks off the fallback mechanism too if the coord is non-linear. I hadn't foreseen at the time that coord_sf() is technically a non-linear child-coordinate of coord_cartesian().

Reprex from the issue:

devtools::load_all("~/packages/ggplot2")
#> ℹ Loading ggplot2
library(sf)
#> Linking to GEOS 3.12.1, GDAL 3.8.4, PROJ 9.3.1; sf_use_s2() is TRUE

file <- file.choose()
data <- read.csv(file, row.names = 1)

targetcrs <- paste(
  "+proj=lcc", 
  "+lat_1=25", 
  "+lat_2=47", 
  "+lon_0=105", 
  "+datum=WGS84", 
  "+units=m", 
  sep = " "
)

ggplot(data = data, aes(x = x, y = y))+
  geom_raster(aes(fill = value))+
  facet_wrap(~class)+
  coord_sf(
    crs = st_crs('epsg:32649'),
    default_crs = st_crs('epsg:4326')
  )
#> `geom_raster()` only works with `coord_cartesian()`.
#> ℹ Falling back to drawing as `geom_rect()`.
#> `geom_raster()` only works with `coord_cartesian()`.
#> ℹ Falling back to drawing as `geom_rect()`.
#> `geom_raster()` only works with `coord_cartesian()`.
#> ℹ Falling back to drawing as `geom_rect()`.

Created on 2025-03-27 with reprex v2.1.1

@teunbrand teunbrand added this to the ggplot2 4.0.0 milestone May 6, 2025
R/geom-raster.R Outdated
@@ -90,7 +90,7 @@ GeomRaster <- ggproto("GeomRaster", Geom,

draw_panel = function(self, data, panel_params, coord, interpolate = FALSE,
hjust = 0.5, vjust = 0.5) {
if (!inherits(coord, "CoordCartesian")) {
if (!inherits(coord, "CoordCartesian") || !coord$is_linear()) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't it be enough to test coord$is_linear() and forget about the special case for CoordCartesian?

The message below also needs an update

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah good points, I agree

Copy link
Member

@thomasp85 thomasp85 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@teunbrand teunbrand merged commit 109d049 into tidyverse:main May 20, 2025
13 checks passed
@teunbrand teunbrand deleted the fix_6382 branch May 20, 2025 08:50
@Breeze-Hu
Copy link

Great!
Thanks : )

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

No error reported but not displayed when using crs and default_crs parameter in coord_sf
3 participants