leo_wa
2008-Oct-21 02:47 UTC
[R] how to plot the histogram and the curve in the same graph
i want to plot the histogram and the curve in the same graph.if i have a set of data ,i plot the histogram and also want to see what distribution it was.So i want to plot the curve to know what distribution it like. -- View this message in context: http://www.nabble.com/how-to-plot-the-histogram-and-the-curve-in--the-same-graph-tp20082506p20082506.html Sent from the R help mailing list archive at Nabble.com.
Dieter Menne
2008-Oct-21 06:19 UTC
[R] how to plot the histogram and the curve in the same graph
leo_wa <kwngai6022 <at> hotmail.com> writes:> > i want to plot the histogram and the curve in the same graph.if i have a > set of data ,i plot the histogram and also want to see what distribution > it was.So i want to plot the curve to know what distribution it like.See the example under histogram/lattice. Also have a look at Deepayan's book-site. http://lmdvr.r-forge.r-project.org/figures/figures.html Dieter
matthieu dubois
2008-Oct-21 07:53 UTC
[R] how to plot the histogram and the curve in the same graph
leo_wa <kwngai6022 <at> hotmail.com> writes:> > > i want to plot the histogram and the curve in the same graph.if i have a set > of data ,i plot the histogram and also want to see what distribution it > was.So i want to plot the curve to know what distribution it like.You will find below an example using only basic plotting functions. The created function (called histplot) plots a histogram of the data, along with a density kernel estimate of the distribution and (if asked by option ncurve=T) the normal distribution (with mean and sd computed from the original data). Hope this will help Matthieu #generating random values to be plotted dat <- rnorm(100) #plotting function histplot <- function(dat, breaks="Sturges", ncurve=TRUE, ...) { #compute the histogram and density of "dat" hdat <- hist(dat, breaks=breaks, plot=F) ddat <- density(dat) #compute the xlim and ylim of the plot # i.e. the min and max of the different superimposed #plots (hist, density and normal curves) xlim <- range(ddat$x) if(ncurve) { #max of the normal curve maxnorm <- pnorm(mean(dat), mean=mean(dat), sd=sd(dat)) ylim <- c(0, max(hdat$density,ddat$y,maxnorm)) } else { ylim <- c(0, max(hdat$density,ddat$y)) } #plotting plot(hdat, freq=F, xlim=xlim, ylim=ylim, ...) lines(ddat) if (ncurve) curve(dnorm(x, mean=mean(dat), sd=(sd(dat))), lty=3, add=TRUE) } #usage histplot(dat) histplot(dat, ncurve=F) histplot(dat, col="blue") #arguments are passed to the hist plotting function
Rubén Roa-Ureta
2008-Oct-21 12:05 UTC
[R] how to plot the histogram and the curve in the same graph
leo_wa wrote:> i want to plot the histogram and the curve in the same graph.if i have a set > of data ,i plot the histogram and also want to see what distribution it > was.So i want to plot the curve to know what distribution it like. > >To draw the curve and the distribution you should have an idea about the distribution. You cann't just draw the histogram and expect R to make a curve of the best distribution to fit that histogram. But you can plot a curve of a kernel density. x <- rnorm(1000,5,3) library(MASS) (x.normfit <- fitdistr(x,"normal")) hist(x,prob=TRUE) lines(density(x,na.rm=TRUE),col="red") # kernel density curve(dnorm(x,mean= x.normfit$estimate[1],sd= x.normfit$estimate[2]),col="blue",add=TRUE) #maximum likelihood estimate Rub?n
Greg Snow
2008-Oct-21 18:00 UTC
[R] how to plot the histogram and the curve in the same graph
It is not clear what curve you want to plot, but this code may help get you started (the final plot will probably need the x and y limits expanded and maybe other options changed): x <- rnorm(100, rep( c(100,105), c(75,25)), 2 ) hist(x, probability=TRUE) lines( density(x), col='green' ) curve( dnorm(x, mean=mean(x), sd=sd(x)), col='blue', add=TRUE ) see the help for hist, lines, and curve for details. Hope this helps, -- Gregory (Greg) L. Snow Ph.D. Statistical Data Center Intermountain Healthcare greg.snow at imail.org 801.408.8111> -----Original Message----- > From: r-help-bounces at r-project.org [mailto:r-help-bounces at r- > project.org] On Behalf Of leo_wa > Sent: Monday, October 20, 2008 8:47 PM > To: r-help at r-project.org > Subject: [R] how to plot the histogram and the curve in the same graph > > > i want to plot the histogram and the curve in the same graph.if i have > a set > of data ,i plot the histogram and also want to see what distribution it > was.So i want to plot the curve to know what distribution it like. > -- > View this message in context: http://www.nabble.com/how-to-plot-the- > histogram-and-the-curve-in--the-same-graph-tp20082506p20082506.html > Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > R-help at 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.
Carl Witthoft
2008-Oct-21 21:12 UTC
[R] how to plot the histogram and the curve in the same graph
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)) }
Apparently Analagous Threads
- recursively Merging a list a zoo objects
- maintaining row connections during aggregate
- Superimposing Line over Histogram in Density Plot
- My very first loop!! I failed. May I have some start-up aid?
- Plotting barplot and scatterplot on same device - x-axis problem