从下图中,我希望将从绿色到红色(黄色部分)的过渡限制在红线上。当z<1时,我想用绿色表示,当z>1时用红色表示,并让黄色位于z=1的中心。
library(ggplot2)
Data(仅用于说明情节)
x=seq(0,5,length=1e2)
y=seq(0,5,length=1e2)
#
df=expand.grid(x=x,y=y)
df$z=df$x*df$y
情节
ggplot(data=df, aes(x=x,y=y,z=z,fill=z)) +
geom_raster()+
stat_contour(breaks=1, size=1, colour="red") +
scale_fill_gradientn (colours=colorRampPalette (c ("green", "yellow","red")) (20))
感谢您的帮助