bee
bee

Reputation: 63

Legend title position using patchwork?

I've laid out 2 graphs using patchwork, and have a legend which describes them both at the bottom. The legend has a horizontal orientation, and I'm trying to move the title to the top, rather than its default on the left. When I use

guides(
fill = guide_legend(title.position = "top")
) 

the (continuous) legend is converted into a discrete legend. Is there an easy way to prevent this from happening?

Upvotes: 0

Views: 1419

Answers (1)

Allan Cameron
Allan Cameron

Reputation: 173813

In ggplot, a guide_legend implies that you want discrete legend keys. I think you are looking for guide_colorbar.

To demonstrate, let's recreate your problem. First, the original plot:

library(ggplot2)

set.seed(69)
df <- data.frame(x = 1:10, y = sample(10), z = 1:10)

p <- ggplot(df, aes(x, y, fill = z)) + 
      geom_col() + 
      theme(legend.position = "bottom")

p

Now, the code you are using that causes a problem:

p + guides(fill = guide_legend(title.position = "top"))

And the code that resolves it:

p + guides(fill = guide_colorbar(title.position = "top"))

Created on 2020-08-07 by the reprex package (v0.3.0)

Upvotes: 4

Related Questions