[R] Telling plot() the max y value to expect when plotting one distribution and then using lines() to add more distributions

FJ M chicagobrownblue at hotmail.com
Fri Feb 24 23:58:13 CET 2012


I am plotting three Pearson Type IV distributions. It looks like I have to plot the distribution with the highest value of y and then use lines() to add the two distributions that are shorter / have lower max values of y. The following code figures out which distribution has the max y value, plots it first and then uses lines for the other two distributions with a series of three if statements. This works. I run R from a batch file that reads the following in a text file.
 
I want to graph dozens of distributions and I am looking for a more elegant way to do this.
 
New to R, experienced C programmer, thanks in advance. Frank



colors <- c("red", "blue", "darkgreen", "gold", "black")
labels <- c("1", "2","3")
pdf("C:\\Users\\Frank\\Documents\\R\\Scripts\\pt4_Graph.pdf")
## load Pearson package
library(PearsonDS)
##range for x axis
no_of_increments<- 100
x <- seq(-0.06, +0.06, length=no_of_increments)
##parameters for the plots of three distributions
mx<- c(1.95, 18.35,1.93)
nux<- c(0.08,-1.02,0.25)
locationx<- c(0.0048,-0.00254,0.00189)
scalex<- c(0.0115,0.082187,0.026675)
## calculate probability density function
hx1<- dpearsonIV(x,m=mx[1],nu=nux[1],location=locationx[1],scale=scalex[1])
hx2<- dpearsonIV(x,m=mx[2],nu=nux[2],location=locationx[2],scale=scalex[2])
hx3<- dpearsonIV(x,m=mx[3],nu=nux[3],location=locationx[3],scale=scalex[3])
##calculate max of each distribtion
maxhx1<- max(hx1)
maxhx2<- max(hx2)
maxhx3<- max(hx3)
maxhx<- max(hx1,hx2,hx3)
maxhx1
maxhx2
maxhx3
maxhx

if (maxhx1==maxhx) 
 {plot(x, hx1 , type="l", lwd=2, col=colors[1], xlab="x value",  ylab="Density", main="pt4")
for (i in 2:3){
  lines(x, dpearsonIV(x,m=mx[i],nu=nux[i],location=locationx[i],scale=scalex[i]), lwd=2, col=colors[i])}
legend("topright", inset=.05, title="Distributions",
  labels, lwd=2, lty=c(1, 1, 1, 1, 2), col=colors)
grid()
}

if (maxhx2==maxhx) {plot(x, hx2 , type="l", lwd=2, xlab="x value",  ylab="Density", main="SPX", col=colors[2])
{
  lines(x, dpearsonIV(x,m=mx[1],nu=nux[1],location=locationx[1],scale=scalex[1]), lwd=2, col=colors[1])
  lines(x, dpearsonIV(x,m=mx[3],nu=nux[3],location=locationx[3],scale=scalex[3]), lwd=2, col=colors[3])
legend("topright", inset=.05, title="Distributions",
  labels, lwd=2, lty=c(1, 1, 1, 1, 2), col=colors)
grid()
}

if (maxhx3==maxhx) 
 {plot(x, hx3 , type="l", lwd=2, col=colors[3], xlab="x value",  ylab="Density", main="SPX")
for (i in 1:2){
  lines(x, dpearsonIV(x,m=mx[i],nu=nux[i],location=locationx[i],scale=scalex[i]), lwd=2, col=colors[i])}
legend("topright", inset=.05, title="Distributions",
  labels, lwd=2, lty=c(1, 1, 1, 1, 2), col=colors)
grid()
}
 
  		 	   		  


More information about the R-help mailing list