【问题标题】:How do I plot constraints in R如何在 R 中绘制约束
【发布时间】:2016-10-19 13:37:09
【问题描述】:

我必须在 R 中绘制约束,这对我来说很新。请有人给我一些帮助。

x1 + 2*x2

3x1 + x2

非常感谢

【问题讨论】:

  • 您还应该根据变量之一指定约束的限制。

标签: r constraints linear-programming constraint-programming


【解决方案1】:

更新:

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

您可以进行一些增强,例如为约束下的区域添加阴影,以及为约束线添加标签。

【讨论】:

    猜你喜欢
    • 2022-11-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-27
    • 1970-01-01
    • 2012-12-08
    • 1970-01-01
    相关资源
    最近更新 更多