R - ggplot 上的背景颜色沿对角线分开?

Jib*_*ril 1 r background-color ggplot2

使用 R,库(ggplot2)

我有一个完美的正方形 ggplot。从左上角到右下角,我有一条线。

所以说 xlim(0,100), ylim(0,100) 和 geom_abline(intercept=100,lope=-1)。

我如何使线条上方的所有内容都说蓝色背景和红色下方?

eip*_*i10 5

library(ggplot2)

dat = data.frame(x=c(0,100), ymin=0, y=c(100,0), ymax=100)

ggplot(dat) +
  geom_ribbon(aes(x, ymin=y, ymax=ymax), fill="blue") +
  geom_ribbon(aes(x, ymin=ymin, ymax=y), fill="red") +
  geom_abline(aes(intercept=100, slope=-1)) +
  #geom_line(aes(x,y), lwd=1) +  # If you just want a line with the same extent as the data
  theme_bw()
Run Code Online (Sandbox Code Playgroud)

在此处输入图片说明

类似地,对于更一般的曲线:

x=seq(0,100,0.1)
dat = data.frame(x=x, ymin=0, y=2*x + 3*x^2 - 0.025*x^3)
dat$ymax=max(dat$y)

ggplot(dat) +
  geom_ribbon(aes(x, ymin=y, ymax=ymax), fill="blue") +
  geom_ribbon(aes(x, ymin=ymin, ymax=y), fill="red") +
  geom_line(aes(x,y), colour="yellow", lwd=1) +
  theme_bw()
Run Code Online (Sandbox Code Playgroud)

在此处输入图片说明