Meredith Ballard LaBeau
2012-Sep-27 00:53 UTC
[R] Comparing density plots using same axes or same axes scale
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
--
Doctoral Candidate
Department of Civil and Environmental Engineering
Michigan Technological University
[[alternative HTML version deleted]]
Rui Barradas
2012-Sep-27 01:08 UTC
[R] Comparing density plots using same axes or same axes scale
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
>
Rui Barradas
2012-Sep-27 01:13 UTC
[R] Comparing density plots using same axes or same axes scale
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). 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 >> > > ______________________________________________ > 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.
David Winsemius
2012-Sep-27 01:47 UTC
[R] Comparing density plots using same axes or same axes scale
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
Maybe Matching Threads
- Merging multiple columns into one column
- Removing lower whisker in boxplot to see the effects of the high values
- Build package for R 2.0.1 under Windows
- Contour plot (level curves)
- After a few time lost access to the Samba server (server samba - a member of the AD Win2k3)