1

Is there a way to partially suppress legend in ggplot2 ? For example for chart below

enter image description here

There are way too many colours in the legend. Let's say I still want to display all colours, but only want to show the legend for letters b to e. Is there any way to do so?

Ricky
  • 4,616
  • 6
  • 42
  • 72
  • 3
    2 possibilies: break the legend into columns or give all the points that are not of interest the same color and call them "other". You shouldn't use more than about 10 discrete colors (with an appropriate color palette) in a plot because more cannot be distinguished well. – Roland Jan 24 '15 at 10:42
  • 1
    You can set `limits` using `scale_colour_hue` to suppress the legend for certain points - [this might help.](http://stackoverflow.com/questions/27375504/remove-legends-for-each-point-and-keep-only-those-which-are-outliers-for-ggplot?answertab=votes#tab-top) . But as Roland says it may ne worth rethinking the plot – user20650 Jan 24 '15 at 16:44

1 Answers1

2

Keeping in mind the comments above, you may use the following solution. The linked answer proposes changing limits, but another possibility is to override breaks. Here's how:

let <- letters[1:20]
let_be <- let
p <- qplot(1:20, 1:20, colour = let)
let_be[!(let %in% c("b", "e"))] <- NA
p + scale_color_discrete(breaks = let_be)

enter image description here

tonytonov
  • 25,060
  • 16
  • 82
  • 98