Various position adjustments of legend in ggplot2


As everyone knows, legend guide is important to map the visual property onto the value in efficient way for brain.

There are three parameters for adjusting the position of legend:

  • legend.position (top, bottom, left, or right): the location where the legends are placed.
  • legend.direction (vertical or horizontal): the direction of key/value pair.
  • legend.box (vertical or horizontal): the direction of the placement of multiple legend boxes.

Example is best way to understand.

Data:

mtcars$long <- factor(sample(3, nrow(mtcars), TRUE),
  labels = c("this is very long label", "this is very long label2", "this is\nvery long\nlabel3"))
mtcars$short_elements_with_long_title <- factor(sample(2, nrow(mtcars), TRUE), labels = c("s1", "s2"))

 
Examples with long title and short key/values, default:

p <- qplot(mpg, wt, data = mtcars, colour = factor(cyl), shape = short_elements_with_long_title)
p


 
position bottom, horizontal direction and horizontal box alignment:

p + opts(legend.direction = "horizontal", legend.position = "bottom")

 
position bottom, horizontal direction and vertical box alignment:

p + opts(legend.direction = "horizontal", legend.position = "bottom", legend.box = "vertical")

 
Examples with short title and long key/values, default:

p <- qplot(mpg, wt, data = mtcars, colour = factor(cyl), shape = long)
p

 
position bottom, horizontal direction and horizontal box alignment:

p + opts(legend.direction = "horizontal", legend.position = "bottom")

 
position bottom, horizontal direction and vertical box alignment:

p + opts(legend.direction = "horizontal", legend.position = "bottom", legend.box = "vertical")

 
Justification of legend title on the left side:

p + opts(legend.direction = "horizontal", legend.position = "bottom", legend.box = "vertical", legend.title.align = 0)

12 thoughts on “Various position adjustments of legend in ggplot2

  1. Hi,

    Would you know how to the boxes of the legend under a long title? I mean, if you look at the figure with “short_element_with_long_title” the legend isn’t centered under this title.

    Thank you!

  2. Something that would be great would be to have the opportunity to display continuous colour scale legend using a gradient picture with a graduated axis on the side…
    This is more compact and more intuitive than the current separated boxes. But it is a bit out the topic of this post! Just tell this as I noticed that you already did a great job to improve ggplot2 legend option! Thanks btw…

    • If you say legend.position=”top” or “bottom” you can hard code extra white space in the name of the labels like

      + scale_fill_manual(labels=c(“1st label “,”2nd label”))+

      but i think there should be an option in futur versions

  3. hey. how did you put the labels over the legend items? When I execute your code at home the labels are placed on the left side of the legend items…

  4. Pingback: Side by side horizontal legends in in ggplot2 | Solutions for enthusiast and professional programmers

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s