将蓝色以外的颜色应用于bin2d

use*_*207 4 r colors ggplot2

如何将不同于默认蓝调的颜色应用于geom_bin2d绘图?

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))
Run Code Online (Sandbox Code Playgroud)

但显然ggplot2只是在没有实际使用它的情况下为情节增加了第二个尺度.我打算更换默认比例......

rcs*_*rcs 6

你需要使用scale_fill_gradientn(),因为stat_bin2dfill审美:

p + scale_fill_gradientn(limits=c(0,50), breaks=seq(0, 40, by=10), colours=rainbow(4))
Run Code Online (Sandbox Code Playgroud)

scale_fill_gradientn