Agenda


Learn to modify legend

  • title
  • label
  • bar

Libraries


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

Title


  • position
    • top
    • bottom
    • left
    • right
  • alignment
    • 0 (left)
    • 1 (right)

Legend Title


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

Title Alignment


ggplot(mtcars) + geom_point(aes(disp, mpg, color = factor(cyl))) +
  scale_color_manual(values = c("red", "blue", "green"),
    guide = guide_legend(title = "Cylinders", title.hjust = 0.5))

Title Position


ggplot(mtcars) + geom_point(aes(disp, mpg, color = factor(cyl))) +
  scale_color_manual(values = c("red", "blue", "green"),
    guide = guide_legend(title = "Cylinders", title.hjust = 0.5, 
      title.position = "top"))

Label


  • position
    • top
    • bottom
    • left
    • right
  • alignment
    • 0 (left)
    • 1 (right)
  • direction
    • horizontal
    • vertical
  • reverse
    • TRUE
    • FALSE
  • rows

Label Position


ggplot(mtcars) + geom_point(aes(disp, mpg, color = factor(cyl))) +
  scale_color_manual(values = c("red", "blue", "green"),
    guide = guide_legend(label.position = "right"))

Label Alignment


ggplot(mtcars) + geom_point(aes(disp, mpg, color = factor(cyl))) +
  scale_color_manual(values = c("red", "blue", "green"),
    guide = guide_legend(label.hjust = 0.5))

Direction


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

Rows


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

Reverse


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

Guides


ggplot(mtcars) + geom_point(aes(disp, mpg, color = factor(cyl))) +
  scale_color_manual(values = c("red", "blue", "green"),
    guide = guide_legend(title = "Cylinders", title.hjust = 0.5, 
      title.position = "top", label.position = "right", 
      direction = "horizontal", label.hjust = 0.5, nrow = 2, reverse = TRUE)
  )

Title Alignment (Vertical)


ggplot(mtcars) + geom_point(aes(disp, mpg, color = hp)) +
  scale_color_continuous(guide = guide_colorbar(
    title = "Horsepower", title.position = "top", title.vjust = 1))

Labels Alignment (Vertical)


ggplot(mtcars) +
  geom_point(aes(disp, mpg, color = hp)) +
  scale_color_continuous(guide = guide_colorbar(
    label.vjust = 0.8))

Legend Bar


  • width
  • height
  • bins
  • ticks
  • limits

Legend Bar Width


ggplot(mtcars) +
  geom_point(aes(disp, mpg, color = hp)) +
  scale_color_continuous(guide = guide_colorbar(
    barwidth = 10))

Legend Bar Height


ggplot(mtcars) +
  geom_point(aes(disp, mpg, color = hp)) +
  scale_color_continuous(guide = guide_colorbar(
    barheight = 3))

Legend Bar Bins


ggplot(mtcars) +
  geom_point(aes(disp, mpg, color = hp)) +
  scale_color_continuous(guide = guide_colorbar(
    nbin = 4))

Legend Bar Ticks


ggplot(mtcars) +
  geom_point(aes(disp, mpg, color = hp)) +
  scale_color_continuous(guide = guide_colorbar(
    ticks = FALSE))

Legend Bar Upper/Lower Limits


ggplot(mtcars) +
  geom_point(aes(disp, mpg, color = hp)) +
  scale_color_continuous(guide = guide_colorbar(
    draw.ulim = TRUE, draw.llim = FALSE))

Guides


ggplot(mtcars) +
  geom_point(aes(disp, mpg, color = hp, 
    size = qsec, shape = factor(gear))) + 
  guides(color = "colorbar", shape = "legend", size = "legend")

Guides: Color, Shape & Size


ggplot(mtcars) +
  geom_point(aes(disp, mpg, color = hp, size = wt, shape = factor(gear))) + 
  guides(color = guide_colorbar(title = "Horsepower"),
    shape = guide_legend(title = "Weight"), size = guide_legend(title = "Gear")
  )