eliocamp / ggnewscale

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

setting legend name in `guides` vs. in `scale_manual` with `new_scale()` #66

Closed A-Zalesak closed 2 months ago

A-Zalesak commented 4 months ago

This issue seems potentially related to #59 , but its differences might be enough to help inform the refactoring/recoding you've said you're working on.

Before I share the context, the issue can be reproduced with only these lines:

# doesn't work
library(ggplot2)
library(ggnewscale)
plot = ggplot() + guides(linetype = guide_legend("Linetype Legend Title")) + guides(fill = "none") + new_scale_fill()

The warning message that accompanies the incorrect output is: "In names(guides)[to_change] <- paste0(names(guides), "_new") : number of items to replace is not a multiple of replacement length"

When I set the linetype legend name a different way, there is no problem, but it took me a while to track down the issue and figure out what to do differently since I am not experienced with ggplot2 or ggnewscale.

# works
library(ggplot2)
library(ggnewscale)
plot = ggplot() + scale_linetype(name="Linetype Legend Title") + guides(fill = "none") + new_scale_fill()

This code probably doesn't make much sense out of context, so if you're interested, here's my real use case. I decided to use ggnewscale because I've got two different datasets that I'm plotting together that both need the fill aesthetic, one for geom_ribbon() and one for geom_point(). I also plot some lines where I use the linetype aesthetic. I sometimes want two different legends for the fill of the ribbons and the points, but sometimes I only have one ribbon with one color, so I want to make the scale to set that fill color, but I don't want to show the legend for it. So I remove the legend with guides(fill = 'none') before making a new fill scale. I'm baffled that changing the linetype legend would mess with the fill scale, but after reading some of what you've said here and in Stack Overflow, I can see that the internals of the package could make that happen.

Hopefully this example can be of use to you in the refactor/recode you've mentioned. From the perspective of a user less experienced with ggplot2, I'd also find it useful to have a more informative warning message that could suggest that I don't use guides() if I'm using the ggnewscale package. Then again, the guides(fill = "none") part of my example didn't trigger any issues, only the guides(linetype = guide_legend("Linetype Legend Title")), so it must be more complicated than that.

Thank you for making this handy package, and please let me know if I can provide more details.

eliocamp commented 2 months ago

Hi, thanks for the report.

The first example now works on the latest dev version of ggnewscale

library(ggplot2)
library(ggnewscale)
plot = ggplot() +
  guides(linetype = guide_legend("Linetype Legend Title")) + 
  guides(fill = "none") + 
  new_scale_fill()

plot

Created on 2024-07-18 with reprex v2.1.0

Bear in mind that ggnewscale is now only compatible with ggplot2 >= 3.5.0 due to changes in the internal representation of guides (likely that was the problem here).