Skip to content
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

geom_sf() and facet_*(..., scales="free"): plot aspect ratio #2651

Closed
adrfantini opened this issue May 22, 2018 · 12 comments · Fixed by #2652
Closed

geom_sf() and facet_*(..., scales="free"): plot aspect ratio #2651

adrfantini opened this issue May 22, 2018 · 12 comments · Fixed by #2652
Labels
bug an unexpected problem or unintended behavior coord 🗺️
Milestone

Comments

@adrfantini
Copy link

adrfantini commented May 22, 2018

Setting scales="free" in facet_* resets geom_sf aspect ratio handling, even if not necessary. See reprex below.

library(ggplot2)
library(dplyr)
library(sf)
nc <- sf::st_read(system.file("shape/nc.shp", package = "sf"), quiet = TRUE) %>% mutate(dummy=1)
nc2 <- sf::st_read(system.file("shape/nc.shp", package = "sf"), quiet = TRUE) %>% mutate(dummy=2)
#Correct form factor for the plot:
ggplot(rbind(nc, nc2)) +  geom_sf(aes(fill = AREA)) + facet_wrap(~dummy)
#Adding scales="free" causes the plot to be warped:
ggplot(rbind(nc, nc2)) +  geom_sf(aes(fill = AREA)) + facet_wrap(~dummy, scales="free")

Without scales="free":
spectacle twz916
With scales="free":
spectacle hcf916

scales="free" should allow facets to have different ranges without altering the plot aspect ratio.

@adrfantini adrfantini changed the title geom_sf() and facet_*(..., scales="free") geom_sf() and facet_*(..., scales="free"): plot aspect ratio May 22, 2018
@clauswilke
Copy link
Member

I think the real bug is that coord_sf() now allows free scales. It's not generally possible to scale axes freely and maintain the correct aspect ratio and keep facet sizes equal.

I believe the bug was introduced as a result of #2594:

coord_sf()$is_free()
# [1] TRUE

versus:

coord_fixed()$is_free()
# [1] FALSE

The reason is coord_sf() inherits from coord_cartesian() but doesn't change the is_free() method:

ggplot2/R/sf.R

Line 262 in eecc450

CoordSf <- ggproto("CoordSf", CoordCartesian,

@clauswilke clauswilke added bug an unexpected problem or unintended behavior coord 🗺️ labels May 22, 2018
@adrfantini
Copy link
Author

It's not generally possible to scale axes freely and maintain the correct aspect ratio and keep facet sizes equal.

What if I want to show some data over USA and Canada, in two different facets? Currently, (without scales = "free") I'll have two big facets spanning the whole of North America (One with Canada data, one with USA data), instead (using scales = "free") of two smaller facets spanning USA and Canada separately. Why shouldn't this be allowed?

@clauswilke
Copy link
Member

The problem is that throughout the ggplot2 code base, the assumption is made that axes can be trained independently of other axes. To implement what you're requesting, one would have to write a training algorithm that trains x and y axes simultaneously while producing a targeted aspect ratio. This is possible in principle, but it's not immediately obvious how to do it in the current code base, at least to me. The best way forward might be to write a new faceting function specifically for this purpose, probably first in the form of an extension package. @thomasp85 may have further insight.

@batpigandme
Copy link
Contributor

batpigandme commented May 22, 2018

There are ggplot2 extensions (see http://www.ggplot2-exts.org/gallery/) that should allow you to arrange two maps side by side as such, e.g. patchwork, and (@clauswilke, correct me if I'm wrong) cowplot.

@clauswilke
Copy link
Member

@batpigandme Yes, that's the other possible approach, make the plots separately and then arrange side-by-side. But then we don't get the power of faceting.

@batpigandme
Copy link
Contributor

Oh yeah, of course, I was just offering a workaround!

@hadley
Copy link
Member

hadley commented May 22, 2018

Yes, this is a bug - this plot should throw an error message.

@hadley hadley added this to the v2.3.0 milestone May 22, 2018
clauswilke added a commit to wilkelab/ggplot2_archive that referenced this issue May 22, 2018
@adrfantini
Copy link
Author

adrfantini commented May 23, 2018

Thanks for the bugfix. I do not know the internals of ggplot so my request for clarification might seem trivial (and maybe already answered by @clauswilke ), but if the following works, why can't the same be made working with coord_sf?

d = data.frame(x=1:2)
ggplot(d, aes(x=x, y=x)) + geom_point() + facet_wrap(~x, scales="free")

@clauswilke
Copy link
Member

clauswilke commented May 23, 2018

coord_sf has a fixed aspect ratio. The appropriate comparison would be something like this:

library(ggplot2)
d = data.frame(x=c(1,100), y=c(1,100), f=c(1, 100))
ggplot(d, aes(x=y, y=y)) + geom_point() + facet_wrap(~f, scales="free") + coord_fixed()
#> Error: coord_fixed doesn't support free scales

Created on 2018-05-23 by the reprex package (v0.2.0).

@adrfantini
Copy link
Author

@clauswilke Thanks for the explanation! I believe this can be closed, right?

@clauswilke
Copy link
Member

The issue will be automatically closed once the pull request is integrated into the main code base. That hasn’t happened yet.

@lock
Copy link

lock bot commented Nov 20, 2018

This old issue has been automatically locked. If you believe you have found a related problem, please file a new issue (with reprex) and link to this issue. https://reprex.tidyverse.org/

@lock lock bot locked and limited conversation to collaborators Nov 20, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
bug an unexpected problem or unintended behavior coord 🗺️
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants