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

new_scale_colour does not work with a scale_* function that includes a guide argument #58

Closed
davidhodge931 opened this issue Sep 5, 2023 · 4 comments

Comments

@davidhodge931
Copy link

library(palmerpenguins)
library(tidyverse)
#> Warning: package 'purrr' was built under R version 4.3.1
library(ggnewscale)
#> Warning: package 'ggnewscale' was built under R version 4.3.1

d <- penguins |>
  group_by(sex, species) |>
  summarise(across(flipper_length_mm, \(x) mean(x, na.rm = TRUE))) |>
  tidyr::drop_na(sex) |>
  mutate(species = stringr::str_to_sentence(species))
#> `summarise()` has grouped output by 'sex'. You can override using the `.groups`
#> argument.


p <- d |>
  ggplot(aes(x = flipper_length_mm, colour = species, fill = species, y = sex)) +
  geom_col(position = "dodge", width = 0.75, alpha = 0.8) +
  scale_colour_manual(values = c("lightgrey", "darkblue", "orange"),
                      guide = ggplot2::guide_legend(reverse = T, title.position = "top"),
                      aesthetics = c("colour", "fill"))

##########################
#does not work with a guide
##########################
p +
  ggnewscale::new_scale_colour()
#> Error in scale$guide == "none": comparison (==) is possible only for atomic and list types

p <- d |>
  ggplot(aes(x = flipper_length_mm, colour = species, fill = species, y = sex)) +
  geom_col(position = "dodge", width = 0.75, alpha = 0.8) +
  scale_colour_manual(values = c("lightgrey", "darkblue", "orange"),
                      # guide = ggplot2::guide_legend(reverse = T, title.position = "top"),
                      aesthetics = c("colour", "fill"))

##########################
#works without a guide
##########################
p +
  ggnewscale::new_scale_colour()

Created on 2023-09-05 with reprex v2.0.2

@davidhodge931
Copy link
Author

This is using the DEV version of ggplot2 and ggnewscale

Fantastic package btw :)

@davidhodge931
Copy link
Author

davidhodge931 commented Sep 5, 2023

If I put the guides arguments as a seperate layer instead of from within the scale function, then it allows the new_scale_color layer but creates problems with the legend by ignoring the initial colour scale guide arguments.

library(tidyverse)
#> Warning: package 'purrr' was built under R version 4.3.1
#> Warning: package 'dplyr' was built under R version 4.3.1
library(palmerpenguins)
library(ggnewscale)

d <- penguins |>
  group_by(sex, species) |>
  summarise(across(flipper_length_mm, \(x) mean(x, na.rm = TRUE))) |>
  tidyr::drop_na(sex) |>
  mutate(species = stringr::str_to_sentence(species))
#> `summarise()` has grouped output by 'sex'. You can override using the `.groups`
#> argument.

d |>
  ggplot(aes(y = flipper_length_mm, x = sex, colour = species, fill = species)) +
  geom_col(position = "dodge", width = 0.75, alpha = 0.8) +
  scale_colour_manual(values = c("lightgrey", "darkblue", "orange"),
                      aesthetics = c("colour", "fill")) +
  guides(colour = ggplot2::guide_legend(reverse = TRUE, title.position = "top"),
         fill = ggplot2::guide_legend(reverse = TRUE, title.position = "top")) +
  new_scale_color()

Created on 2023-09-06 with reprex v2.0.2

@davidhodge931
Copy link
Author

I assume that this relates to this??
#57

@davidhodge931 davidhodge931 changed the title new_scale_colour does not work with a ggplot2 guide new_scale_colour does not work with a scale_* function that includes a guide argument Sep 11, 2023
@eliocamp
Copy link
Owner

I assume that this relates to this?? #57

Yes, that PR fixed this issue.

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

2 participants