How to include more decimals in ggplot's gradient color scale bar?

Viewed 300

I am plotting x and y based on coordinates, the color is a third continuous variable with 6 decimals.

ggplot(data, aes(x=x, y=y, color=continuous_variable)) +
  geom_point()

Currently, the color is creating the gradient based on 3 decimals but I would like it to show 5.

Is there a way to increase the number of decimal places ggplot uses to create the gradient?

1 Answers

You can pass a function that creates the labels from the breaks. Here I use sprintf to create character strings with the desired number (5) of decimals:

library(ggplot2)

ggplot(mtcars, aes(mpg, wt, colour = cyl)) + geom_point()+ 
  scale_color_gradient(labels = function(x) sprintf("%.5f", x))
Related