ggplot2
ggplot2 copied to clipboard
Awkward interaction between faceting, polar coordinates, and free scales
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.
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?
With the current development version of ggplot2, the reprex that Claus posted above runs with the same output that he posted above.
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)
This did it. Thanks for the tip.
On Tue, Jun 18, 2019 at 12:30 PM mymil [email protected] wrote:
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$is_free <- function() TRUE ggplot(mpg, aes(x = 1, fill = drv)) + geom_bar() + cp + facet_wrap(~manufacturer, scales = "free") + theme(aspect.ratio = 1)
[image: aspect ratio_facet_pie] https://user-images.githubusercontent.com/33109970/59701890-2a3c8800-91bc-11e9-953a-01a40d000f8f.png
— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/tidyverse/ggplot2/issues/2815?email_source=notifications&email_token=AGBIAYRN2AIKR25XQ2S55STP3EERVA5CNFSM4FOXW752YY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGODX7GQKA#issuecomment-503212072, or mute the thread https://github.com/notifications/unsubscribe-auth/AGBIAYSAKQUVSE2WVJ5BI2DP3EERVANCNFSM4FOXW75Q .
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)