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:

> # 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]')
>


Distribution Density, Distribution, and Quantile Plots Side-by-side”]Uniform[0,1] Distribution Density, Distribution, and Quantile Plots Side-by-side

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:

# 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)
>


Standard Normal Distribution Density, Distribution, and Quantile Plots Side-by-side

Standard Normal Distribution Density, Distribution, and Quantile Plots Side-by-side

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.

> 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)))


Poisson (lambda=1) Distribution Density, Distribution, and Quantile Plots Side-by-side

Poisson (lambda=1) Distribution Density, Distribution, and Quantile Plots Side-by-side

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:

> 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)))


, Normal(0,1), Exponential(1), and Poisson(1) Distribution Plots”]Uniform[0,1], Normal(0,1), Exponential(1), and Poisson(1) Distribution Plots

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:

> #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]')
There 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)
>


, Normal(0,1), Exponential(1), and Poisson(1) Distribution Plots — Nicely Formatted and Labeled”]Uniform[0,1], Normal(0,1), Exponential(1), and Poisson(1) Distribution Plots -- Nicely Formatted and Labeled

This entry was posted in Plotting, R, Statistics, Visualization and tagged , . Bookmark the permalink.

One Response to Multiple Plots and Visualizing Distributions – Part 7 in a Series

  1. Pingback: Stochastic Nonsense | Visualizing and Comparing Distributions — Part 8 of a Series

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>