8000 7FFF
Skip to content

Add the possibilty for show.legend to be a vector #1798

Description

@Tutuchan

In geom_* functions, the show.legend parameter can be either TRUE or FALSE and applies to the whole layer.

When TRUE (the default), lines appear in the legends for factor(vs) (colour) and factor(am) (linetype):

library(ggplot2)

ggplot(mtcars) +
  geom_point(aes(mpg, qsec, colour = factor(vs), shape = factor(am))) + 
  geom_line(aes(mpg, qsec, colour = factor(vs), linetype = factor(am)))

image

When FALSE, lines do not appear in the legend at all:

ggplot(mtcars) +
  geom_point(aes(mpg, qsec, colour = factor(vs), shape = factor(am))) + 
  geom_line(aes(mpg, qsec, colour = factor(vs), linetype = factor(am)), show.legend = FALSE)

image

The idea would be to provide a named logical vector to show.legend in order to specify which aesthetic are shown in the legend for this layer, like so, where lines appear in the legend for factor(am) but not for factor(vs):

ggplot(mtcars) +
    geom_point(aes(mpg, qsec, colour = factor(vs), shape = factor(am))) + 
    geom_line(aes(mpg, qsec, colour = factor(vs), linetype = factor(am)), 
              show.legend = c(colour = FALSE))

image

Please let me know if you think this could be a useful addition. I tried my hand at implementing this (see here) and this works (except when using color instead of colour) but I'm sure there is a better way if you decide to implement it.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

    0