Skip to content

Instantly share code, notes, and snippets.

@tomhopper
Created February 9, 2018 13:42
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tomhopper/0a51ff880a89c5ec94060cd50166b401 to your computer and use it in GitHub Desktop.
Save tomhopper/0a51ff880a89c5ec94060cd50166b401 to your computer and use it in GitHub Desktop.
When using many factor levels, a ggplot2 legend can become too busy to be useful. This shows how to pair back the labels in the legend to something readable, with less detail.
# Limit factor levels displayed in ggplot2 legend
# When using many factor levels, the legend can become too large to be useful
# We want to pair back the legend to something readable, with less detail.
# For our minimally reproducible example, we will create a series of 40 sinusoidal
# curves that are slightly offset from each other, then plot each one in a different
# color. The resulting plot will have a somewhat rainbow look, but the legend will
# contain 40 labels; we want to reduce this to about 5 labels in the legend.
# Libraries used
library(dplyr)
library(ggplot2)
# Create some data for demonstration purposes (minimal reproducible example)
base_curve <- sin(x = seq(0, 2*pi, by = 0.05))
multipliers <- rep(seq(from = 0, to = 1, by = 0.025), each = length(base_curve))
rep_times <- length(multipliers) / length(base_curve)
base_curve <- rep(base_curve, times = rep_times)
my_df <- data_frame(base_curve,
multipliers) %>%
mutate(curve = base_curve + multipliers,
mult_fact = as.factor(multipliers),
index = rep(seq(0, 2*pi, by = 0.05), times = rep_times))
# Plot the data with ggplot
my_df %>%
ggplot(aes(x = index, y = curve, color = mult_fact)) +
geom_line() +
scale_color_discrete(breaks = c(0, 0.2, 0.4, 0.8, 1))
# Run the plot again without scale_color_discrete() to see how the legend
# is too detailed and visually busy
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment