Stochastic Nonsense

Put something smart here.

Multiple Plots and Visualizing Distributions - Part 7 in a Series

This is post #07 in a running series about plotting in R.

I was helping a friend plot some interesting distributions this weekend, so I decided to use distributions to demonstrate one of the neater bits of R’s basic plotting tools: the ability to easily combine plots into a single plot.

par will allow you to manipulate all sorts of parameters to plots — you should eventually poke through the docs. Nonetheless, one of the simplest things to do is to add plots to a matrix plot. You can specify whether you want these to be row major or column major by using the mfrow or mfcol parameter respectively. Let’s take a look, and play with the uniform distribution while we’re at it:

1
2
3
4
5
6
7
8
9
10
11
# create a new plotting window
dev.set(which=1)

# a 1x3 matrix of plots, row major
par(mfrow=c(1,3))

s01=seq(0,1,by=0.01)
plot(x=s01, y=dunif(s01), type='l', main='density', ylab='dunif', xlab='[0,1]')
plot(x=s01, y=punif(s01), type='l', ylim=c(0,1), main='distribution', 
ylab='punif', xlab='[0,1]')
plot(x=s01, y=qunif(s01), type='l', main='quantile', ylab='qunif', xlab='[0,1]')

From left to right, we see the density (dunif), distribution (punif) and quantile (quniff) plots for the Uniform[0,1] distribution. The first thing we might wish to do is to add a label to the left side of the combined plot saying what we’re plotting. To do this, we’ll have to adjust the margins for the combined plot, again using par:

1
2
3
4
5
6
7
8
oma sets the outer margins in terms of lines of text
par(mfrow=c(1,3), oma=c(0,4,0,0))

s=seq(-5,5,by=0.01)
plot(x=s, y=dnorm(s), type='l', main='density', ylab='dnorm', xlab='[-5,5]')
plot(x=s, y=pnorm(s), type='l', ylim=c(0,1), main='distribution', ylab='pnorm', xlab='[-5,5]')
plot(x=s01, y=qnorm(s01), type='l', main='quantile', ylab='qnorm', xlab='[0,1]')
mtext(text='Standard Normal', side=2, line=2, outer=T)

Now, let’s suppose we wanted to use something like a LaTeX expression in our plots — we can do this using expression. For a complete explanation of the syntax, see plotmath.

1
2
3
4
5
6
7
8
9
par(mfrow=c(1,3), oma=c(0,4,0,0))
s <- seq(0,10, by=0.01)
plot(x=s, y=dpois(s, lambda=1), type='l', main='density', ylab=expression(qpois(lambda==1)), xlab='[0,10]')
plot(x=s, y=ppois(s, lambda=1), type='l', ylim=c(0,1), 
  main='distribution', ylab=expression(ppois(lambda==1)), xlab='[0,10]')
plot(x=s01, y=qpois(s01, lambda=1), type='l', 
  main='quantile', ylab=expression(qpois(lambda==1)), xlab='[0,1]')
poisson with lambda = 1
mtext(side=2, line=2, outer=T, text=expression(Poisson(lambda==1)))

Now that we’ve looked at 3 distributions, say we wanted to look at Uniform[0,1], Standard Normal, Exponential(rate=1) and Poisson(lambda=1) all on the same plot. par will allow us to do that:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
par(mfrow=c(4,3), oma=c(0,4,0,0))
s01=seq(0,1,by=0.01)
plot(x=s01, y=dunif(s01), type='l', main='density', ylab='dunif', xlab='[0,1]')
plot(x=s01, y=punif(s01), type='l', ylim=c(0,1), main='distribution', ylab='punif', xlab='[0,1]')
plot(x=s01, y=qunif(s01), type='l', main='quantile', ylab='qunif', xlab='[0,1]')
mtext(text=expression('Uniform'['[0,1]']), side=2, line=2, outer=T)

s=seq(-5,5,by=0.01)
plot(x=s, y=dnorm(s), type='l', main='density', ylab='dnorm', xlab='[-5,5]')
plot(x=s, y=pnorm(s), type='l', ylim=c(0,1), main='distribution', ylab='pnorm', xlab='[-5,5]')
plot(x=s01, y=qnorm(s01), type='l', main='quantile', ylab='qnorm', xlab='[0,1]')
mtext(text='Standard Normal', side=2, line=2, outer=T)

s <- seq(0,10, by=0.01)
plot(x=s, y=dexp(s), type='l', main='density', ylab='dexp', xlab='[0,10]')
plot(x=s, y=pexp(s), type='l', ylim=c(0,1), main='distribution', ylab='pexp', xlab='[0,10]')
plot(x=s01, y=qexp(s01), type='l', main='quantile', ylab='qexp', xlab='[0,1]')
mtext(text='Exponential', side=2, line=2, outer=T)

s <- seq(0,10, by=0.01)
plot(x=s, y=dpois(s, lambda=1), type='l', main='density', ylab=expression(qpois(lambda==1)), xlab='[0,10]')
plot(x=s, y=ppois(s, lambda=1), type='l', ylim=c(0,1), 
  main='distribution', ylab=expression(ppois(lambda==1)), xlab='[0,10]')
plot(x=s01, y=qpois(s01, lambda=1), type='l', 
  main='quantile', ylab=expression(qpois(lambda==1)), xlab='[0,1]')
mtext(side=2, line=2, outer=T, text=expression(Poisson(lambda==1)))

There are a couple issues with this plot, still: there is no main title, the labels we carefully applied with mtext stomped all over each other, and we waste an awful lot of whitespace. Let’s take a stab at fixing all of the above:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#dev.set(which=1)
par(mfrow=c(4,3), oma=c(0,4,4,0), mar=par()$mar*0.4)

s01=seq(0,1,by=0.01)
plot(x=s01, y=dunif(s01), type='l', main='density', ylab='dunif', xlab='[0,1]')
plot(x=s01, y=punif(s01), type='l', ylim=c(0,1), main='distribution', ylab='punif', xlab='[0,1]')
plot(x=s01, y=qunif(s01), type='l', main='quantile', ylab='qunif', xlab='[0,1]')
mtext(text=expression('Uniform'['[0,1]']), side=2, line=2, outer=T, at=0.88)

s=seq(-5,5,by=0.01)
plot(x=s, y=dnorm(s), type='l', main='density', ylab='dnorm', xlab='[-5,5]')
plot(x=s, y=pnorm(s), type='l', ylim=c(0,1), main='distribution', ylab='pnorm', xlab='[-5,5]')
plot(x=s01, y=qnorm(s01), type='l', main='quantile', ylab='qnorm', xlab='[0,1]')
mtext(text='Standard Normal', side=2, line=2, outer=T, at=0.62)



s <- seq(0,10, by=0.01)
plot(x=s, y=dexp(s), type='l', main='density', ylab='dexp', xlab='[0,10]')
plot(x=s, y=pexp(s), type='l', ylim=c(0,1), main='distribution', ylab='pexp', xlab='[0,10]')
plot(x=s01, y=qexp(s01), type='l', main='quantile', ylab='qexp', xlab='[0,1]')
mtext(text='Exponential', side=2, line=2, outer=T, at=0.38)

s <- seq(0,10, by=0.01)
plot(x=s, y=dpois(s, lambda=1), type='l', main='density', ylab=expression(qpois(lambda==1)), xlab='[0,10]')
ere were 50 or more warnings (use warnings() to see the first 50)
plot(x=s, y=ppois(s, lambda=1), type='l', ylim=c(0,1), 
  main='distribution', ylab=expression(ppois(lambda==1)), xlab='[0,10]')
plot(x=s01, y=qpois(s01, lambda=1), type='l', 
  main='quantile', ylab=expression(qpois(lambda==1)), xlab='[0,1]')
mtext(side=2, line=2, outer=T, text=expression(Poisson(lambda==1)), at=0.13)

mtext(text='Interesting Distribution Functions', side=3, line=2, outer=T)