在 R 中绘制不等式 [英] Plot inequalities in R
本文介绍了在 R 中绘制不等式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在 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.
推荐答案
除了注释中的链接之外,还有一种方法是使用 geom_ribbon
特别适合绘制具有线性约束的区域.
One way , in addition to the link in the comment is to use geom_ribbon
specially adequate to plot region with a linear constraints.
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')
这篇关于在 R 中绘制不等式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文