用R中的abline对可行区域进行着色

use*_*737 1 plot r region

我有直线作为约束,我试图遮蔽可行区域.

我使用abline绘制我的线条,但我不能在多边形中遮挡.这就是我到目前为止所拥有的.

我是R的新手

plot(c(0, 2), c(0, 2), type='n')

abline(-1/4, 6)
abline(1/2,7)
abline(2,-8)
abline(1,-3)
Run Code Online (Sandbox Code Playgroud)

jlh*_*ard 5

所以这是一种不需要计算角落的方法:

plot(c(0, .5), c(0, 2), type='n')
abline(-1/4, 6, lty=2)
abline(1/2,7, lty=2)
abline(2,-8, lty=2)
abline(1,-3, lty=2)

conditions <- function(x,y) {
  c1 <- (y > -1/4 + 6*x)
  c2 <- (y <  1/2 + 7*x)
  c3 <- (y <    2 - 8*x)
  c4 <- (y >    1 - 3*x)
  return(c1 & c2 & c3 & c4)
}

x <- seq(0,0.5,length=1000)
y <- seq(0,2,length=1000)
z <- expand.grid(x=x,y=y)
z <- z[conditions(z$x,z$y),]
points(z, col="red")
Run Code Online (Sandbox Code Playgroud)

和使用相同的东西 ggplot

library(ggplot2)
ggplot(z, aes(x,y))+geom_point(colour="red", alpha=.5)+
  geom_abline(intercept=-1/4,slope=6,  linetype=2)+
  geom_abline(intercept=1/2, slope=7,  linetype=2)+
  geom_abline(intercept=2,   slope=-8, linetype=2)+
  geom_abline(intercept=1,   slope=-3, linetype=2)+
  xlim(0,0.5)+ylim(0,2)
Run Code Online (Sandbox Code Playgroud)