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))
}

______________________________________________
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.

Reply via email to