Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Changing legend title in ggplot changes the shown legend aesthetic

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:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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

>Solution :

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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading