Skip to content

Instantly share code, notes, and snippets.

@cdesante
Created September 19, 2012 16:38
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cdesante/3750663 to your computer and use it in GitHub Desktop.
Save cdesante/3750663 to your computer and use it in GitHub Desktop.
Greek and Variables in Plot Titles
shade.norm <- function(MU, SD, Lower, Upper, fill.color) {
cord.x <- c(Lower,seq(Lower,Upper,0.01),Upper)
cord.y <- c(0,dnorm(seq(Lower,Upper,0.01),
mean = MU, sd = SD),0)
y.axis.text <-bquote("Density of N " * " " * "(" *
mu * " " * "=" * " " * .(MU) * ","* " " * sigma *
" " * "=" * " " * .(SD) * ")")
curve(dnorm(x, MU, SD), xlim=c(MU - 4*SD, MU + 4*SD),
ylab=y.axis.text)
polygon(cord.x, cord.y, col=fill.color)
}
#The function is then called using shade.norm()
#So, which proportion of the population has IQs between 80 and 90?
shade.norm(100, 15, 80, 90, 'red')
#And, if we don't like red,
shade.norm(100, 15, 120, 130, 'blue')
#Or green?
shade.norm(5, 1.3, 2,4, 'green')
#Now, if we want to change how filled in the plot is, we can
#add an additional argument to our function. To keep things
#straight, I'm going to make a new function called
# shade.norm.density, and add a sixth argument, fill.density.
#This then appears in the plotting command, as the argument
# that corresponds with, you guessed it, the density of the
#color on the polygon function.
shade.norm.density <- function(MU, SD, Lower, Upper,
fill.color, fill.density){
cord.x <- c(Lower,seq(Lower,Upper,0.01),Upper)
cord.y <- c(0,dnorm(seq(Lower,Upper,0.01), mean = MU, sd = SD),0)
y.axis.text <-bquote("Density of N " * " " * "(" *
mu * " " * "=" * " " * .(MU) * ","* " " * sigma *
" " * "=" * " " * .(SD) * ")")
curve(dnorm(x, MU, SD), xlim=c(MU - 4*SD, MU + 4*SD)
, ylab=y.axis.text)
polygon(cord.x, cord.y, col=fill.color, density = fill.density)
}
shade.norm.density(0, 1, 1, 2, 'darkblue', 45)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment