sociolog
sociolog

Reputation: 101

How can I add a normal distribution curve into geom_density?

I want to add a normal distribution curve in to my density curve to compare between them.

heights %>% filter(sex == "Male") %>% ggplot() +
  aes(x = height) +
  geom_density(fill = "blue")

What should I do after this? I want it to be seen like this when it's done:
enter image description here

Upvotes: 0

Views: 649

Answers (1)

Ahorn
Ahorn

Reputation: 3876

You could use stat_function:

heights %>% filter(sex == "Male") %>% ggplot() +
  aes(x = height) +
  geom_density(fill = "blue") +
  stat_function(fun = dnorm, n = 101, size = 1.5, args = list(mean = mean(heights$height[heights$sex == "Male"]), sd = sd(heights$height[heights$sex == "Male"])))

enter image description here

Upvotes: 1

Related Questions