RoiMinuit
RoiMinuit

Reputation: 414

How to label the mean and three standard deviations on Normal Curve with R

I am trying to add the mean and 3 sd's as labels on this plot; I have tried " How to plot a normal distribution by labeling specific parts of the x-axis? " but the solution in there will not work since I have 1000 observations. I can't go labels = c(0, 1, 2...mean...500, 501...750) and there is no solution to add the three standard deviation labels either. Here is the plot: enter image description here

Here is my current code:

x = seq(0, 750, length = 1000)
y <- dnorm(x, mean = 435, sd = 72)
plot(x, y,
     type = 'p',
     lwd = 1,
     col = 'red',
     main = "Standard Normal Distribution with Mean 435 and SD 72",
     xlab = "Score",
     ylab = "Frequency"
     )

Upvotes: 0

Views: 1295

Answers (2)

ThomasIsCoding
ThomasIsCoding

Reputation: 102599

Maybe you can try to add the following lines after your code

abline(v = 435 + seq(-3,3)*72, col = "black",lty = 2)
text(435 + seq(-3,3)*72,max(y)/2,c(paste0(-(3:1),"sd"),"mean",paste0(1:3,"sd")))

such that enter image description here

Upvotes: 3

Ben Norris
Ben Norris

Reputation: 5747

You can do this easily with ggplot2.

library(ggplot2)

df <- data.frame(x, y)

ggplot(df) + 
  geom_point(aes(x, y), color ="red") +
  scale_x_continuous(breaks = 425 + seq(-3 * 72, 3 * 72, 72)) +
  geom_text(aes(x = 425 + seq(-3 * 72, 3 * 72, 72), y = 0.006, 
            label = c("-3 SD", "-2 SD", "-1 SD", "mean", "+1 SD", "+2 SD", "+3 SD"))) +
  geom_vline(xintercept = 425 + seq(-3 * 72, 3 * 72, 72), linetype = 2, size = .5, alpha = .5 )

Omit the last two lines if you don't want the text labels and dotted lines.

enter image description here

Upvotes: 2

Related Questions