corybrunson / ggalluvial

ggplot2 extension for alluvial plots
http://corybrunson.github.io/ggalluvial/
GNU General Public License v3.0
498 stars 34 forks source link

geom_flow does not work properly with ggnewscale #120

Open mdhall272 opened 1 year ago

mdhall272 commented 1 year ago

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!

corybrunson commented 1 year ago

Hi @mdhall272, this is an interesting issue, thanks. I can reproduce the problem, and i see that it's not an issue of where data is specified (as i thought might be based on the {ggnewscale} examples). I'll have a closer look ASAP, but that might not be this or next week.