5

Is it possible to rename a legend which combines different aesthetics in ggplot2

For example:

  1. Combine color with linetype: Controlling line color and line type in ggplot legend
  2. Combine color with shape: How to recycle colours in a colorbrewer palette using line symbols

How could one rename such a combined legend? When I try using name for scale_shape_manual or scale_color_manual the combined legend splits into two different legends. That is not what I want.


Possible Solution: A solution which I found here: https://stackoverflow.com/a/14622513/4317408 worked for me...

Basically there are two possible approaches, in both of them for the combined legend to get a new name, the name of the two different legends which are combined has to be modified as the same:

  scale_color_manual(values = c(brewer.pal(5, "Set1") ) , name = "Combined Legend Title") +
  scale_shape_manual(values = c(1,2,3,4,5) , name = "Combined Legend Title")

or

  guides(shape=guide_legend(title="Combined Legend Title"), color=guide_legend(title="Combined Legend Title"))

Still I wonder if there is a simpler way of doing this?

Community
  • 1
  • 1
cross
  • 1,018
  • 13
  • 32
  • 1
    I don't think so. In ggplot, legends combine if their mapping _and_ their name is the same, so AFAIK you'll have to repeat the renaming twice. – tonytonov Jun 15 '15 at 08:48

0 Answers0