133

How can I move a ggplot2 legend to the bottom of the plot and turn it horizontally?

Sample code:

library(reshape2) # for melt
df <- melt(outer(1:4, 1:4), varnames = c("X1", "X2"))
p1 <- ggplot(df, aes(X1, X2)) + geom_tile(aes(fill = value))
p1 + scale_fill_continuous(guide = guide_legend())

Desired (approximate) outcome: enter image description here

AndrewGB
  • 16,126
  • 5
  • 18
  • 49
Tyler Rinker
  • 108,132
  • 65
  • 322
  • 519
  • 6
    After 7 years and 8 months, I finally figured out how to get the desired outcome for this question :) Scroll down to the second answer. – Arthur Yip Dec 07 '19 at 22:37

2 Answers2

178

If you want to move the position of the legend please use the following code:

library(reshape2) # for melt
df <- melt(outer(1:4, 1:4), varnames = c("X1", "X2"))
p1 <- ggplot(df, aes(X1, X2)) + geom_tile(aes(fill = value))
p1 + scale_fill_continuous(guide = guide_legend()) +
    theme(legend.position="bottom")

This should give you the desired result. Legend at bottom

Tyler Rinker
  • 108,132
  • 65
  • 322
  • 519
Shreyas Karnik
  • 3,953
  • 5
  • 27
  • 26
  • 2
    do you know if it possible to draw a continuous legend bar on the bottom? (so not with the number in between but on top). thanks. – Janvb Aug 23 '12 at 08:29
  • 3
    With current `ggplot`, this gives me the warning `'opts' is deprecated. Use 'theme' instead. (Deprecated; last used in version 0.9.1)`. Replacing `opts` by `theme` works. – krlmlr Apr 10 '13 at 22:33
  • Yes I anticipate there is change in the internal workings of `ggplot` – Shreyas Karnik Apr 10 '13 at 23:17
  • 11
    It is bad practice to use depreciated items. You can do it using theme the exact same way: `+ theme(legend.position='bottom')` – Omar Wagih Sep 26 '13 at 14:42
  • unfortunately there is some ambiguity when the numbers and colors are side-by-side; see my answer below at several attempts to improve on this. – Arthur Yip Dec 07 '19 at 21:59
  • also see https://stackoverflow.com/questions/50883294/increasing-whitespace-between-legend-items-in-ggplot2/50885122#50885122 https://stackoverflow.com/questions/42401568/r-ggplot-add-space-between-legend-items https://stackoverflow.com/questions/51780836/spacing-between-legend-keys-in-ggplot https://stackoverflow.com/questions/38402427/space-between-gpplot2-horizontal-legend-elements to avoid the spacing problem. – Arthur Yip Dec 07 '19 at 22:19
60

Here is how to create the desired outcome:

library(reshape2); library(tidyverse)
melt(outer(1:4, 1:4), varnames = c("X1", "X2")) %>%
ggplot() + 
  geom_tile(aes(X1, X2, fill = value)) + 
  scale_fill_continuous(guide = guide_legend()) +
  theme(legend.position="bottom",
        legend.spacing.x = unit(0, 'cm'))+
  guides(fill = guide_legend(label.position = "bottom"))

Created on 2019-12-07 by the reprex package (v0.3.0)


Edit: no need for these imperfect options anymore, but I'm leaving them here for reference.

Two imperfect options that don't give you exactly what you were asking for, but pretty close (will at least put the colours together).

library(reshape2); library(tidyverse)
df <- melt(outer(1:4, 1:4), varnames = c("X1", "X2"))
p1 <- ggplot(df, aes(X1, X2)) + geom_tile(aes(fill = value))
p1 + scale_fill_continuous(guide = guide_legend()) +
 theme(legend.position="bottom", legend.direction="vertical")

p1 + scale_fill_continuous(guide = "colorbar") + theme(legend.position="bottom")

Created on 2019-02-28 by the reprex package (v0.2.1)

Arthur Yip
  • 5,810
  • 2
  • 31
  • 50
  • Whilst this may theoretically answer the question, [it would be preferable](//meta.stackoverflow.com/q/8259) to include the essential parts of the answer here, and provide the link for reference. – Rohit Gupta Jun 20 '15 at 23:23