Applying colours other than blue to bin2d

How can I apply colours different from the default blues to geom_bin2d plots?

library(ggplot2)
x <- rnorm(100000)
y <- rnorm(100000)
df <- data.frame(x,y)
p <- ggplot(df, aes(x, y))
p <- p + stat_bin2d(bins = 200)
p + scale_colour_gradientn(limits=c(0,50), breaks=c(0,10,20,30,40), colours=rainbow(4))

but apparently ggplot2 just adds a second scale to the plot without actually using it. I intended to replace the default-scale...

0

1 Answer

You need to use scale_fill_gradientn(), since stat_bin2d has a fill aesthetic:

p + scale_fill_gradientn(limits=c(0,50), breaks=seq(0, 40, by=10), colours=rainbow(4))

scale_fill_gradientn

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

You Might Also Like