4

I have a dataframe with columns X , Y , VALUE. I am using ggplot2 to plot the values at the specified coordinates. But I wish to fill colour based on custom ranges for the VALUE column. For example :

VALUE    COLOUR
0 to 2    blue
2 to 5    green
5 to 10   red
10 to 20  yellow
20 to 30  orange
30 to 40  grey
>40       black

How do I fill specific colour for particular values using ggplot2 ?

I can resample my data if required and map individual resampled value to colours after that, for example :

VALUE    RESAMPLED VALUE    COLOUR
0 to 2        10             blue
2 to 5        20             green
5 to 10       30             red
10 to 20      40             yellow
20 to 30      50             orange
30 to 40      60             grey
>40           70             black

But the values are not getting mapped to colours with this code :

ggplot2::ggplot() + ggmap::theme_nothing() +
ggplot2::geom_tile(data = xyDataFrame, alpha = 0.6, aes(x = X, y = Y, fill=VALUE)) + 
ggplot2::scale_fill_gradientn(colours = c("blue", "green", "red", "yellow", "orange", "grey", "black"))
Harsh Pensi
  • 113
  • 7

1 Answers1

5

You should probably use cut to make an additional variable in your data frame.

DATA$COLOUR <- cut(DATA$VALUE, c(0, 2, 5, 10, 20, 30, 40, Inf),
    c("blue", "green", "red", "yellow", "orange", "grey", "black"))
Benjamin
  • 16,897
  • 6
  • 45
  • 65
  • Thanks for the reply Benjamin. After using the cut function as in your comment i tried plotting the following : ggplot2::geom_tile(data = xyDataFrame, alpha = 0.6, aes(x = x, y = y, fill=COLOUR)) This seems to have coloured the plot according to the correct ranges, but the colors are not the ones I intend but some other set of colours have been put on the plot. – Harsh Pensi Jul 27 '15 at 12:06
  • 1
    `ggplot2::ggplot() + ggmap::theme_nothing() + ggplot2::geom_tile(data = xyDataFrame, alpha = 0.6, aes(x = X, y = Y, fill=COLOUR))` Is this the correct usage after the cut function – Harsh Pensi Jul 27 '15 at 12:14
  • Oh. right. This bites me every time. `ggplot` is looking at `COLOUR` as an aesthetic. You will also need to change the fill scale. `scale_fill_manual(value = c("blue", "green", "red", "yellow", "orange", "grey", "black"))` should get you in the right ball park. You may have to rearrange the order of colors depending on how `cut` ordered your factor. – Benjamin Jul 27 '15 at 12:17
  • Awesome. Works perfectly !!! Thanks a lot :) `ggplot2::ggplot() + ggmap::theme_nothing() + ggplot2::geom_tile(data = xyDataFrame, alpha = 0.6, aes(x = x, y = y, fill=COLOUR)) + scale_fill_manual(values = c("blue", "green", "red", "yellow", "orange", "grey", "black"))` – Harsh Pensi Jul 27 '15 at 12:31
  • Facing an issue with this solution. If there is no data in any particular range, then the plot is colouring the next available range with its colour. So in my case if there is no data for 0 to 2 , then plot is colouring 2 to 5 as blue. – Harsh Pensi Jul 29 '15 at 14:03
  • I know there's a solution to this, but I don't know it off the top of my head. I'd recommend searching for a similar question (I'm sure it's been answered before), or submitting a new question. – Benjamin Jul 29 '15 at 14:30
  • Posted another - http://stackoverflow.com/questions/31716345/r-ggplot2-not-skipping-color-for-a-range-if-there-is-no-data – Harsh Pensi Jul 30 '15 at 06:16