Agenda


Modify legend when the aesthetics are mapped to variables. Specifically

  • title
  • breaks
  • limits
  • range
  • labels
  • values
  • symbol type

Intro


Quite often the aesthetics are mapped to variables ggplot2 automatically creates legends wherever applicable. You may want to modify the appearance of legends. In this module, we will learn to modify the legends when the following aesthetics are mapped to categorical/continuous variables

  • continuous variables
    • alpha
    • size
  • discrete variables
    • alpha
    • color
    • fill
    • shape
    • size

Libraries


library(ggplot2)
library(dplyr)
library(tidyr)

Continuous Variables


If you have mapped aesthetics to a continuous variable, you can modify the legend

  • title
  • breaks
  • limits
  • range
  • and labels

Map Size to Continuous Variable


ggplot(mtcars) +
  geom_point(aes(disp, mpg, size = hp))

Legend Title


ggplot(mtcars) +
  geom_point(aes(disp, mpg, size = hp)) +
  scale_size_continuous(name = "Horsepower")

Legend Range


ggplot(mtcars) +
  geom_point(aes(disp, mpg, size = hp)) +
  scale_size_continuous(range = c(3, 6))

Legend Limits


ggplot(mtcars) +
  geom_point(aes(disp, mpg, size = hp)) +
  scale_size_continuous(limits = c(0, 400))

Legend Breaks


ggplot(mtcars) +
  geom_point(aes(disp, mpg, size = hp)) +
  scale_size_continuous(breaks = c(100, 200, 300, 400))

Legend Labels


ggplot(mtcars) +
  geom_point(aes(disp, mpg, size = hp)) +
  scale_size_continuous(breaks = c(100, 200, 300, 400),
    labels = c("Hundred", "2 Hundred", "3 Hundred", "4 Hundred"))

Size - Continuous


ggplot(mtcars) +
  geom_point(aes(disp, mpg, size = hp)) +
  scale_size_continuous(name = "Horsepower", range = c(3, 6), 
    limits = c(0, 400), breaks = c(100, 200, 300, 400),
    labels = c("Hundred", "2 Hundred", "3 Hundred", "4 Hundred"))

Map Alpha to Continuous Variable


ggplot(mtcars) +
  geom_point(aes(disp, mpg, alpha = hp), color = 'blue')

Legend Title


ggplot(mtcars) +
  geom_point(aes(disp, mpg, alpha = hp), color = 'blue') +
  scale_alpha_continuous("Horsepower")

Legend Breaks


ggplot(mtcars) +
  geom_point(aes(disp, mpg, alpha = hp), color = 'blue') +
  scale_alpha_continuous(breaks = c(0, 100, 200, 300, 400))

Legend Limits


ggplot(mtcars) +
  geom_point(aes(disp, mpg, alpha = hp), color = 'blue') +
  scale_alpha_continuous(limits = c(0, 400))

Legend Range


ggplot(mtcars) +
  geom_point(aes(disp, mpg, alpha = hp), color = 'blue') +
  scale_alpha_continuous(range = c(0.4, 0.8))

Legend Labels


ggplot(mtcars) +
  geom_point(aes(disp, mpg, alpha = hp), color = 'blue') +
  scale_alpha_continuous(breaks = c(0, 100, 200, 300, 400),
    labels = c("Zero", "Hundred", "2 Hundred", 
      "3 Hundred", "4 Hundred"))

Alpha - Continuous


ggplot(mtcars) +
  geom_point(aes(disp, mpg, alpha = hp), color = 'blue') +
  scale_alpha_continuous("Horsepower", breaks = c(0, 100, 200, 300, 400),
    limits = c(0, 400), range = c(0.4, 0.8),
    labels = c("Zero", "Hundred", "2 Hundred", 
      "3 Hundred", "4 Hundred"))

Alpha Discrete


If you have mapped aesthetics to a discrete variable, you can to modify legend:

  • title
  • breaks
  • range
  • limits
  • labels
  • and values

Map Color to Discrete Variable


ggplot(mtcars) +
  geom_point(aes(disp, mpg, color = factor(cyl)))

Legend Title


ggplot(mtcars) +
  geom_point(aes(disp, mpg, color = factor(cyl))) +
  scale_color_manual(name = "Cylinders", 
    values = c("red", "blue", "green"))

Legend Values


ggplot(mtcars) +
  geom_point(aes(disp, mpg, color = factor(cyl))) +
  scale_color_manual(values = c("red", "blue", "green"))

Legend Labels


ggplot(mtcars) +
  geom_point(aes(disp, mpg, color = factor(cyl))) +
  scale_color_manual(values = c("red", "blue", "green"),
    limits = c(4, 6, 8))

Legend Limits


ggplot(mtcars) +
  geom_point(aes(disp, mpg, color = factor(cyl))) +
  scale_color_manual(values = c("red", "blue", "green"),
    labels = c('Four', 'Six', 'Eight'))

Legend Breaks


ggplot(mtcars) +
  geom_point(aes(disp, mpg, color = factor(cyl))) +
  scale_color_manual(values = c("red", "blue", "green"),
    breaks = c(4, 6, 8))

Color - Discrete Variable


ggplot(mtcars) +
  geom_point(aes(disp, mpg, color = factor(cyl))) +
  scale_color_manual(name = "Cylinders", values = c("red", "blue", "green"),
    labels = c('Four', 'Six', 'Eight'), limits = c(4, 6, 8), breaks = c(4, 6, 8))

Map Fill to Discrete Variable


ggplot(mtcars) +
  geom_point(aes(disp, mpg, fill = factor(cyl)), shape = 22)

Legend Title


ggplot(mtcars) +
  geom_point(aes(disp, mpg, fill = factor(cyl)), shape = 22) +
  scale_fill_manual(name = "Cylinders", 
    values = c("red", "blue", "green"))

Legend Values


ggplot(mtcars) +
  geom_point(aes(disp, mpg, fill = factor(cyl)), shape = 22) +
  scale_fill_manual(values = c("red", "blue", "green"))

Legend Labels


ggplot(mtcars) +
  geom_point(aes(disp, mpg, fill = factor(cyl)), shape = 22) +
  scale_fill_manual(values = c("red", "blue", "green"),
    labels = c('Four', 'Six', 'Eight'))

Legend Limits


ggplot(mtcars) +
  geom_point(aes(disp, mpg, fill = factor(cyl)), shape = 22) +
  scale_fill_manual(values = c("red", "blue", "green"),
    limits = c(4, 6, 8))

Legend Breaks


ggplot(mtcars) +
  geom_point(aes(disp, mpg, fill = factor(cyl)), shape = 22) +
  scale_fill_manual(values = c("red", "blue", "green"),
    breaks = c(4, 6, 8))

Legend Fill


ggplot(mtcars) +
  geom_point(aes(disp, mpg, fill = factor(cyl)), shape = 22) +
  scale_fill_manual(name = "Cylinders", values = c("red", "blue", "green"),
    labels = c('Four', 'Six', 'Eight'), limits = c(4, 6, 8), breaks = c(4, 6, 8))

Map Shape to Discrete Variable


ggplot(mtcars) +
  geom_point(aes(disp, mpg, shape = factor(cyl)))

Legend Title


ggplot(mtcars) +
  geom_point(aes(disp, mpg, shape = factor(cyl))) +
  scale_shape_manual(name = "Cylinders",values = c(4, 12, 24))

Legend Values


ggplot(mtcars) +
  geom_point(aes(disp, mpg, shape = factor(cyl))) +
  scale_shape_manual(values = c(4, 12, 24))

Legend Labels


ggplot(mtcars) +
  geom_point(aes(disp, mpg, shape = factor(cyl))) +
  scale_shape_manual(values = c(4, 12, 24), labels = c('Four', 'Six', 'Eight'))

Legend Limits


ggplot(mtcars) +
  geom_point(aes(disp, mpg, shape = factor(cyl))) +
  scale_shape_manual(values = c(4, 12, 24), limits = c(4, 6, 8))

Legend Breaks


ggplot(mtcars) +
  geom_point(aes(disp, mpg, shape = factor(cyl))) +
  scale_shape_manual(values = c(4, 12, 24), breaks = c(4, 6, 8))

Shape - Discrete Variable


ggplot(mtcars) +
  geom_point(aes(disp, mpg, shape = factor(cyl))) +
  scale_shape_manual(name = "Cylinders", labels = c('Four', 'Six', 'Eight'),  
     values = c(4, 12, 24), limits = c(4, 6, 8), breaks = c(4, 6, 8))

Map Alpha to Discrete Variable


ggplot(mtcars) +
  geom_point(aes(disp, mpg, alpha = factor(cyl)), color = 'blue')

Legend Title


ggplot(mtcars) +
  geom_point(aes(disp, mpg, alpha = factor(cyl)), color = 'blue') +
  scale_alpha_discrete(name = "Cylinders")

Legend Breaks


ggplot(mtcars) +
  geom_point(aes(disp, mpg, alpha = factor(cyl)), color = 'blue') +
  scale_alpha_discrete(breaks = c("4", "6", "8"))

Legend Range


ggplot(mtcars) +
  geom_point(aes(disp, mpg, alpha = factor(cyl)), color = 'blue') +
  scale_alpha_discrete(range = c(0.4, 0.8))

Legend Labels


ggplot(mtcars) +
  geom_point(aes(disp, mpg, alpha = factor(cyl)), color = 'blue') +
  scale_alpha_discrete(labels = c("Four", "Six", "Eight"))

Alpha - Discrete Variable


ggplot(mtcars) +
  geom_point(aes(disp, mpg, alpha = factor(cyl)), color = 'blue') +
  scale_alpha_discrete(name = "Cylinders", range = c(0.4, 0.8),
    breaks = c("4", "6", "8"), labels = c("Four", "Six", "Eight"))

Shape & Size


If you have mapped shape/size to a discrete variable which has less than six categories, you can use the following functions:

  • scale_shape()
  • scale_size()

Map Shape to Discrete Variable


ggplot(mtcars) +
  geom_point(aes(disp, mpg, shape = factor(cyl))) +
  scale_shape()

Legend Title


ggplot(mtcars) +
  geom_point(aes(disp, mpg, shape = factor(cyl))) +
  scale_shape(name = 'Cylinders')

Legend Labels


ggplot(mtcars) +
  geom_point(aes(disp, mpg, shape = factor(cyl))) +
  scale_shape(labels = c('Four', 'Six', 'Eight'))

Legend Limits


ggplot(mtcars) +
  geom_point(aes(disp, mpg, shape = factor(cyl))) +
  scale_shape(limits = c(4, 6, 8))

Legend Breaks


ggplot(mtcars) +
  geom_point(aes(disp, mpg, shape = factor(cyl))) +
  scale_shape(breaks = c(4, 6, 8))

Legend Types


ggplot(mtcars) +
  geom_point(aes(disp, mpg, shape = factor(cyl))) +
  scale_shape(solid = FALSE)

Shape - Discrete Variable


ggplot(mtcars) +
  geom_point(aes(disp, mpg, shape = factor(cyl))) +
  scale_shape(solid = FALSE, name = 'Cylinders', limits = c(4, 6, 8),
    labels = c('Four', 'Six', 'Eight'), breaks = c(4, 6, 8))