Home > Net >  Draw density plot in R with only mean and sd information
Draw density plot in R with only mean and sd information

Time:05-26

I wonder to know if we can represent in R (ggplot) a density plot when we only have mean (i.e. 34) and standard deviation (i.e. 4.5).

CodePudding user response:

Using stat_function and dnorm

library(ggplot2)

ggplot()   
  stat_function(fun = ~ dnorm(.x, 34, 4.5), geom = "area",
                fill = "deepskyblue4", alpha = 0.5, color = "black")  
  theme_bw(base_size = 16)  
  xlim(c(20, 48))

enter image description here

CodePudding user response:

You can use rnorm to create a sample distribution for a given mean and sd, then ggplot:

library(tidyverse)

tibble(x = rnorm(10000, mean = 34, sd = 4.5)) |> 
  ggplot(aes(x))  
  geom_density(fill = "lightgrey")

Created on 2022-05-25 by the reprex package (v2.0.1)

  • Related