0

I have a dataframe with columns :

X   Y   VALUE

I am trying to plot colors according to value ranges for example :

VALUE RANGE        COLOR
 0 to 2            blue
 2 to 5            green
 5 to 10           red
 10 to 20          yellow
 20 to 30          orange
 > 30              grey

I am using cut from package base to create factor on the dataframe according to ranges (as suggested on http://goo.gl/W1EJzI) :

xyDataFrame$COLOUR <- cut(xyDataFrame$VALUE, c(0, 2, 5, 10, 20, 30,  Inf), c("blue", "green", "red", "yellow", "orange", "grey"))

And I am plotting using the following :

ggplot2::ggplot() + ggmap::theme_nothing() +
ggplot2::geom_tile(data = xyDataFrame, alpha = 0.6, aes(x = X, y = Y, fill=COLOUR)) +
ggplot2::scale_fill_manual(values = c("blue", "green", "red", "yellow", "orange", "grey"))

The issue I am facing is that 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 if there is data for 2 to 5 range. Can you please suggest ?

Jaap
  • 81,064
  • 34
  • 182
  • 193
Harsh Pensi
  • 113
  • 7
  • 1
    Could you please post a complete example? Also, there's a [tag:ggplot2] tag. – krlmlr Jul 30 '15 at 06:23
  • 1
    just add `drop=FALSE` to scale_fille_manual. See these questions : http://stackoverflow.com/questions/10834382/ggplot2-keep-unused-levels-barplot ; http://stackoverflow.com/questions/10002627/ggplot2-0-9-0-automatically-dropping-unused-factor-levels-from-plot-legend – scoa Jul 30 '15 at 07:48
  • Great. Works fine. Thanks :) – Harsh Pensi Jul 30 '15 at 08:37

0 Answers0