在 R 中为不同参数值绘制多个图
Graph multiple plots for different values of parameters in R
我想以图形方式探索给定方程组的一系列参数。
a = 1
b = 0.5
c = 0.8
d = 0.1
e = 0.6
f = 0.7
g = 0.2
y1 = function(x){c + b * x + d+ g+ f}
y2 = function(x){a + c + b^2 * x + e + g}
curve(y1, from = 0, to = 100, n = 100, xlim = c(-100, 100), ylim =c(-100,100), col = "grey")
par(new = TRUE)
curve(y2, from = 0, to = 100, n = 100, col = "orange")
我想可视化图形如何针对给定的其他参数值从 a = 0 到 100(增量为 2,因此为 0、2、4、...100)变化。我想在一个面板中绘制 50 个图表,每个图表具有不同的 all 值,或者说 5 行,每行 10 个图。如何在没有大量代码的情况下编写此代码(即无需重复情节代码 50 次)?
恐怕我不具备在 base R 中执行此操作的知识,但可以通过在源 table 中预先计算结果在 ggplot(并且可能也是 base R)中完成.
library(ggplot2); library(dplyr)
b = 0.5
c = 0.8
d = 0.1
e = 0.6
f = 0.7
g = 0.2
df <- tibble(
x = rep(-100:100, 51),
a = rep(seq(0, 100, by = 2), each = 201),
y1 = c + b * x + d + g + f,
y2 = if_else(x >= 0,
a + c + b^2 * x + e + g,
NA_real_)
)
ggplot(df, aes(x = x)) +
geom_line(aes(y = y1), color = "grey") +
geom_line(aes(y = y2), color = "orange") +
facet_wrap(~a, labeller = label_both) +
# To simulate "Base R" theme
theme_bw() +
theme(text = element_text(size=12),
panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
strip.background = element_blank()
)
我想以图形方式探索给定方程组的一系列参数。
a = 1
b = 0.5
c = 0.8
d = 0.1
e = 0.6
f = 0.7
g = 0.2
y1 = function(x){c + b * x + d+ g+ f}
y2 = function(x){a + c + b^2 * x + e + g}
curve(y1, from = 0, to = 100, n = 100, xlim = c(-100, 100), ylim =c(-100,100), col = "grey")
par(new = TRUE)
curve(y2, from = 0, to = 100, n = 100, col = "orange")
我想可视化图形如何针对给定的其他参数值从 a = 0 到 100(增量为 2,因此为 0、2、4、...100)变化。我想在一个面板中绘制 50 个图表,每个图表具有不同的 all 值,或者说 5 行,每行 10 个图。如何在没有大量代码的情况下编写此代码(即无需重复情节代码 50 次)?
恐怕我不具备在 base R 中执行此操作的知识,但可以通过在源 table 中预先计算结果在 ggplot(并且可能也是 base R)中完成.
library(ggplot2); library(dplyr)
b = 0.5
c = 0.8
d = 0.1
e = 0.6
f = 0.7
g = 0.2
df <- tibble(
x = rep(-100:100, 51),
a = rep(seq(0, 100, by = 2), each = 201),
y1 = c + b * x + d + g + f,
y2 = if_else(x >= 0,
a + c + b^2 * x + e + g,
NA_real_)
)
ggplot(df, aes(x = x)) +
geom_line(aes(y = y1), color = "grey") +
geom_line(aes(y = y2), color = "orange") +
facet_wrap(~a, labeller = label_both) +
# To simulate "Base R" theme
theme_bw() +
theme(text = element_text(size=12),
panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
strip.background = element_blank()
)