[R] superimposing graphs

Roger Peng rpeng at stat.ucla.edu
Sat Apr 19 02:47:41 CEST 2003


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
>



More information about the R-help mailing list