ggalluvial icon indicating copy to clipboard operation
ggalluvial copied to clipboard

geom_flow does not work properly with ggnewscale

Open mdhall272 opened this issue 9 months ago • 1 comments

Description of the issue

If you try to use ggnewscale to have different fills for flows and strata, then one of them will not show up. (This does not seem to happen with geom_alluvium, only geom_flow.)

Reproducible example

Adapted from https://www.r-bloggers.com/2022/10/how-to-create-a-ggalluvial-plot-in-r/:

ggplot(data = vaccinations,
       aes(axis1 = survey, axis2 = response, y = freq)) +
  geom_flow(aes(fill = response)) +
  scale_fill_brewer(palette = "Set1") +
  geom_stratum(aes(fill = response)) +
  geom_text(stat = "stratum",
            aes(label = after_stat(stratum))) +
  scale_x_discrete(limits = c("Survey", "Response"),
                   expand = c(0.15, 0.05)) +
  theme_void()

But now suppose we want to use ggnewscale to use different fill scales for the stratum and the flow:

library(ggnewscale)

ggplot(data = vaccinations,
       aes(axis1 = survey, axis2 = response, y = freq)) +
  geom_flow(aes(fill = response)) +
  scale_fill_brewer(palette = "Set1") +
  new_scale_fill()+
  geom_stratum(aes(fill = response)) +
  scale_fill_brewer(palette = "Set2") +
  geom_text(stat = "stratum",
            aes(label = after_stat(stratum))) +
  scale_x_discrete(limits = c("Survey", "Response"),
                   expand = c(0.15, 0.05)) +
  theme_void()

The flow fills do not show up. Alternatively, if you do this in the opposite order:

ggplot(data = vaccinations,
       aes(axis1 = survey, axis2 = response, y = freq)) +
  geom_stratum(aes(fill = response)) +
  scale_fill_brewer(palette = "Set2") +
  new_scale_fill()+
  geom_flow(aes(fill = response)) +
  scale_fill_brewer(palette = "Set1") +
  geom_text(stat = "stratum",
            aes(label = after_stat(stratum))) +
  scale_x_discrete(limits = c("Survey", "Response"),
                   expand = c(0.15, 0.05)) +
  theme_void()

The strata fills do not show up.

I'm aware that the bug could easily be in ggnewscale and this request may be a bit above and beyond the call of duty!

mdhall272 avatar Sep 13 '23 16:09 mdhall272