绘制R中不同参数值的多个图

问题描述 投票:0回答:1

我想以图形方式探索给定方程组的一系列参数。

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个图表,每个图表在一个面板中具有不同的值,或者说5行,每行10个图。如何在没有大量代码的情况下编写这个(即不重复绘图代码50次)?

r plot curve par
1个回答
1
投票

我恐怕我没有在基础R中做到这一点的知识,但它可以通过预先计算源表中的结果在ggplot(也可能是基础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()
)

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.