Search code examples
rggplot2legend

How do I position two legends independently in ggplot


Title pretty well covers it.

I have two legends, relating to size, and colour, and wish to have one, say, on the top, and one within the graph.

Is this possible and, if so, how?


Solution

  • From my understanding, basically there is very limited control over legends in ggplot2. Here is a paragraph from the Hadley's book (page 111):

    ggplot2 tries to use the smallest possible number of legends that accurately conveys the aesthetics used in the plot. It does this by combining legends if a variable is used with more than one aesthetic. Figure 6.14 shows an example of this for the points geom: if both colour and shape are mapped to the same variable, then only a single legend is necessary. In order for legends to be merged, they must have the same name (the same legend title). For this reason, if you change the name of one of the merged legends, you’ll need to change it for all of them.