英文:
plot constraints in R
问题
1- 0.20x1 + 0.70x2 <= 100
2- x1 + x2 <= 200
我想使用R绘制上述两个约束条件的图表:
我已经使用ggplot尝试生成了图表,但结果不正确,有些奇怪。以下是我的尝试和代码:
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., <
), 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
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")
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 = "空间")
创建于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 = "line constraint1")) +
stat_function(fun = \(x) {(198 + x) }, aes(color = "line constraint2")) +
labs(y = "Space")
<!-- -->
<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("Line constraint 1", "Line constraint 2"), each = 2),
Y = ifelse(
Type == "Line constraint 1",
-505 + 3.5*Budget,
198 + Budget
)
)
df %>%
ggplot() +
geom_line(aes(x = Budget, y = Y, colour = Type)) +
geom_ribbon(
data=df %>% pivot_wider(names_from = Type, values_from = Y),
aes(x = Budget, ymin = `Line constraint 1`, ymax = `Line constraint 2`),
fill = "steelblue", alpha = 0.25
) +
labs(y = "Space")
答案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)
<details>
<summary>英文:</summary>
Use plotPolytope which makes use of 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)
[![screenshot][1]][1]
or get the corner points and then plot using classic graphics
library(gMOIP)
cp <- cornerPoints(A, b)
plot(cp, type = "n")
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>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论