Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # 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
Add Comment
Please, Sign In to add comment