Skip to content

geom_flow does not work properly with ggnewscale #120

@mdhall272

Description

@mdhall272

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!

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions