Home > Blockchain >  Set continuous colour scale in ggplot2
Set continuous colour scale in ggplot2

Time:04-02

I the following code:

sa_map3_plt <- OpenStreetMap::autoplot.OpenStreetMap(sa_map2)   
geom_point( data = XY_ClassHF15_df, aes(x = x, y = y,colour= HF), size =  2.5  )  
#scale_colour_gradientn(colours = terrain.colors(18))                  
            scale_colour_gradient(low = "blue", high = "red", na.value = NA)
            xlab("Longitude (°E)")   ylab("Latitude (°S)" ) 
sa_map3_plt

which generates the following figure:

enter image description here

What I would like to do though, is to change the colour pallete, so that it looks like the figure below:

enter image description here

Basically, the low and high values will remain (almost) the same, but the intermediate colours should be different. I guess there is a manual way to do it, but I havent managed yet to do it

CodePudding user response:

Obviously we don't have your data, but using a simple random example should show the options here.

df <- data.frame(x = rnorm(10000), y = rnorm(10000), z = runif(10000))

Firstly, you could try scale_color_distiller with palette = "Spectral"

ggplot(df, aes(x, y, color = z))  
  geom_point()  
  scale_color_distiller(palette = 'Spectral')

enter image description here

Another option is to specify a full palette yourself using scale_color_gradientn which allows for arbitrary gradients. This one is a reasonable match for the scale in your example image.

ggplot(df, aes(x, y, color = z))  
  geom_point()  
  scale_color_gradientn(colours = c('#5749a0', '#0f7ab0', '#00bbb1',
                                    '#bef0b0', '#fdf4af', '#f9b64b',
                                    '#ec840e', '#ca443d', '#a51a49'))

enter image description here

  • Related