Part 63 Legend guides

There are four styles of legend guides available:

  1. guide_legend()
  • Basic legend guide.
  • Use this one most of the time.
  1. guide_bins()
  • Use if you’ve got binned scales.
  1. guide_colorbar()
  • Nicely show a continuous color gradient.
  1. guide_colorsteps()
  • Use with binned color/fill scales.
  • Can also be useful if you have continous scales, but want to pick out useful reference levels.
p1 <- p + 
  guides(
    colour = guide_legend(title = "legend"),
    size = "none",
    shape = "none"
  )
p2 <- p + 
  guides(
    colour = guide_bins(title = "bins"),
    size = "none",
    shape = "none"
  )
p3 <- p + 
  guides(
    colour = guide_colorbar(title = "colorbar"),
    size = "none",
    shape = "none"
  )
p4 <- p + 
  guides(
    colour = guide_colorsteps(title = "colorsteps"),
    size = "none",
    shape = "none"
  )

p1 + p2 + p3 + p4

63.1 Legend arguments

  1. title
  • Set the title of the legend guide
  • Also impacts legend merging
  1. direction
  • "vertical" or "horizontal"
  1. nrow and ncol
  • Control number of rows and columns of legend
  1. reverse
  • TRUE or FALSE: Should the order of the legend values be reversed?
  1. order
  • If multiple legend guides are shown separately, what order should they appear in?
  • A number between 1 and 99

To place the legend somewhere other than the right side, add theme(legend.position = "bottom") to the plot:

p + 
  guides(
    colour = guide_legend(title = "title"),
    size = guide_legend(title = "title"),
    shape = guide_legend(title = "title")
  ) +
  theme(legend.position = "bottom")