ggplot2geom-point

How to increase the range of colors in a the geom_point?


I am trying the following code to make a heat map using ggplot

library(RColorBrewer)
color_palette <- brewer.pal(9, "Spectral")

ggplot(df, aes(x=x, y=y)) +
  geom_point(aes(color=signal_density_local), size = 0.05, alpha = .2) +
  scale_x_continuous(breaks = seq(0, 12, by = 0.5)) +
  scale_color_gradientn(colors = colorRampPalette(color_palette)(10))

This gives me this plot

enter image description here

Is there a way I can just increase the range of colors in my geom_point?


Solution

  • I'm going to give unsollicited advice that it is probably a bad idea to have a very wide gamut of hues in your colour palette, mostly because it has perceptual problems.

    That said, here is how you could use basically all the colours of the rainbow. Notice that it is very difficult to tell apart e.g. 15 from 20 or even 10 from 35. Moreover, the most common form of colour blindness is red-green blindness, so the entire lower half of your palette is practically indistinguishable (for further consideration, see https://en.wikipedia.org/wiki/Color_blindness#/media/File:Color_blindness.svg).

    library(ggplot2)
    
    p <- ggplot(mpg, aes(displ, hwy, colour = cty)) +
      geom_point()
    
    p + scale_colour_gradientn(colours = rainbow(20))
    

    However, the rainbow-esque palette with the least perceptual problems is probably the 'turbo' palette.

    p + scale_colour_viridis_c(option = "turbo")
    

    Created on 2024-04-12 with reprex v2.1.0