Ioan-Florian Damian
Ioan-Florian Damian

Reputation: 11

Plot inequalities in R

I have a problem in R and I would greatly appreciate your help.

I have to plot the feasibility area for the following constrains:

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

Any ideas? Also i need to color the feasible are.

Upvotes: 0

Views: 5545

Answers (1)

agstudy
agstudy

Reputation: 121568

One way , in addition to the link in the comment is to use geom_ribbon specially adequate to plot region with a linear constraints.

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

Upvotes: 5

Related Questions