R ggplot2 does not skip color for range if no data

I have a dataframe with columns:

X   Y   VALUE

I am trying to display colors according to ranges of values, 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 the package base to create a coefficient on the dataframe according to the ranges (as suggested at 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 draw 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 problem that I am facing is that if there is no data in any particular range, then the graph colors the next available range with its color. Therefore, in my case, if there is no data for 0 to 2, then the graph paints from 2 to 5 as blue, if there is data for the range from 2 to 5. Can you suggest?

0
source share

Source: https://habr.com/ru/post/1599863/


All Articles