你可以将两者结合起来
stat_function
s,并用创建两个
aes
为您
fill
使用创建图例
scale_fill_manual
这样地:
library(ggplot2)
ggplot(data.frame(x = c(-4, 4)), aes(x)) +
stat_function(fun = dnorm, args = list(mean = 0, sd = 1), col='red') +
stat_function(fun = dnorm, xlim = c(-4, -1), geom = "area", aes(fill = "plot 1")) +
stat_function(fill='red', fun = dnorm, xlim = c(-1, 4), geom = "area", alpha = 0.3) +
stat_function(fun = dnorm, args = list(mean = 0, sd = 2), col='blue') +
stat_function(fun = dnorm, args = list(mean = 0, sd = 2), xlim = c(-4, -1), geom = "area", aes(fill = "plot 2")) +
stat_function(fill='blue', fun = dnorm, args = list(mean = 0, sd = 2), xlim = c(-1, 4), geom = "area", alpha = 0.3) +
scale_fill_manual(name = "Legend", values = c("red", "blue"))
创建于2022-12-31
reprex v2.0.2