可以按组使用stat_function吗?

时间:2019-06-12 10:53:47

标签: r ggplot2 grouping

在回答my previous question之后,假设我正在用ggplot按组绘制密度曲线,并且我想为每组生成相应的法线曲线(及其相应的均值和标准偏差) )。 我首先尝试的是

library(ggplot2)
mtcars$vs <- as.factor(mtcars$vs)
ggplot(mtcars,aes(x=mpg, fill = vs, colour = vs)) + geom_density(alpha = 0.1) + 
    stat_function(fun = dnorm, args = list(mean = mean(mtcars$mpg), sd = sd(mtcars$mpg)))

但它会产生唯一的法线曲线。然后我在this question(其答案我看不出有什么帮助我)中发现,stat_function了解group的美学,所以我尝试了

ggplot(mtcars,aes(x=mpg, fill = vs, colour = vs)) + geom_density(alpha = 0.1) + 
    stat_function(aes(group = vs), fun = dnorm, args = list(mean = mean(mtcars$mpg), sd = sd(mtcars$mpg)))

但是情节没有改变。那么如何告诉stat_function我希望每个vs组都接受参数?我还希望每条正常曲线的颜色与同一组的mpg曲线颜色相同(或相关)。

我也尝试过

library(dplyr)
ggplot(mtcars %>% group_by(vs),...

但没有效果。

谢谢!

1 个答案:

答案 0 :(得分:1)

使用循环

示例1:两个变量

mtcars$vs <- as.factor(mtcars$vs)
p <- unique(mtcars$vs)
g <- ggplot(mtcars, aes(x = mpg, fill = vs, colour = vs))
for (i in seq_along(p))  {
  df <- mtcars %>% filter(vs == p[i])
  g <- g + geom_density(alpha = .05) +
    stat_function(data = df,
                  fun = dnorm,
                  args = list(mean = mean(df$mpg), sd = sd(df$mpg)))
}
g

enter image description here

示例2:两个以上的变量

mtcars$cyl <- as.factor(mtcars$cyl)
p <- unique(mtcars$cyl)
g <- ggplot(mtcars, aes(x = mpg, fill = cyl, colour = cyl))
for (i in seq_along(p))  {
  df <- mtcars %>% filter(cyl == p[i])
  g <- g + geom_density(alpha = .05) +
    stat_function(data = df,
                  fun = dnorm,
                  args = list(mean = mean(df$mpg), sd = sd(df$mpg)))
}
g

enter image description here

偷偷摸摸的解决方案:添加两层

library(ggplot2)

mtcars$vs <- as.factor(mtcars$vs)
mt1 <- filter(mtcars, vs == 1)
mt0 <- filter(mtcars, vs == 0)

ggplot(mtcars, aes(x = mpg, fill = vs, colour = vs)) + geom_density(alpha = 0.1) +
  stat_function(data = mt0, fun =  dnorm,
    args = list(mean = mean(mt0$mpg), sd = sd(mt0$mpg))) +
  stat_function(data = mt1, fun = dnorm,
                args = list(mean = mean(mt1$mpg), sd = sd(mt1$mpg)))

输出: enter image description here