Reputation: 473
I'm trying to use scale_colour_gradientn to clearly show neg vs pos values: negative is blue, positive is red. But gradientn seems to ignore my vector of colors, and just makes one big gradient instead of n small gradients. Here's my sample code ...
xx <- -100:100
yy <- xx
fma <- data.frame( xx, yy)
ggplot( fma) +
geom_point( aes( xx, yy, col=yy)) +
scale_colour_gradientn(
colours=c('#0000ff','#0000ff','#ff0000','#ff0000'),
values=c( -100, -1, 1, 100))
How can I convince gradientn to color everything in [-100,-1] blue and everything in [1,100] red?
Upvotes: 2
Views: 4064
Reputation: 666
if you'd like to keep the color scale as a gradient, instead of a dichotemous scale, you could do this:
xx <- -100:100
yy <- xx
fma <- data.frame( xx, yy)
ggplot( fma) +
geom_point( aes( xx, yy, col=yy)) +
scale_colour_gradientn(
colours=c('indianred4','indianred1','white','darkseagreen1','darkseagreen4'),
values=c( 0, 49.5, 50, 50.5, 100)/100)
the values-argument only takes values between 0 and 1, which means that in your case, -100 is equal to 0, and 100 is 1.
I have added the color white, which corresponds to ~0, for neutrality. This white range could be expanded, by setting .495 to something like .40, and .50 to .50
also notice that I'm making a vector for the values argument without decimals, for convenience and readability, and then dividing it with a 100, to rescale it so the values argument can use it.
Upvotes: 5
Reputation: 12155
Rather than use scale_color_continuous
, I'd make a factor
variable that tells what color to color those points and use scale_color_discrete
or *_manual
. In this example, we use a conditional in the aes
so col
receives either TRUE
or FALSE
depending on the value of yy
, but if you wanted more possible values, you could use switch
or case_when
to populate a yy_range
variable with the possible color categories.
ggplot(fma) +
geom_point(aes( xx, yy, col = yy < 0)) +
scale_color_discrete(c('#0000ff','#ff0000'))
Upvotes: 1