How to control the interval and size of continuous legend bar

I use ggplot2 to display data, and use the function "scale_fill_distiller" to display continuous color bar. However, I want to increase the interval from every 100 to every 200 in the color bar, how to do this? The target variable is annual precipitation, so from 0 to 2000 mm.

Also, how to increase the length of the color bar so that the numbers will be displayed clearly. I display the color bar as horizontal rather than vertical, so that the numbers such as 100, 200, 300, etc. are clumped together and hard to differentiate. Thanks for your help

p+ scale_fill_distiller('pr',palette='Spectral')

Hey @Ellenz, would you please provide a reproducible example? If you wonder how, take a look at the following :slight_smile:

1 Like

My question is similar to this post, but how to increase the interval of the color bar display? Thanks.

For example, if the color bar is displayed as in the figure, how to have the interval of 2 m/s, or 0.5 m/s, rather than every 1 m/s? Thanks very much.%E6%8D%95%E8%8E%B7

Do you mean like this?

library(ggplot2)
df <- data.frame(x = c(1,2,1,2), y = c(1,1,2,2), COL = c(100, 250, 400, 550))
ggplot(df, aes(x, y, fill = COL)) + geom_tile() + theme_classic() +
  scale_fill_distiller('pr',palette='Spectral')


ggplot(df, aes(x, y, fill = COL)) + geom_tile() + theme_classic() +
  scale_fill_distiller('pr',palette='Spectral', breaks = c(200, 400))

Created on 2019-04-23 by the reprex package (v0.2.1)

1 Like

Actually, I have another question based on this.
Suppose I have two datasets, with similar coordinates but different rainfall values. In df1, as the dataset you created, the values vary from 100 to 550. While in df2, the values vary from 50 to 800. Then I use the same scale_fill_distiller in order to put the two figures on one panel. The code is shown below:

fill_com = scale_fill_distiller('pr',palette='Spectral', breaks = c(200, 400))
figure1= ggplot(df1, aes(x, y, fill = COL)) + geom_tile() + theme_classic() + fill_com

figure2= ggplot(df2, aes(x, y, fill = COL)) + geom_tile() + theme_classic() + fill_com

ggarrange(figure1, figure2, ncol=2, nrow=1, common.legend = TRUE, legend="bottom")
But I think the color bar depends on the dataframe with the lowest range (550 in this case, rather than 800), such as df1. Thus, the high values in df2 does not show on the color bar. How to solve that? Thanks again.

There is a limits argument for scale_fill_distiller.

ggplot(df, aes(x, y, fill = COL)) + geom_tile() + theme_classic() +
  scale_fill_distiller('pr',palette='Spectral', 
                       breaks = c(200, 400, 600), limits = c(0,800))
1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.