Yocoda

Bell Curve in R


Make sure to install packages

My First Attempt:

z_values <- data.frame(z = seq(-4, 4, by = 0.01)) %>% 
  mutate(probability = dnorm(z))
ggplot(z_values, aes(x = z, y = probability)) + 
  geom_line() + 
  geom_area(data = subset(z_values, z >= -1 & z <= 1), fill = "blue", alpha = 0.4) +
  geom_area(data = subset(z_values, z >= -2 & z <= 2), fill = "blue", alpha = 0.3) +
  geom_area(data = subset(z_values, z >= -3 & z <= 3), fill = "blue", alpha = 0.2) +
  labs(title = "Bell Curve", x = "Z Values", y = "Probability Density") +
  geom_segment(aes(x = 0, y = 0, xend = 0, yend = dnorm(0)), linetype = "dashed", color = "red") +
  geom_segment(aes(x = -1, y = 0, xend = -1, yend = dnorm(-1)), linetype = "dashed", color = "red") +
  geom_segment(aes(x = 1, y = 0, xend = 1, yend = dnorm(1)), linetype = "dashed", color = "red") +
  geom_segment(aes(x = -2, y = 0, xend = -2, yend = dnorm(-2)), linetype = "dashed", color = "red") +
  geom_segment(aes(x = 2, y = 0, xend = 2, yend = dnorm(2)), linetype = "dashed", color = "red") +
  geom_segment(aes(x = -3, y = 0, xend = -3, yend = dnorm(-3)), linetype = "dashed", color = "red") +
  geom_segment(aes(x = 3, y = 0, xend = 3, yend = dnorm(3)), linetype = "dashed", color = "red") +
  geom_text(aes(x = 0, y = -.07, label = "μ", vjust = -2)) +
  geom_label(aes(x = -2.5, y = -.05, label = "2.14%", vjust = -1)) +
  geom_label(aes(x = 2.5, y = -.05, label = "2.14%", vjust = -1)) +
  geom_label(aes(x = -1.5, y = -.05, label = "13.59%", vjust = -1)) +
  geom_label(aes(x = 1.5, y = -.05, label = "13.59%", vjust = -1)) +
  geom_label(aes(x = -0.5, y = -.05, label = "34.13%", vjust = -1)) +
  geom_label(aes(x = 0.5, y = -.05, label = "34.13%", vjust = -1)) +
  scale_x_continuous(breaks = -4:4) +
  theme_minimal()


My Second Attempt (cleaning up the code):

z_values <- data.frame(z = seq(-4, 4, by = 0.01)) %>%
  mutate(probability = dnorm(z))
ggplot(z_values, aes(x = z, y = probability)) + 
  geom_line() + 
  labs(title = "Bell Curve", x = "Z Values", y = "Probability Density") +
  scale_x_continuous(breaks = -4:4) +
  theme_minimal() +
  # Add shaded areas under the curve
  geom_area(data = subset(z_values, z >= -1 & z <= 1), fill = "blue", alpha = 1.2) +
  geom_area(data = subset(z_values, z >= -2 & z <= 2), fill = "blue", alpha = 0.5) +
  geom_area(data = subset(z_values, z >= -3 & z <= 3), fill = "blue", alpha = 0.2) +
  # Use a loop to draw the dashed lines at the standard deviations
  lapply(seq(-3, 3), function(x) {
    geom_segment(aes(x = x, y = 0, xend = x, yend = dnorm(x)), linetype = "dashed", color = "red")
  }) +
  geom_text(aes(x = 0, y = -.07, label = "μ", vjust = -2)) +
  annotate("label", x = c(-2.5, -1.5, -0.5, 0.5, 1.5, 2.5), y = -.06,
           label = c("2.14%", "13.59%", "34.13%", "34.13%", "13.59%", "2.14%"), vjust = -1)


Recent posts