Skip to content
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

scale_xfill_manual does not work as expected with factors when using drop argument #62

Open
Michael-Sheppard opened this issue Jan 22, 2025 · 0 comments

Comments

@Michael-Sheppard
Copy link

The scale_xfill_manual() does not seem to work as expected for a categorical factor when the argument drop = FALSE is set. Here is a simple, reproducible example where I create a series that can take random values between 5 and 50. I create 3 categories for binning the y-values where the first 2 will obviously capture all values in the series and then intentionally created the 3rd category to capture values above 50 (which cannot happen) and will help illustrate the issue.

library(tidyverse)
library(ggside)
library(RColorBrewer)

set.seed(1234)

df <- tibble(
  x = 1:20,
  y = runif(n = 20, 5, 50)
  ) %>%
  mutate(
    class = case_when(
      y <= 20 ~ "Category 1",
      y > 20 & y <= 50 ~ "Category 2", 
      y > 50 ~ "Category 3"
    ),
    class = factor(class, levels = paste("Category", 1:3))
  )

pal <- brewer.pal(n = 3, name = "Dark2")

ggplot(data = df) + 
  theme_bw() + 
  geom_line(aes(x = x, y = y)) + 
  scale_y_continuous(
    limits = c(0, 60),
    expand = c(0, 0)
  ) + 
  geom_xsidetile(aes(xfill = class, x = x, y = "Dummy class")) + 
  scale_xsidey_discrete() + 
  scale_xfill_manual(
    values = pal, 
    name = "Classification", 
    drop = FALSE
  ) + 
  ggside(x.pos = "bottom") + 
  theme(legend.position = "bottom")

Here is the resulting graph when using geom_xsidetile():

Image

As you can see, the label for Category 3 shows in the legend, but the color (which should be purple with the Dark2 color palette) does not. Please keep in mind that this is just a toy example to illustrate the issue. This can cause a problem if you are creating many plots at once for a large number of time series where not every series will necessary take on all possible categories.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant