Skip to content

Awkward interaction between faceting, polar coordinates, and free scales #2815

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

Closed
clauswilke opened this issue Aug 9, 2018 · 5 comments · Fixed by #5354
Closed

Awkward interaction between faceting, polar coordinates, and free scales #2815

clauswilke opened this issue Aug 9, 2018 · 5 comments · Fixed by #5354
Labels
bug an unexpected problem or unintended behavior coord 🗺️ facets 💎

Comments

@clauswilke
Copy link
Member

The concept of free scales under faceting doesn't quite work the way I would think it should when we're dealing with polar coordinates.

If we make set of facets with simple polar bar plots, we get partial pies because by default the scales are the same across facets:

library(ggplot2)

ggplot(mpg, aes(x = 1, fill = drv)) + 
  geom_bar() +
  coord_polar(theta = "y") +
  facet_wrap(~manufacturer)

Trying to make the scales free, so that each facet has its own theta scale, doesn't work, because coord_polar() needs a fixed aspect ratio and hence coord_polar()$is_free() returns FALSE:

ggplot(mpg, aes(x = 1, fill = drv)) + 
  geom_bar() +
  coord_polar(theta = "y") +
  facet_wrap(~manufacturer, scales = "free_y")
#> Error: coord_polar doesn't support free scales

However, we can make a version of coord_polar() that returns TRUE and it works just fine and makes the pie charts as expected. But then the aspect ratio is wrong:

cp <- coord_polar(theta = "y")
cp$is_free <- function() TRUE

ggplot(mpg, aes(x = 1, fill = drv)) + 
  geom_bar() +
  cp +
  facet_wrap(~manufacturer, scales = "free")

It seems to me that there are two separate concepts that are being muddled together, the aspect ratio of the facets and the limits of the scales. For most coordinate systems these two concepts are linked one-to-one, but for some (like coord_polar()) they are not.

@cdbyron
Copy link

cdbyron commented Jun 6, 2019

I am also having this problem. Have you discovered a fix? I used to be able to get the faceted polar plots to show (but in skewed oval fashion). Now I can't get the code to process anymore. Has something changed with newer versions of Tidyverse?

@paleolimbot
Copy link
Member

With the current development version of ggplot2, the reprex that Claus posted above runs with the same output that he posted above.

@paleolimbot paleolimbot added coord 🗺️ facets 💎 bug an unexpected problem or unintended behavior labels Jun 7, 2019
@Joe-Wasserman
Copy link

Joe-Wasserman commented Jun 18, 2019

I've been struggling with this same issue but located a solution to the skewed plots using theme(aspect.ratio = 1): https://stackoverflow.com/a/21295436/8218363

cp <- coord_polar(theta = "y")
cp$is_free <- function() TRUE

ggplot(mpg, aes(x = 1, fill = drv)) + 
  geom_bar() +
  cp +
  facet_wrap(~manufacturer, scales = "free") +
  theme(aspect.ratio = 1)

aspect ratio_facet_pie

@cdbyron
Copy link

cdbyron commented Jun 19, 2019 via email

@frederikziebell
Copy link

This is working for facet_wrap(), but the same trick does not work for facet_grid()

cp <- coord_polar(theta = "y")
cp$is_free <- function() TRUE

ggplot(mpg, aes(x = 1, fill = drv)) + 
  geom_bar() +
  cp +
  facet_wrap(~manufacturer, scales = "free") +
  theme(aspect.ratio = 1)

image

@thomasp85 thomasp85 added this to the ggplot2 3.3.4 milestone Mar 25, 2021
@thomasp85 thomasp85 modified the milestones: ggplot2 3.3.4, ggplot2 3.4.0 Apr 9, 2021
@thomasp85 thomasp85 removed this from the ggplot2 3.4.0 milestone May 18, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug an unexpected problem or unintended behavior coord 🗺️ facets 💎
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants