2014-11-02 41 views
-1

我在R中遇到問題,我非常感謝您的幫助。在R中繪製不等式

我繪製的可行性區域以下約束:

constrains: 
5*x + 3*y >= 210 
x + y <= 110 
4*x + y <= 200 

任何想法?另外我需要着色可行的。

+2

看看http://stackoverflow.com/questions/15385063/easiest-way-to-plot-inequalities-with-hatched-fill – NPE 2014-11-02 09:50:16

回答

2

一種方法,除了評論中的鏈接,是使用geom_ribbon特別足以繪製線性約束區域。

enter image description here

library(ggplot2) 

fun1 <- function(x) 210/3 -5/3*x 
fun2 <- function(x) 110 -x 
fun3 <- function(x) 200 -4*x 
x1 = seq(-5,100) 
mydf = data.frame(x1, y1=fun1(x1), y2=fun2(x1),y3= fun3(x1)) 
mydf <- transform(mydf, z = pmax(y1,pmin(y2,y3))) 
ggplot(mydf, aes(x = x1)) + 
    geom_line(aes(y = y1), colour = 'blue') + 
    geom_line(aes(y = y2), colour = 'green') + 
    geom_line(aes(y = y3), colour = 'red') + 
    geom_ribbon(aes(ymin=y1,ymax = z), fill = 'gray60')