0

There is a function within ggplot2 in R, scale_color_gradient, where you can specify a "low" colour and a "high" colour, within a set limit, and your numbers are coloured according to that scale.

eg.

scale_color_gradient(low="red", high="blue", limits = c(0, 1))

Will colour numbers = 0 as red, and numbers = 1 as blue, and numbers in between there a colour which is inbetween red and blue (the number determining the colour).

My question is - given a single number (eg. 0.5), is it possible to assign a colour to this number based on that same scale?

icedcoffee
  • 935
  • 1
  • 6
  • 18
  • 2
    If you want to specify more than two colors, use `scale_colour_gradientn`. (note the `n`). It's easier to help you if you include a simple [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) with sample input and desired output that can be used to test and verify possible solutions. – MrFlick Aug 11 '20 at 21:17
  • FYI, this is a good reason *against* using rdocumentation-dot-org for a reference: their help-pages are quite out of date, here is no exception: it's giving you `ggplot2-1.0.0`, which was current over six years ago. Assuming you have a version of `ggplot2` a little less antiquated than that, for this package you should go straight to https://ggplot2.tidyverse.org/reference/scale_gradient.html (which includes `scale_colour_gradientn` as @MrFlick recommended). – r2evans Aug 11 '20 at 21:39

1 Answers1

3

As others mentioned, you can use scale_color_gradientn() to specify a gradient of n number of colors and make your own gradient. You can check the documentation on the function for more information, but here's a working example.

library(ggplot2)
set.seed(1234)
df <- data.frame(
  x=1:200,
  y=rnorm(200, 1, 0.2),
  cols=sample(1:10, 200, replace = TRUE)
)

p <- ggplot(df, aes(x,y, color=cols)) + geom_point()
p + scale_color_gradientn(colors=c('blue','green','yellow','red'))

enter image description here

That looks great, but what if you wanted to (as you mentioned in your case), specify a particular position for the colors? In this case, let's say we wanted the df$cols==5.0 to be yellow.

You can do that by supplying a vector to the values= argument. However, there is a bit of a trick to this that is not obvious from the documentation. The values= argument expects a vector to indicate the positions of each of your colors= on a scale from 0 to 1. Note how we have our df$cols ranging from 0 to 10, so that means that df$cols==5.0 is represented by values=0.5.

p + scale_color_gradientn(
    colors=c('blue','green','yellow','red'),
    values=c(0,0.2,0.5,1)
  )

enter image description here

chemdork123
  • 12,369
  • 2
  • 16
  • 32
  • Just as a small note: when you take this approach I'd also recommend setting the limits manually as a sanity check. In your example, the data ranges from 1-10 instead of 0-10, so the yellow occurs at 5.5 instead of 5. – teunbrand Aug 11 '20 at 22:45