Legend ggplot2 to the bottom and horizontal

How can I move the ggplot2 legend to the bottom of the graph and rotate it horizontally?

Code example:

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) result: enter image description here

+57
r ggplot2
Apr 05 2018-12-12T00:
source share
2 answers

If you want to move the legend position, 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 will give you the desired result. Legend at bottom

+70
Apr 05 2018-12-12T00:
source share

This does not give you exactly what you are asking for, but at least put the colors together:

 +theme(legend.position="bottom",legend.direction="vertical") 
+16
Jun 20 '15 at 23:05
source share



All Articles