Agenda


In this module, we will continue to explore different ways to modify/customize the legend of plots including:

  • position
  • alignment
  • direction
  • legend bar
    • width
    • height
    • bins
    • ticks
    • limits

Libraries


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

Position


  • position
    • top
    • bottom
    • left
    • right

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"))

Alignment


  • alignment
    • 0 (left)
    • 1 (right)

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


  • direction
    • horizontal
    • vertical

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


  • reverse
    • TRUE
    • FALSE

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


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


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

Bar Width


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

Bar Height


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

Bar Bins


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

Bar Ticks


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

Bar 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"))