4

I make some plot with ggplot2 like this:

library(ggplot2)
ggplot(df, aes(x, y, col= col)) +
  geom_point()

Okay

Everything is good but as soon I set another legend title as shown here, the continuous colour gradient changes to some points in the legend:

library(ggplot2)
ggplot(df, aes(x, y, col= col)) +
  geom_point() +
  guides(col= guide_legend(title= "Some title"))

Why

How can I change just the title not the eastethic in the legend?

Data used here:

df <- data.frame(x= 1:10, y= 1:10, col= rnorm(10))
LulY
  • 976
  • 1
  • 9
  • 24

1 Answers1

8

You need guide_colourbar instead of guide_legend

ggplot(df, aes(x, y, col = col)) +
  geom_point() +
  guides(col = guide_colourbar(title = "Some title"))

Though personally, I would normally just change the label of the color aesthetic:

ggplot(df, aes(x, y, col = col)) +
  geom_point() +
  labs(color = "Some title")

Which gives the same result with fewer keystrokes.

Allan Cameron
  • 147,086
  • 7
  • 49
  • 87