[R] Two overlaid density plots - Does order matter?

Eik Vettorazzi E.Vettorazzi at uke.uni-hamburg.de
Thu Sep 25 18:17:08 CEST 2008


Since you did not specify plotting limits (xlim, ylim) the plot-function 
automatically sets them to the range of your data, extended by some 
factor (depending on your axis style). If you draw another object 
afterwards with "line" this limits are not altered.
So depending on the data range of the first object to be drawn your 
final result will be clipped to that actual range.

So

plot(pre.den, xlim=c(-5,30))
lines(cdata.den,col = 1)

should correct the x-axis. You may notice, that this automatic process 
also applies to the y-axis, so

yl<-range(pre.den$y,cdata.den$y)
plot(pre.den, xlim=c(-5,30),ylim=yl)
lines(cdata.den,col = 1)

should produce the desired plot.

hth.

Stephen Collins schrieb:
> In the following code, the only difference between the two plots is the 
> order the variables are plotted.  In this case, the plot of "cdata.den" in 
> plot #1 is different from its plot in #2. Specifically, "cdata.den" spans 
> the x-axis from -5 to 30 in plot #1 and from 0 to 20 in plot #2. Does 
> anyone understand why these two plots do not yield the same result? 
>
> #Make density objects
> pre.den<- density(
>         preclaims[preclaims[,7]>cc1 & preclaims[,7]<cc2,7],
>         from = cc1,
>         to = cc2)
> cdata.den<-density(cdata,from=cc1,to=cc2)
>
> #Plot No. 1
> x11()
> plot(cdata.den,col=1)
> lines(pre.den,col=2)
>
> #Plot No. 2
> plot(pre.den,col=2)
> lines(cdata.den,col = 1)
>
>  Regards,
>  
> Stephen Collins, MPP | Analyst
> Health & Benefits | Aon Consulting
> 200 East Randolph, Suite 900, Chicago, IL
> Tel: 312-381-2578 | Fax: 312-381-0136
> Email: stephen_collins at aon.com
>  
>
> 	[[alternative HTML version deleted]]
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>   

-- 
Eik Vettorazzi
Institut für Medizinische Biometrie und Epidemiologie
Universitätsklinikum Hamburg-Eppendorf

Martinistr. 52
20246 Hamburg

T ++49/40/42803-8243
F ++49/40/42803-7790



More information about the R-help mailing list