[R] Newbie question about plotting density objects

Jason Turner jasont at indigoindustrial.co.nz
Wed Sep 10 04:48:23 CEST 2003

cmprobst wrote:
> Dear List,
> I have an array of 6400 x 56 elements. I want to calculate the density function for each column and plot all 56 density functions in one plot.
> I have tried several procedures, but they all failed.
> What can I do?

56 lines could even confuse a spider, so you might want to re-think your 
plotting approach.  However....

## untested code!
## get a density object for each column.
den.list <- apply(zz,2,density)

## set up your plot window
all.x <- sapply(den.list,function(d,...) {d$x})
all.y <- sapply(den.list,function(d,...) {d$y})

## now plot each one on that graph
## this will make each line the same colour; for
## different line types or colours, you'll probably
## have to use a for() loop.

lapply(den.list,function(d,...) {lines(d$x,d$y)})

There's probably a much, much cleaner way to do that, though.  I think 
the sm library has the sm.density function that takes an "add" argument 
to its plots...  too busy to check that out on this box, however.



Indigo Industrial Controls Ltd.
jasont at indigoindustrial.co.nz

More information about the R-help mailing list