Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ggplot legend: position of key relative to labels

Tags:

r

ggplot2

I'm using ggplot to make a graph with the legend positioned horizontally above the plot. There are multiple legends for my variables (ie color, shape, linetype).

+ theme(legend.position = 'top', legend.direction = "horizontal", legend.box = "horizontal")

Is there anyway to put the title of each legend above the descriptions rather than to the side

and/or

place the key above the label text.

like image 273
Luke Hankins Avatar asked Nov 16 '15 04:11

Luke Hankins


1 Answers

Something like adding guides(...) for each aesthetic?

ggplot(diamonds, aes(depth, price, color = cut)) + 
  geom_point() +  
  theme(legend.position = 'top', 
        legend.direction = "horizontal", 
        legend.box = "horizontal") + 
  guides(color = guide_legend(title.position = "top", 
                              # hjust = 0.5 centres the title horizontally
                              title.hjust = 0.5,
                              label.position = "bottom")) 

enter image description here

like image 189
Hugh Avatar answered Oct 05 '22 20:10

Hugh