[R] how to plot the histogram and the curve in the same graph

Carl Witthoft carl at witthoft.com
Tue Oct 21 23:12:08 CEST 2008


Here is some code that more or less does what you want -- not a true 
histogram, but all else is in place.
Feel free to modify, give away, etc. No copyright ;-)


myhist<-function(data,breaks="Sturges",xlabel="data 
value",ylabel='counts',curve=TRUE, title="myhist.R made this fake 
histogram",sdf=5)
{
sudh<-hist(data,plot=FALSE,breaks=breaks)

#
# Note: the literature says a histogram relates count density to box 
volume, so
# technically this function makes a bar chart, not a histogram.  Whatever.
#
# get current graph dims and calculate  a nice bar width
pdim<-par('din')
barwd<-pdim[1]*2
# find midpoints of histogram cells IDIOT: hist() does this for you
#mids<-breaks[1:(length(breaks)-1)] +diff(breaks)/2
# get max of spline-- will almost always be > data
#smoo<-spline(sudh$breaks[1:length(sudh$breaks)-1],sudh$counts)
smoo<-spline(sudh$mids,sudh$counts)
#this was dumb: only plot smoo if curve=TRUE,
# labels and stuff  are hairy if merge a PLOT inside and outside IF
#subtle: to get same graph size, need to set ymax for plot to max of 
spline(y)
# I think usr=c(min(smoo$x),max(sudh$breaks),0,max(smoo$y)) works
if(curve) {
	plot(smoo$x, 
smoo$y,xlab=xlabel,ylab=ylabel,'l',col='green',usr=c(min(smoo$x),max(sudh$breaks),0,max(smoo$y)))
	#now plot the bars
	
	lines (sudh$mids,sudh$counts,type='h',lwd=barwd,lend=1)
	}
else {
	#just plot the hist, not the spline
#plot(sudh$breaks[1:length(sudh$breaks)-1],sudh$counts,type='h',xlab=xlabel,ylab=ylabel,lwd=barwd,lend=1)
plot(sudh$mids,sudh$counts,type='h',xlab=xlabel,ylab=ylabel,lwd=barwd,lend=1)
#lines(sudh$breaks[1:length(sudh$breaks)-1]/60,sudh$counts,col='red')
	}
title(main=title)
histstuff<-list(input=data, histogram=sudh, spline=smoo)
return(invisible(histstuff))
}



More information about the R-help mailing list