ggplot2: use scale_colour_brewer() and specify legend name / entry labels manually
Asked Answered
R

1

5

I want to use scale_colour_brewer() and scale_fill_brewer() to specify the fill or colours via the brewer palette:

diagram <- diagram + scale_colour_brewer() + scale_fill_brewer()

Nevertheless, I still want to set the legend label and its entries manually. I did so formerly via:

diagram + 
  scale_colour_manual(name="Cumulative Percentage", 
  values=c("#d7191c","#fdae61","#000000","#abdda4","#2b83ba"), 
  labels=c("GN","GN1","GN2","GN3","GN4"))

How can I use the auto-palette feature, while still setting the legend name and the entry labels manually?

Thanks!

Rattlebrain answered 28/4, 2014 at 10:36 Comment(1)
COuld you provide a minimal data and your solution?Gehenna
D
9

You can use the same arguments name= and labels= also in scale_colour_brewer().

+ scale_colour_brewer(name="Cumulative Percentage", 
                      labels=c("GN","GN1","GN2","GN3","GN4"))
Drink answered 28/4, 2014 at 10:42 Comment(3)
Thanks, that did it! I just noted the (no that obvious) reference to the parameters of scale_discrete() in the docs.Rattlebrain
@Didzis Elferts Hi Didzis, I was wondering why scale_colour_brewer() works in this specific case. Is it because the legend Chris is referring to shows values plotted with an aesthetic that has different colors per value? Do you know if there is a way for users to know what scale_-function is appropriate to apply?Chrissychrist
@Chrissychrist brewer scale works when color depends on discrete variable and you need brewer palette. Scales should be chosen according variable (discrete or continuous) and aesthetic used - fill or color, and so on. Manual scale is used if you need to use your own colors.Drink

© 2022 - 2024 — McMap. All rights reserved.