15

I have a data frame like this

BP        R2      LOG10
96162057 0.2118000 2.66514431
96162096 0.0124700 0.31749391
96162281 0.0008941 0.07012148
96163560 0.5011000 2.48505399
96163638 0.8702000 3.37778598

and I want to plot BP against LOG10, and color the points by R2. R2 are continuous values from 0-1.

myplot <- read.cvs("mytable.csv",head=TRUE)
attach(myplot)
ggplot(myplot,aes(BP,LOG10, color=R2)) + geom_point() 

So far, so good. However I would like to display the R2 colors in manually selected intervals and colors, like this (if I had discrete values).

ggplot(myplot,aes(BP,LOG10, color=R2)) + geom_point() + 
      scale_color_manual(breaks= c("1","0.8","0.6","0.4","0.2","0"), 
                values = c("red","yellow","green","lightblue","darkblue"))
Error: Continuous value supplied to discrete scale

This looks pretty, but I would rather set the colors my self.

ggplot(myplot,aes(BP,LOG10, color=R2)) + geom_point(shape=1) + 
               scale_colour_gradientn(colours = rainbow(10))

So, how can I manually select intervals from continuous values (1-0.8, 0.8-0.6, 0.6-0.4, 0.4-0.2, 0.2-0), and color them to my liking (red, yellow, green, light, darkblue)? A smooth gradient between the colors would be cool, but not crucial.

Didzis Elferts
  • 95,661
  • 14
  • 264
  • 201
user2724998
  • 153
  • 1
  • 1
  • 4

2 Answers2

24

You can use scale_colour_gradientn() and then provide your own colours= and values=. Values will give intervals for each color.

ggplot(myplot,aes(BP,LOG10, color = R2)) + geom_point() + 
  scale_colour_gradientn(colours = c("red","yellow","green","lightblue","darkblue"),
                         values = c(1.0,0.8,0.6,0.4,0.2,0)) 

gradientn chart example

micstr
  • 5,080
  • 8
  • 48
  • 76
Didzis Elferts
  • 95,661
  • 14
  • 264
  • 201
  • Thanks a lot Didzis, that totally solves it! Do you also have a solution without gradients, so rather all values from 1-0.8 = red, 0.8-0.6 = yellow, and so on? – user2724998 Aug 29 '13 at 06:39
  • Then you should divide your data in intervals using cut() function and use those intervals as discrete values. – Didzis Elferts Aug 29 '13 at 06:50
  • How do I make this gradient across the 6 different values but only from blue to red? I would like to only provide "low" and "high" color and the intermediate levels are blended over accordingly. I'm talking of a discrete version of this: `scale_color_gradient(low="blue", high="red")`. I could only get it to work with a custom function but there must be an easier way: `color.gradient.discrete = function(color.low, color.high, n) { scales::seq_gradient_pal(low=color.low, high=color.high)(seq(0, 1, length.out = n)) }` – Mario Reutter Sep 15 '21 at 10:10
  • Expected outcome is illustrated [here](https://raw.githubusercontent.com/spressi/misc/main/discrete%20color%20gradient.png) – Mario Reutter Sep 15 '21 at 10:22
22

After 4 years a comment to this post: It is important to note that the "values" map your variable values between 0 and 1, 0 being the lowest and 1 the highest value. In this example it coincidentally fits to the values of the variable which are 0 to 1.

tjebo
  • 21,977
  • 7
  • 58
  • 94