R-背景色在ggplot上沿对角线分割?

R-背景色在ggplot上沿对角线分割?,r,ggplot2,background-color,R,Ggplot2,Background Color,使用R,库(ggplot2) 我有一个完美的正方形。从左上角到右下角,我有一条线 比如说xlim(0100),ylim(0100)和geom_abline(截距=100,斜率=1) 如何使线条上方的所有内容都具有蓝色背景和红色背景 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), f

使用R,库(ggplot2)

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

比如说xlim(0100),ylim(0100)和geom_abline(截距=100,斜率=1)

如何使线条上方的所有内容都具有蓝色背景和红色背景

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()

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

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()

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

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()

你是个巫师,@eipi10谢谢你的帮助。你是个巫师,@eipi10谢谢你的帮助。投票通过并被接受。