1

So a handful of posts already address how to remove unwanted legends in ggplot.

The wonderful answer posted to "Remove extra legends in ggplot2" suggests:

For any mapped variable you can supress the appearance of a legend by using guide = 'none' in the appropriate scale_...

However, I'm having problems with unwanted legends being created by adding the group aesthetic. I tried the scale approach, but it doesn't seem to work with the group argument:

could not find function "scale_group" 

A search here didn't provide any insight on the proper function call to modify group aesthetics either.

User @joran provided the following insight in the linked post above:

That's because the group aesthetic doesn't generate any scales or guides on its own. It's always sort of modifying something else. You'll never get a legend for the group aesthetic.


Example

So I could just add show.legend = FALSE to my function call containing group to remove any legend for that function, but this doesn't work out if I want some other portion (i.e., aesthetic) of that call to be included in the legend.

#Set Up Example:

library(lme4)
library(ggplot2)

mod <- lmer(mpg ~ hp + (1 |cyl), data = mtcars)
pred <- predict(mod,re.form = NA)
pdat <- data.frame(mtcars[,c('hp','cyl')], mpg = pred, up = pred+1, low = pred-1)

Adding show.legend = F to function calls work as expected:

gp <- 
  ggplot(data = mtcars, aes(x = hp, y = mpg, color = cyl, group = cyl), show.legend = F) +
  geom_point(aes(group = cyl),show.legend = F) +
  facet_wrap(~cyl) + 
  geom_line(data = pdat, aes(group = cyl),show.legend = F, color = 'orange')

But when I want to add a legend for a geom_ribbon fill based on the same group (and therefore cannot use the show.legend = F argument), I get a legend for my group again...

gp + geom_ribbon(data = pdat, aes(ymin = low, ymax = up, group = cyl, fill = 'mod'), alpha = 0.3) + 
scale_fill_manual(values=c("orange"), name="model")

The outputs:

enter image description here

theforestecologist
  • 4,667
  • 5
  • 54
  • 91
  • 1
    It's because it's inheriting the `color` aesthetic (you can see it in the outline of the ribbons). If you only set that in `geom_point`, it will go away. – alistaire May 24 '18 at 16:02
  • @alistaire ohhhhh. So by assigning `color` in the main `ggplot ` function call, all subsequent function calls are "activating" (not sure the right word to use) the `color` call (and thus creating a legend for it unless supressed using `show.legend=F`)? Is that right? – theforestecologist May 24 '18 at 16:05
  • @theforestecologist: what's your expected output? only `model` legend without `cyl` gradient scale? – Tung May 24 '18 at 16:16
  • 2
    Use `inherit.aes = FALSE` such as `geom_ribbon(data = pdat, aes(x = hp, ymin = low, ymax = up, group = cyl, fill = 'mod'), inherit.aes = FALSE, alpha = 0.3)` – Tung May 24 '18 at 16:21
  • 1
    @theforestecologist Yep, provided that geom plots the aesthetic in some way (here the outline of the ribbon). If you look in a geom's docs (`?geom_ribbon` here), it will tell you which aesthetics it understands. – alistaire May 24 '18 at 16:28
  • @Tung yes. my expected output is to have colored points without a legend (i.e., without the `cyl` gradient scale) but to have a legend for my "`model`" ribbons. I can see now that the issue lies with the `color` aesthetic, not with `group` directly. Your `inherit.aes` argument also works great in this situation (after adding the necessary `x` `aes` to the ribbon function). Thanks! :) – theforestecologist May 24 '18 at 16:36
  • @theforestecologist: nice, good to know the problem was solved! – Tung May 24 '18 at 16:43

1 Answers1

0

The last geom cover the first geom, you can try this

ggplot(data = mtcars, aes(x = hp, y = mpg, color = cyl, group = cyl), show.legend = F) +
  geom_point(aes(group = cyl)) +                             
  facet_wrap(~cyl) + 
  geom_line(data = pdat, aes(group = cyl),color = 'orange') 

gp + geom_ribbon(data = pdat, aes(ymin = low, ymax = up, group = cyl, fill = 'mod'),       alpha = 0.3) + 
  scale_fill_manual(values=c("orange"), name="model")+
  scale_color_continuous(guide ='none')

By the way, my idea comes from the link you posted at the top.

CCwudi
  • 1