在ggplot2中使用scale_colour_gradient(low =“red”,high =“blue”)创建对称色阶

时间:2012-12-01 16:28:20

标签: r ggplot2

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

生成一个看起来大部分为红色的颜色渐变。我期望red-violet-blue在中间有紫罗兰,但我得到了red-still_red-violet-blue

red-still_red-violet-blue

如何获得紫罗兰居中的渐变?

2 个答案:

答案 0 :(得分:8)

scale_colour_gradientn(colours=c("red","violet","blue"))

答案 1 :(得分:7)

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)

并且如评论中所述,使用scale_colour_gradient2

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

enter image description here