[R] Comparing density plots using same axes or same axes scale

David Winsemius dwinsemius at comcast.net
Thu Sep 27 03:47:32 CEST 2012


On Sep 26, 2012, at 6:13 PM, Rui Barradas wrote:

> Hello, again.
> 
> I forgot, but the subject line also says "same axes", not just same axes scale.
> If you want all densities on the same graph, use ?matplot (matrix plot).

If you use matplot ,  presumably just plotting the y-values, you will need to set up the "from" and "to"  ranges in the calls to density() to be the same. Possibly with:

d.list <- apply(sag, 2, density, from= min(sapply(sag,min)), to=max(sapply(sag,max)) )
# then proceeding in sensible fashion.

-- 
david.
> 
> Rui Barradas
> Em 27-09-2012 02:08, Rui Barradas escreveu:
>> Hello,
>> 
>> Something like this?
>> 
>> sag <- matrix(rnorm(1e3 * 9), ncol = 9)
>> 
>> d.list <- apply(sag, 2, density)
>> xrange <- range(sapply(d.list, function(d) range(d$x)))
>> ymax <- max(sapply(d.list, function(d) max(d$y)))
>> 
>> op <- par(mfrow=c(3,3))
>> for(i in 1:9)
>>    plot(d.list[[i]], xlim = xrange, ylim = c(0, ymax))
>> par(op)
>> 
>> 
>> Hope this helps,
>> 
>> Rui Barradas
>> Em 27-09-2012 01:53, Meredith Ballard LaBeau escreveu:
>>> Good Evening-
>>>   I have a set of nine scenarios I want to plot to see how the distribution
>>> is changing, if one tail is getting larger in certain scenario, currently I
>>> am using this code
>>> 
>>> 
>>> colnames<-dimnames(sag_pdfs)[[2]]
>>> 
>>> par(mfrow=c(3,3))
>>> 
>>> for(i in 1:9) {
>>> 
>>> d<-density(sag[,i])
>>> 
>>> plot(d,type="n", main=colnames[i])
>>> 
>>> polygon(d,col="red",border="grey")}
>>> 
>>> where sag is a 7305x9 double matrix and 9 different scenarios. I want to be
>>> able to compare the distribution using the same axes scale.
>>> Can anyone help?
>>> 
>>> Thanks
>>> Meredith LaBeau
>>> 
> 


David Winsemius, MD
Alameda, CA, USA




More information about the R-help mailing list