David Rubinger
David Rubinger

Reputation: 3938

How to get legend labels that differ from fill aesthetic in R ggplot2

In ggplot2, how do I get the legend labels for the fill aesthetic to differ from the variable actually used as the fill aesthetic? What I'd like to see from the following plot is the legend labels reflecting the name variable. I know I could just use the name itself as the fill aesthetic; however, in the following example, it's more convenient to set up the colour vector storm_cols (used for ggplot2::scale_fill_manual) using the id column as the vector names rather than typing out each name.

library(dplyr)
library(ggplot2)
dat <-
    storms %>% 
    filter(year >= 2015) %>% 
    group_by(name, year) %>% 
    summarize(avg_wind = mean(wind)) %>% 
    ungroup() %>% 
    mutate(id = as.character(row_number())) %>% 
    slice(1:4)
storm_cols <- c("1" = "red", "2" = "blue", "3" = "green", "4" = "yellow")
dat %>% 
    ggplot(aes(id, avg_wind, fill = id)) +
    geom_col() +
    scale_fill_manual(values = storm_cols)

Upvotes: 0

Views: 490

Answers (1)

eipi10
eipi10

Reputation: 93761

You don't need to explicitly type out the names for the color vector. Instead, you can create it programmatically, making it easier to create the desired color assignments and use name directly as the fill aesthetic. For example, in this case you can use the set_names function from the purrr package (or the base R setNames function).

library(tidyverse)

dat %>% 
  ggplot(aes(id, avg_wind, fill = name)) +
  geom_col() +
  scale_fill_manual(values = c("red","blue","green","yellow") %>% set_names(dat$name))

enter image description here

With your original example, you could change the legend labels with the labels argument to scale_fill_manual:

dat %>% 
  ggplot(aes(id, avg_wind, fill = id)) +
  geom_col() +
  scale_fill_manual(values = storm_cols,
                    labels = dat$name)

Upvotes: 2

Related Questions