Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Side by side horizontal legends in in ggplot2

Tags:

r

ggplot2

I would like to get my ggplot legends to appear side by side, underneath the plot with the variable names above the symbols, as they are in this blog post (the second plot). The opts function is now defunct, and theme does not appear to replicate its behaviour...

library("ggplot2")
ggplot(diamonds, aes(x = carat, y=price, shape = cut, group=interaction(cut, color), color=color)) +
geom_point() +
#opts(legend.direction = "horizontal", legend.position = "bottom")
#potential options, not all seem have an effect...
theme(legend.direction = "horizontal") +
theme(legend.position = "bottom") +
theme(legend.box = "vertical") +
theme(legend.title.align = 0)

enter image description here

...using my MS paint skills to illustrate the desired plot.

like image 200
guyabel Avatar asked Jan 21 '15 11:01

guyabel


3 Answers

You need to specify theme(legend.box = "horizontal")

Try this:

library("ggplot2") ggplot(diamonds, aes(x = carat, y=price, shape = cut, group=interaction(cut, color), color=color)) +   geom_point() +   theme(legend.direction = "horizontal",          legend.position = "bottom",         legend.box = "horizontal"         ) 

enter image description here

like image 158
Andrie Avatar answered Sep 23 '22 02:09

Andrie


Adapting from the previous suggestions with legend.box = "horizontal", I have found that you can get the legend titles on top using title.position = "top" in the guides for the scale_ functions. These have to be defined for each variable that makes up the legend else the title will be to the left.

ggplot(data = diamonds,         mapping = aes(x = carat, y = price, shape = cut,                      group=interaction(cut, color), color=color)) +   geom_point() +   theme(legend.box = "horizontal",         legend.position="bottom") +   scale_shape(guide = guide_legend(title.position = "top")) +   scale_colour_discrete(guide = guide_legend(title.position = "top", nrow = 1)) 

enter image description here

You could shift the titles to the center, as I suggested in the question, using title.hjust = 0.5. However, upon inspection, doing so might confuse the reader as to which colours/points refers to which variable.

like image 36
guyabel Avatar answered Sep 22 '22 02:09

guyabel


@gjbel - I think to make the legend title above the symbols you either need to change legend direction from horizontal to vertical or remove legend direction entirely as the default is vertical:

library("ggplot2")
ggplot(diamonds, aes(x = carat, y=price, shape = cut, group=interaction(cut, color), color=color)) +
  geom_point() +
  theme(legend.direction = "vertical", 
        legend.position = "bottom",
        legend.box = "horizontal"
        )

OR

library("ggplot2")
    ggplot(diamonds, aes(x = carat, y=price, shape = cut, group=interaction(cut, color), color=color)) +
      geom_point() +
      theme(legend.position = "bottom",
            legend.box = "horizontal"
            )
like image 28
user4670961 Avatar answered Sep 19 '22 02:09

user4670961