【发布时间】:2016-10-19 13:37:09
【问题描述】:
我必须在 R 中绘制约束,这对我来说很新。请有人给我一些帮助。
x1 + 2*x2
3x1 + x2
非常感谢
【问题讨论】:
-
您还应该根据变量之一指定约束的限制。
标签: r constraints linear-programming constraint-programming
我必须在 R 中绘制约束,这对我来说很新。请有人给我一些帮助。
x1 + 2*x2
3x1 + x2
非常感谢
【问题讨论】:
标签: r constraints linear-programming constraint-programming
您可以使用geom_polygon 对约束集进行着色。使用线性约束,约束集的边缘很容易计算。
library(ggplot2)
ggplot(data_frame(x = c(0, 100)), aes(x = x)) +
stat_function(fun = function(x) {(100 - x)/2}, aes(color = "Function 1")) +
stat_function(fun = function(x) {(75 - 3*x) }, aes(color = "Function 2")) +
theme_bw() +
scale_color_discrete(name = "Function") +
geom_polygon(
data = data_frame(
x = c(0, 0, 100, Inf),
y = c(0, 50, 0, 0)
),
aes(
x = x, y = y, fill = "Constraint 1"
),
inherit.aes = FALSE, alpha = 0.4
) +
geom_polygon(
data = data_frame(
x = c(0, 0, 25, Inf),
y = c(0, 75, 0, 0)
),
aes(
x = x, y = y, fill = "Constraint 2"
),
inherit.aes = FALSE, alpha = 0.4
) +
scale_fill_discrete(name = "Constraint Set") +
scale_y_continuous(limits = c(0, 100))
这给出:
使用stat_function。这是一个简单的例子:
ggplot(data_frame(x = c(0, 100)), aes(x = x)) +
stat_function(fun = function(x) {(100 - x)/2}, aes(color = "Function 1")) +
stat_function(fun = function(x) {(75 - 3*x) }, aes(color = "Function 2")) +
theme_bw() +
scale_color_discrete(name = "Function")
【讨论】: