Reputation:
I want to graphically explore a range of parameters for the given set of equations.
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")
I want to visualize how the graphs change for a = 0 to 100 (with increments of 2, so 0, 2, 4,...100) for given value of other parameters. I want to plot 50 graphs each with a different value of a all in one panel, or say 5 rows with 10 plots each. How to write this without huge blocks of the code (i.e. without repeating the plot code 50 times)?
Upvotes: 0
Views: 1273
Reputation: 66755
I'm afraid I don't have the knowledge to do this in base R, but it could be done in ggplot (and presumably base R, too) by precalculating the results in a source table.
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()
)
Upvotes: 0