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

R. Michael Weylandt michael.weylandt at gmail.com
Sat Feb 25 21:40:59 CET 2012

```I might (re-)format your code as follows -- others will make some
different design decisions. This isn't the most efficient (more
vectorization could be squeezed in there and it's arguably better to
use apply statements but I'd argue you'd loose clarity here) but I
think it shows some useful R idioms that are helpful for someone who
(to use Patrick Burns' phrase) "speaks R with a C accent."
(Incidentally, look up a text called "the R inferno": all sorts of
goodies in there)

library(PearsonDS)

PARAMETERS = data.frame(color = c("red", "blue", "green"), label =
c(1, 2, 3), m = c(1.95, 18.35, 1.93), nu = c(0.08, -1.02, 0.25),
location = c(0.0048,  -0.00254, 0.00189), scale = c(0.0115, 0.082187,
0.026675), stringsAsFactors = FALSE)

x <- seq(-0.06, 0.06, length = 100)

densities <- matrix(NA, nrow = nrow(PARAMETERS), ncol = length(x))
for(i in seq_len(nrow(PARAMETERS))){
densities[i, ] <- with(PARAMETERS[i, ], dpearsonIV(x, m, nu, location, scale))
}

plot(x, rep(0, length(x)), ylim = c(0, max(densities)), xlab = "x
value", ylab = "Density", main = "SPX", type = "n")
for(i in seq_len(nrow(PARAMETERS))){
lines(x, densities[i, ], col = PARAMETERS[i, "color"])
}

legend("topright", title = "Distributions", legend = PARAMETERS[,
"label"], lwd = 1, col = PARAMETERS[, "color"])
grid()

Hope this helps,

Michael

On Sat, Feb 25, 2012 at 2:22 AM, R. Michael Weylandt
<michael.weylandt at gmail.com> <michael.weylandt at gmail.com> wrote:
> Easiest thing to do: use the optional ylim argument to plot (taking values like c(0, maxhx) or possibly a little wider) which will let you set the bounds directly.
>
> Michael
>
> PS It's possible to get the following to be much more easily extensible using loops and what not, but as its very late in my time zone, I'll wait til the morning to work it out (for fear of making yet another public mistake :-P) For now, note that it's perfectly ok to use
>
> maxhx <- max(hx1, hx2, hx3)
>
> which will save you a few lines.
>
> On Feb 24, 2012, at 5:58 PM, FJ M <chicagobrownblue at hotmail.com> wrote:
>
>>
>> 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")
>> 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()
>> }
>>
>>
>> ______________________________________________
>> R-help at r-project.org mailing list
>> https://stat.ethz.ch/mailman/listinfo/r-help