Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Scale color gradient and AND outside the limits [duplicate]

Tags:

r

ggplot2

I used this Question (ggplot scale color gradient to range outside of data range) to create a gradient within a specific range but now I have the problem, that all colors outside these limits are grey. I mean, it is logic to me but I wanted to have those to have the same color as the closest point.

ggplot(data.frame(a=1:10), aes(1, a, color=a)) + 
  geom_point(size=6) + 
  scale_colour_gradientn(colours=c('red','yellow','green'), limits=c(2,8))

So the top points should be green and the lower point should be red as well. So what I am looking for is a range like limits=c(<2,>8) but I know this is not working

like image 727
drmariod Avatar asked Dec 24 '22 09:12

drmariod


1 Answers

You can specify how to deal with out-of-bounds values via argument oob; for example, scales::squish "squishes" values into the range specified by limits:

ggplot(data.frame(a=1:10), aes(1, a, color = a)) +
    geom_point(size = 6) +
    scale_colour_gradientn(
        colours=c('red','yellow','green'), 
        limits=c(2,8), 
        oob = scales::squish);

enter image description here

like image 111
Maurits Evers Avatar answered Dec 28 '22 08:12

Maurits Evers