library(ggplot2) # Generate 100 random data points with mean=50 and sd=10 set.seed(123) d <- rnorm(100, 50, 10) # Calculate mean, variance, and standard deviation m <- mean(d); v <- var(d); s <- sd(d) # Create the plot ggplot(data.frame(d), aes(d)) + geom_density(fill = "lightblue", alpha = 0.5) + # Add vertical lines for mean and standard deviation boundaries geom_vline(xintercept = c(m, m + s, m - s), color = c("red", "green", "green"), linetype = c("dashed", "dotted", "dotted"), linewidth = c(1.2, 1, 1)) + labs(title = "Visualization of Mean, Variance, and Standard Deviation", x = "Data Values", y = "Density") + theme_minimal() + # Annotate mean, mean ± SD, and variance annotate("text", x= m, y = 0.03, label = paste("Mean =", round(m, 2)), color = "red", vjust = -1) + annotate("text", x= m + s, y = 0.02, label = paste("Mean + SD =", round(m + s, 2)), color = "green", vjust = -1) + annotate("text", x= m - s, y = 0.02, label = paste("Mean - SD =", round(m - s, 2)), color = "green", vjust = -1) + annotate("text", x= m + 20, y = 0.04, label = paste("Variance =", round(v, 2)), color = "blue", vjust = -1)