0 votes
1 view
in R Programming by (6.5k points)

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())

enter image description here

1 Answer

0 votes
by (25.3k points)

To move the legend to the bottom, 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")

Output:
image

Related questions

0 votes
1 answer
0 votes
1 answer
0 votes
1 answer
Welcome to Intellipaat Community. Get your technical queries answered by top developers !


Categories

...