[R] superimposing graphs

Faheem Mitha faheem at email.unc.edu
Sat Apr 19 01:22:58 CEST 2003

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

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

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(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.



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)

  truehist(x, nbins=100)

  mydensityfn <- function(x)


More information about the R-help mailing list