Ask Your Question
1

How can the scale ticks and breaks be adjusted to percentiles in a continuous colorbar using scale_fill_gradientn in ggplot2?

asked 2023-06-04 12:25:47 +0000

qstack gravatar image

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
0

answered 2023-06-04 12:53:02 +0000

woof gravatar image

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.

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account. This space is reserved only for answers. If you would like to engage in a discussion, please instead post a comment under the question or an answer that you would like to discuss

Add Answer


Question Tools

Stats

Asked: 2023-06-04 12:25:47 +0000

Seen: 17 times

Last updated: Jun 04 '23