how to remove ggplot legends properly (unexpected effect of show.legend)

The show.legend argument seems to work correctly to control legends for multiple variables when the variables are categorical (see first two examples below), but not when a numerical variable is included (see last example):

library(tidyverse)
my_tibble <- 
  tibble(
    c = rep(c('A', 'B'), 2),
    d = letters[1:4],
    x = c(1, 1, 3, 3),
    y = c(1, 3, 1, 3)
  )

my_tibble %>% 
  ggplot(aes(x, y)) +
  geom_point(aes(color = c, shape = d)
             , size = 2
             , show.legend = 
               c(
                 'color' = TRUE,
                 'shape' = FALSE
                 )
             ) +
  xlim(0, 4) +
  ylim(0, 4)


my_tibble %>% 
  ggplot(aes(x, y)) +
  geom_point(aes(color = c, shape = d)
             , size = 2
             , show.legend = 
               c(
                 'color' = FALSE,
                 'shape' = TRUE
               )
  ) +
  xlim(0, 4) +
  ylim(0, 4)


my_tibble %>% 
  ggplot(aes(x, y)) +
  geom_point(aes(color = x, shape = d)
             , size = 2
             , show.legend = 
               c(
                 'color' = TRUE,
                 'shape' = FALSE
               )
  ) +
  xlim(0, 4) +
  ylim(0, 4)

Created on 2020-02-02 by the reprex package (v0.3.0)

Aside from the possible bug, is this a good approach to managing multiple legends, or is there a better way?

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.