Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

To adjust the scale ticks and breaks to percentiles in a continuous colorbar using scale_fill_gradientn in ggplot2, you can use the limits and the breaks arguments.

The limits argument sets the minimum and maximum values of the scale. By default, it uses the minimum and maximum values of the data. You can set it to the percentiles that you want to use, for example:

limits = c(5, 95) 

The breaks argument sets the positions of the ticks in the scale. By default, it uses a sequence of numbers between the minimum and maximum values of the data. You can set it to percentiles as well, using the quantile function, for example:

breaks = seq(0, 1, by = 0.1) # or breaks = quantile(data, probs = seq(0, 1, by = 0.1)) 

You can combine both arguments in the scale_fill_gradientn function, like this:

ggplot(data, aes(x, y, fill = z)) + geom_point() + scale_fill_gradientn(colors = c("blue", "white", "red"), limits = c(5, 95), breaks = seq(0, 1, by = 0.1)) 

This will create a continuous colorbar with ticks and breaks at the 5th, 15th, 25th, ..., and 95th percentiles of the data.