Bonjour.

J'aimerai tronquer la gaussienne a plus 3 écarts type et moins 3 écarts type, mais je ne sais pas comment m'y prendre. voici un code d'exemple:

Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
d <- data.frame(
    val = c(rnorm(100), 10, -10)
)
 
library(ggplot2)
 
ggplot(d) +
    aes(x = val) +
    geom_histogram(aes(y = ..count..), 
                   color = "white", 
                   alpha = 0.7) +
    stat_function(
        fun = function(x, mean, sd, n){
            dnorm(x = x, mean = mean, sd = sd) * n
        },
        args = with(mtcars, c(mean = mean(d$val), sd = sd(d$val), n
                              = length(d$val))),
        col = "darkgray"
    )