Thursday, September 11, 2014

Histograms

set.seed(121343)
u <- rnorm(100)

#default histogram
hist(u)
#with shading
hist(u, density=20)

#with specific number of bins
hist(u, density=20, breaks=20)
 

# proportion, instead of frequency
# also specifying y-axis
hist(u, density=20, breaks=-3:3, 
     ylim=c(0,.5), prob=TRUE)
# overlay normal curve with x-lab and ylim
# colored normal curve
m<-mean(u)
std<-sqrt(var(u))
hist(u, density=20, breaks=20, prob=TRUE, 
xlab="x-variable", ylim=c(0, 0.7), 
main="normal curve over histogram")
curve(dnorm(x, mean=m, sd=std), 
      col="darkblue", lwd=2, add=TRUE)
hist(u, density=20, breaks=20, prob=TRUE, 
xlab="x-variable", ylim=c(0, 0.8),
main="Density curve over histogram") 
lines(density(u), col = "blue")
 

# hist(u) is an object
# names(uh) will show all of its components
uh<-hist(u)
plot(uh, ylim=c(0, 40), col="lightgray", 
     xlab="", main="Histogram of u")
text(uh$mids, uh$counts+2, label=c(uh$counts))
 


No comments:

Post a Comment