Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create a symmetric colour scale with scale_colour_gradient(low="red", high="blue") in ggplot2

Tags:

r

ggplot2

scale_colour_gradient(low="red", high="blue")

generates a colour gradient that looks mostly red. I expected red-violet-blue with violet at in the middle but I got red-still_red-violet-blue

red-still_red-violet-blue

How can I get a gradient with violet centered?

like image 711
Jonas Stein Avatar asked Dec 01 '12 16:12

Jonas Stein


2 Answers

scale_colour_gradientn(colours=c("red","violet","blue"))
like image 86
metasequoia Avatar answered Oct 26 '22 21:10

metasequoia


library(ggplot2)

dsub <- subset(diamonds, x > 5 & x < 6 & y > 5 & y < 6)
dsub$diff <- with(dsub, sqrt(abs(x-y))* sign(x-y))
(d <- qplot(x, y, data=dsub, colour=diff))

max_val <- max(abs(dsub$diff))
values <- seq(-max_val, max_val, length = 3)

d + scale_colour_gradientn(colours=c("red","violet","blue"),
                           values = values, rescaler = function(x, ...) x, oob = identity)

and as said in comment , more simple with scale_colour_gradient2

 d + scale_colour_gradient2(low='red',mid='violet',high='blue')

enter image description here

like image 20
agstudy Avatar answered Oct 26 '22 22:10

agstudy