Color gradient with ggplot

Hello,

my scale_color_gradient displays a rainbow from low=green to high=red but I want this to be mapped to a variable z (that I have) between 0 and 1. The problem is that even if z does not reach 1, I still get the whole rainbow. I want the rainbow to stop at orange for example if z stops at 0.5. Can you help me with that ?
Thanks.

geom_segment(aes(xend = x, yend = 0, colour = z)) +
scale_color_gradient(low = "green", high = 'red')

use the limits param to tell the scale that the limits are from 0 to 1 (even though you are showing a z which is 0 to .5)

Thanks, that works. Now, the legend displays unused colours. Is there a way to have a legend adapted to the graph ? Below, there is no red on the graph but there is on the legend. I would want it to range from 0 to 75 approximately as you can see. Thanks again.

It seems that its easier to not change the limit; but rather change red to be a particular shade of reddish orange that you want.

Actually, I cannot do this. This graph will run on a Shiny app and the parameters that provide this plot will change. So you could imagine graphs with green as the 'maximum' colour. If you tell me this is not possible to have a legend that adapts to the maximum value of my variable z, that's fine.

It seems the function colorRamp(c("green","red")) maps any number between 0 and 1 to a color between green (0) and red (1). So I thought this was possible.

Ok, I finally manage to do so thanks the the function colorRamp2 from the package 'circlize'. I just set as limits min(z) and max(z) and it works just fine. Thanks again. I valid your answer to close this topic.

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.