在R中绘制约束条件。

huangapple go评论70阅读模式
英文:

plot constraints in R

问题

1- 0.20x1 + 0.70x2 <= 100
2- x1 + x2 <= 200

我想使用R绘制上述两个约束条件的图表:
我已经使用ggplot尝试生成了图表,但结果不正确,有些奇怪。以下是我的尝试和代码:

在R中绘制约束条件。

library(ggplot2)
plot_df <- as.data.frame(matrix(nrow=4, ncol=3))
colnames(plot_df) <- c("Budget", "Space", "type")
plot_df$Budget <- c(0, 143, 0, 500)
plot_df$Space <- c(200, 0, 200, 0)
plot_df$type <- c("line constraint1", "line constraint1", "line constraint2", "line constraint2")
ggplot(plot_df) + geom_path(mapping = aes(x = Budget, y = Space, color = type)) + ggtitle("Plot kale vs Parsnips Growing")

此外,是否有办法给可行区域上色,即两条线之间的公共区域?
谢谢

Note: The code provided in the original text appears to contain HTML entities (e.g., &lt;), which I have retained in the translation. If you want to use this code, you may need to replace these entities with their corresponding symbols in your R script.

英文:

I wanted to plot the two constraints below using R:

1- 0.20x1 + 0.70x2 <= 100

2- x1 + x2 <= 200

I have used ggplot, the plot is generated but it is not right. something weird I have. Here is my attempt and my code below

在R中绘制约束条件。


library(ggplot2)
plot_df &lt;- as.data.frame(matrix(nrow=4,ncol=3))
colnames(plot_df) &lt;- c(&quot;Budget&quot;,&quot;Space&quot;,&quot;type&quot;)
plot_df$Budget &lt;- c(0,143,0,500)
plot_df$Space &lt;- c(200,0,200,0)
plot_df$type &lt;- c(&quot;line constraint1&quot;,&quot;line constraint1&quot;, &quot;line constraint2&quot;, &quot;line constraint2&quot;)
ggplot(plot_df) + geom_path(mapping = aes(x=Budget,y=Space,color=type))+ ggtitle(&quot;Plot kale vs Parsnips Growing&quot;)

Also, is there a way I can shade the feasible area? the common area between the two lines?

Thank you

答案1

得分: 1

你可以使用stat_function来绘制函数作为连续曲线。在你的aes中,你可以为你的函数命名。下面是一些可复制的代码:

library(ggplot2)
plot_df = data.frame(Budget = c(0, 200))

ggplot(plot_df, aes(x = Budget)) + 
  stat_function(fun = \(x) {(-505 + 3.5*x)}, aes(color = "线性约束1")) + 
  stat_function(fun = \(x) {(198 + x) }, aes(color = "线性约束2")) +
  labs(y = "空间")

在R中绘制约束条件。

创建于2023-07-11,使用 reprex v2.0.2

英文:

You could use stat_function to draw functions as a continuous curve. In your aes, you can name your function. Here is some reproducible code:

library(ggplot2)
plot_df = data.frame(Budget = c(0, 200))

ggplot(plot_df, aes(x = Budget)) + 
  stat_function(fun = \(x) {(-505 + 3.5*x)}, aes(color = &quot;line constraint1&quot;)) + 
  stat_function(fun = \(x) {(198 + x) }, aes(color = &quot;line constraint2&quot;)) +
  labs(y = &quot;Space&quot;)

在R中绘制约束条件。<!-- -->

<sup>Created on 2023-07-11 with reprex v2.0.2</sup>

答案2

得分: 1

以下是翻译好的部分:

"And here's how to plot the lines and fill the space between them. The slight difficulty here is that ggplot would like the data in a slightly different format to fill the space to what is ideal for plotting the lines."

"这是如何绘制线条并填充它们之间的空间。这里的小难点在于 ggplot 希望数据格式略有不同,以便填充空间,这对于绘制线条来说是理想的。"

英文:

And here's how to plot the lines and fill the space between them. The slight difficulty here is that ggplot would like the data in a slightly different format to fill the space to what is ideal for plotting the lines.

library(tidyverse)

df = tibble(
  Budget = rep(c(0, 200), times=2),
  Type = rep(c(&quot;Line constraint 1&quot;, &quot;Line constraint 2&quot;), each = 2),
  Y = ifelse(
        Type == &quot;Line constraint 1&quot;,
        -505 + 3.5*Budget,
        198 + Budget
  )
)

df %&gt;% 
  ggplot() + 
    geom_line(aes(x = Budget, y = Y, colour = Type)) +
    geom_ribbon(
      data=df %&gt;%  pivot_wider(names_from = Type, values_from = Y),
      aes(x = Budget, ymin = `Line constraint 1`, ymax = `Line constraint 2`),
      fill = &quot;steelblue&quot;, alpha = 0.25
    ) +
    labs(y = &quot;Space&quot;)

在R中绘制约束条件。

答案3

得分: 0

使用plotPolytope函数,该函数使用ggplot2库。

library(gMOIP)
A <- matrix(c(0.2, 0.7,
              1.0, 1.0), 2, 2, byrow = TRUE)
b <- c(100, 200)

plotPolytope(A, b)

或者获取角点,然后使用经典图形绘制:

library(gMOIP)
cp <- cornerPoints(A, b)
plot(cp, type = "n")
polygon(cp[chull(cp), ], density = 10)

在R中绘制约束条件。

在R中绘制约束条件。


<details>
<summary>英文:</summary>

Use plotPolytope which makes use of ggplot2

    library(gMOIP)
    A &lt;- matrix(c(0.2, 0.7,
                  1.0, 1.0), 2, 2, byrow = TRUE)
    b &lt;- c(100, 200)
    
    plotPolytope(A, b)

[![screenshot][1]][1]

or get the corner points and then plot using classic graphics

    library(gMOIP)
    cp &lt;- cornerPoints(A, b)
    plot(cp, type = &quot;n&quot;)
    polygon(cp[chull(cp), ], density = 10)

[![screenshot][2]][2]


  [1]: https://i.stack.imgur.com/mlZX9.png
  [2]: https://i.stack.imgur.com/2UEJ1.png

</details>



huangapple
  • 本文由 发表于 2023年7月11日 14:35:55
  • 转载请务必保留本文链接:https://go.coder-hub.com/76659242.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定