Skip to content

Instantly share code, notes, and snippets.

@wetherc
Last active August 29, 2015 14:27
Show Gist options
  • Save wetherc/645843767c0a1c8048aa to your computer and use it in GitHub Desktop.
Save wetherc/645843767c0a1c8048aa to your computer and use it in GitHub Desktop.
# Construct a couple sample normal distributions
# and set x limits for plotting
x <- seq(from = 110, to = 174, by = 0.5)
y1 <- dnorm(x, mean = 145, sd = 9)
y2 <- dnorm(x, mean = 138, sd = 8)
# Plot outline of first distribution
# with titles and labels
plot(x, y1, type="l", lwd=2, col="red",
main="Systolic Blood Pressure Before and After Treatment",
xlab = "Systolic Blood Pressure (mmHg)",
ylab = "Frequency", yaxt="n",
xlim = c(110, 175), ylim = c(0, 0.05))
# Add outline of second distribuion
lines(x, y2)
# Fill in both distributions
# x = (min, x, max)
# y = (0, y, 0) since distribution
# asymptotically approaches 0
# at extreme values of x
# Include color and density for prettiness
# Add black border for prettiness
polygon(c(110,x,175),c(0,y2,0), col="firebrick3", density = 100,
border = "black")
polygon(c(117,x,175),c(0,y1,0), col="dodgerblue4", density = 100,
border = "black")
# Make labels for y-axis
ylab=c(seq(from=0, to=175, by=25))
# Scale for y-axis
y=c(seq(from=0, to=0.05, length.out = 8))
# Add text to y-axis
axis(2,at=y,labels=ylab, las=1)
# Add in legend text for each distribution
text(x = 120, y = 0.045, "- Pre-Treatment BP", col = "dodgerblue4", cex = 0.9)
text(x = 120, y = 0.04, " - Post-Treatment BP", col = "firebrick3", cex = 0.9)
points(109, 0.0445, pch = 15, col = "dodgerblue4")
points(109, 0.0395, pch = 15, col = "firebrick3")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment