Dear People, I have a data set of data x from a probability distribution, and I have a function, mydensity, of the pdf of that distribution. I'm asking for help in superimposing the histogram of x and the plot of mydensity. In the function below, I call truehist and curve, but these are plotted in different figures. I'd like them to be plotted on the same figure, and to use common axes, of course. Can anyone suggest a simple, robust way to do this? I'd prefer not to do micro-fiddling of graphical parameters if possible. I'm currently searching R-help mail archives to see if I can turn up something. A couple of people suggest par(new=TRUE), but this superimposes everything, including the axes. I notice that there is something very similar in the trellis demo, namely print(densityplot(~x)) print(histogram(x, type = "density", panel = function(x, ...) { panel.histogram(x, ...) panel.densityplot(x, col = "brown", plot.points = FALSE) })) which plots a histogram and a density plot on top of that. However, I cannot get the syntax to work right. (I'm not at all familar with trellis graphics). Does anyone know whether I could turn this to my purposes? If so, the exact syntax required would be helpful. Thanks in advance. Faheem. *********************************************************************** mg.hist <- function(len,theta,pos,size) { x <- empmargdistvec(len,theta,pos,size) postscript(file="plot.ps", horizontal = FALSE, onefile = FALSE, paper = "special", width=6, height=4) par(mfcol=c(1,2),pch=20) truehist(x, nbins=100) mydensityfn <- function(x) { mydensity(x,theta,pos,len) } curve(mydensityfn) dev.off()
Faheem this has been asked several times in the past couple of months. check ?curve and the parameter 'add' as in curve(fc(x), 0, 10, col = "darkblue", add = TRUE) # from the reference manual If this isn't what you want then search Jonathon Baron's archive at http://finzi.psych.upenn.edu/search.html for numerous other solutions, Richard Rowe Senior Lecturer Department of Zoology and Tropical Ecology, James Cook University Townsville, Queensland 4811, Australia fax (61)7 47 25 1570 phone (61)7 47 81 4851 e-mail: Richard.Rowe at jcu.edu.au http://www.jcu.edu.au/school/tbiol/zoology/homepage.html
On Fri, Apr 18, 2003 at 07:22:58PM -0400, Faheem Mitha wrote:> In the function below, I call truehist and curve, but these are plotted in > different figures. > > I'd like them to be plotted on the same figure, and to use common axes, of > course. Can anyone suggest a simple, robust way to do this? I'd prefer not > to do micro-fiddling of graphical parameters if possible.Here is one way with an estimated density function, IIRC more a less a copy of what is in MASS.> library(MASS) > X<-rt(250,2) > Xdensity <- density(X, width=width.SJ(X, method="dpi"), n=200) > truehist(X, col="lightgray", ymax=max(Xdensity$y)) > lines(Xdensity) > title(main="Histogram and Density") > box()Peter has another solution in ISwR, but that may have been particular to a Normal distribution, and my copy is at work anyway. Hth, Dirk -- Don't drink and derive. Alcohol and algebra don't mix.
You can use curve(), but you need to add the `add = TRUE' argument. For example, library(MASS) x <- rnorm(100) truehist(x) curve(dnorm, -3, 3, add = TRUE) -roger _______________________________ UCLA Department of Statistics http://www.stat.ucla.edu/~rpeng On Fri, 18 Apr 2003, Faheem Mitha wrote:> > Dear People, > > I have a data set of data x from a probability distribution, and I have a > function, mydensity, of the pdf of that distribution. > > I'm asking for help in superimposing the histogram of x and the plot of > mydensity. > > In the function below, I call truehist and curve, but these are plotted in > different figures. > > I'd like them to be plotted on the same figure, and to use common axes, of > course. Can anyone suggest a simple, robust way to do this? I'd prefer not > to do micro-fiddling of graphical parameters if possible. > > I'm currently searching R-help mail archives to see if I can turn up > something. > > A couple of people suggest par(new=TRUE), but this superimposes > everything, including the axes. > > I notice that there is something very similar in the trellis demo, namely > > print(densityplot(~x)) > print(histogram(x, type = "density", > panel = function(x, ...) { > panel.histogram(x, ...) > panel.densityplot(x, col = "brown", > plot.points = FALSE) > })) > > which plots a histogram and a density plot on top of that. However, I > cannot get the syntax to work right. (I'm not at all familar with trellis > graphics). Does anyone know whether I could turn this to my purposes? > If so, the exact syntax required would be helpful. > > Thanks in advance. > > Faheem. > > *********************************************************************** > > mg.hist <- function(len,theta,pos,size) > { > x <- empmargdistvec(len,theta,pos,size) > > > postscript(file="plot.ps", horizontal = FALSE, onefile = FALSE, paper > = "special", width=6, height=4) > par(mfcol=c(1,2),pch=20) > > truehist(x, nbins=100) > > mydensityfn <- function(x) > { > mydensity(x,theta,pos,len) > } > curve(mydensityfn) > > dev.off() > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://www.stat.math.ethz.ch/mailman/listinfo/r-help >