Cómo graficar una Normal con dos colas sombreadas

Author

Carlos J. Gómez

Published

December 9, 2022

Código completo (listo para copiar y pegar)

df <- data.frame(x = seq(-5, 5, length.out = 1000))
library(ggplot2)
ggplot(df, aes(x)) +
  stat_function(fun = dnorm) + 
  stat_function(fun = dnorm, 
                xlim = c(-5, -2),
                geom = 'area',
                fill = 'red') +
    stat_function(fun = dnorm, 
                xlim = c(2, 5),
                geom = 'area',
                fill = 'red') +
  theme_minimal() # Es opcional

Aquí abajo algunas opciones para modificar

Graficamos la Normal con dos colas sombreadas con color azul (blue)

Si quieres cambiar los colores de las colas basta con indicarlo en fill, por ejemplo, cambiemos a azul fill = blue

ggplot(df, aes(x)) +
  stat_function(fun = dnorm) + 
  stat_function(fun = dnorm, 
                xlim = c(-5, -2),
                geom = 'area',
                fill = 'blue') +
    stat_function(fun = dnorm, 
                xlim = c(2, 5),
                geom = 'area',
                fill = 'blue') +
  theme_minimal() # Es opcional

Si quieres eliminar la líneas del grid usar theme_void()

ggplot(df, aes(x)) +
  stat_function(fun = dnorm) + 
  stat_function(fun = dnorm, 
                xlim = c(-5, -2),
                geom = 'area',
                fill = 'blue') +
    stat_function(fun = dnorm, 
                xlim = c(2, 5),
                geom = 'area',
                fill = 'blue') +
  theme_void()