eliocamp / ggnewscale

Multiple Fill, Color and Other Scales in `ggplot2`
https://eliocamp.github.io/ggnewscale/
GNU General Public License v3.0
406 stars 18 forks source link

new_scale_*() throws an error in ggplot2 3.5.0 #64

Closed alhenry closed 8 months ago

alhenry commented 9 months ago

The function new_scale_*() throws the following error in ggplot2 3.5.0:

Error in scale$guide == "none" : 
  comparison (==) is possible only for atomic and list types

Presumably this is something related to the new behaviour of guides & scales in ggplot?

eliocamp commented 8 months ago

This is supposed to be fixed by #57, which is included in the latest release. All tests pass with the current ggplot2 version. Make sure you're running the latest ggnewscale version. If the problem persists, please submit a reproducible example so I can investigate further.

alhenry commented 8 months ago

I've tested with the latest version of ggplot2 and ggnewscale on CRAN and no longer found the issue. Will close this for now. Sorry for the false alarm!

eliocamp commented 8 months ago

Good to know! Do report any other issue, though.

bennotkin commented 4 months ago

_Edit: I wrote all of this and then realized the ggplot 3.5 now allows for guide = "colorsteps", not just guide = guide_colorsteps(). With the former, I get the desired behavior. Editing, rather than deleting, this comment for anyone who may find this helpful._


I continue to have the same error, when I set guide = guide_colorsteps().

Here's a reproducible example:

ggplot(mpg) +
  geom_point(aes(x = cty, y = hwy, color = displ)) +
  scale_color_stepsn(
    colors = c("yellow", "red"),
    guide = guide_colorsteps()) +
  ggnewscale::new_scale_color()
# Error in scale$guide == "none" : 
#   comparison (==) is possible only for atomic and list types

I don't get the error if I don't include guide = guide_colorsteps()[^1], nor if `guide = "legend". Both of the following work:

[^1]: Normally, one wouldn't include guide = guide_colorsteps() in scale_color_stepsn, as it is the default behavior; however, in my non-reprex usage I sometimes set guide = "legend": guide = if (diff(lengths(list(params$labels, params$breaks))) == 1) "legend" else guide_colorsteps()

ggplot(mpg) +
  geom_point(aes(x = cty, y = hwy, color = displ)) +
  scale_color_stepsn(colors = c("yellow", "red")) +
  ggnewscale::new_scale_color()

ggplot(mpg) +
  geom_point(aes(x = cty, y = hwy, color = displ)) +
  scale_color_stepsn(
    colors = c("yellow", "red"),
    guide = "legend") +
  ggnewscale::new_scale_color()

If, rather than setting the guide in scale_color_stepsn but in guides(), there is no error with ggnewscale::new_scale_color(), but the guide setting persists after ggnewscale::new_scale_color(), and that causes a different error when the guide type is different in the next plot:

ggplot(mpg) +
  geom_point(aes(x = cty, y = hwy, color = displ)) +
  scale_color_stepsn(colors = c("yellow", "red")) +
  guides(color = guide_colorsteps()) +
  ggnewscale::new_scale_color() +
  # Shifting x and y so they are obviously separate from previous layer
  geom_point(aes(x = cty + 10, y = 50 - hwy, color = class), shape = 1)
# Error in `parse_binned_breaks()`:
# ! Breaks are not formatted correctly for a bin legend.
# ℹ Use `(<lower>, <upper>]` format to indicate bins.
# Run `rlang::last_trace()` to see where the error occurred.
# Warning message:
# In parse_binned_breaks(scale, breaks) : NAs introduced by coercion