Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
714 views
in Technique[技术] by (71.8m points)

r - Is it possible to use stat_function by group?

Following with the answer to my previous question, let's say that I am plotting density curves by group with ggplot and I want to produce the corresponding normal curve for each group (with their corresponding means and standard deviations). What I tried first was

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)))

but it produces a unique normal curve. Then I found in this question (whose answer I don't see how can help me), that stat_function understands group aesthetics, so I tried

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)))

but plot does not change. So how can I tell to stat_function that I want the arguments should be taken for each vs-group? I also expect the colour of each of these normal curves would be the same that (or related to) the mpg curve colour of the same group.

I had also tried with

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

but it had no effect.

Thanks!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Using loops:

Example 1:two variables

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

Example 2: more than two variables

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

Scrappy solution: adding two layers

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)))

Output: enter image description here


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...