You can do the automated variant of manually creating multiple stat_functions()
for every parameter.
For example, write a function that takes your values as input and produces a list of stat_function()
s.
multidensity <- function(mu, sd, group, class, range = c(-3, 3)) {
mapply(function(mu, sd, group, class) {
stat_function(
data = data.frame(group = group, class = class, x = range),
aes(x = x, colour = class),
fun = dnorm,
args = list(mean = mu, sd = sd),
inherit.aes = FALSE
)
}, mu = mu, sd = sd, group = group, class = class,
SIMPLIFY = FALSE)
}
Then, simply call function when needed.
m = c(1, 1.5, 3, 3.5) # means of gaussians
s = c(0.1, 0.3, 0.5, 0.7) # standard deviations of gaussians
g = c("A", "A", "B", "B") # group
v = c("X", "Y", "X", "Y") # class
ggplot() +
multidensity(m, s, g, v, range = c(0, 5)) +
facet_wrap(~ group)
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…