我将直线作为约束,并寻求对可行区域进行阴影处理。
我已经使用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)发布于 2014-03-12 14:13:08
所以这里有一种不需要计算角点的方法:

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")使用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)

发布于 2014-03-12 08:30:25
使用polygon绘制多边形并对其进行着色。例如:
plot(c(0, 3), c(0, 3), type = 'n')
x <- c(1, 2, 2, 1) # The x-coordinate of the vertices
y <- c(2, 1, 2, 1) # The y-coordinate of the vertices
polygon(x, y, col = 'grey')将创建一个领结形状的多边形并将其着色为灰色。
https://stackoverflow.com/questions/22338145
复制相似问题