<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Stochastic Nonsense &#187; Statistics</title>
	<atom:link href="http://blog.earlh.com/index.php/category/statistics/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.earlh.com</link>
	<description></description>
	<lastBuildDate>Mon, 19 Sep 2011 03:30:21 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Labeled boxplot in R</title>
		<link>http://blog.earlh.com/index.php/2011/09/labeled-boxplot-in-r/</link>
		<comments>http://blog.earlh.com/index.php/2011/09/labeled-boxplot-in-r/#comments</comments>
		<pubDate>Mon, 19 Sep 2011 03:24:03 +0000</pubDate>
		<dc:creator>earl</dc:creator>
				<category><![CDATA[R]]></category>
		<category><![CDATA[Statistics]]></category>
		<category><![CDATA[Visualization]]></category>

		<guid isPermaLink="false">http://blog.earlh.com/?p=739</guid>
		<description><![CDATA[As generated by R&#8217;s boxplot function. I individually labeled the median, quartiles, min, max, and outliers for inclusion in a presentation where the audience can&#8217;t be assumed to know how to interpret box plots. Please feel free to use this &#8230; <a href="http://blog.earlh.com/index.php/2011/09/labeled-boxplot-in-r/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p><a href="http://blog.earlh.com/wp-content/uploads/2011/09/labeled-boxplot-02.jpg"><img src="http://blog.earlh.com/wp-content/uploads/2011/09/labeled-boxplot-02.jpg" alt="boxplot with labeled parts" title="labeled boxplot 02" width="602" height="792" class="aligncenter size-full wp-image-740" /></a></p>
<p>As generated by R&#8217;s boxplot function.  I individually labeled the median, quartiles, min, max, and outliers for inclusion in a presentation where the audience can&#8217;t be assumed to know how to interpret box plots.  Please feel free to use this image if you have a similar need.</p>
<p>In text, shamelessly stolen from a <a href='http://chartsgraphs.wordpress.com/2008/11/18/boxplots-r-does-them-right/'> climate blog</a>,</p>
<blockquote><p>
The rectangle shows the interquartile range (IQR); it goes from the first quartile (the 25th percentile) to the third quartile (the 75th percentile). The whiskers go from the minimum value to the maximum value unless the distance from the minimum value to the first quartile is more than 1.5 times the IQR. In that case the whisker extends out to the smallest value within 1.5 times the IQR from the first quartile. A similar rule is used for values larger than 1.5 times IQR from the third quartile. A special symbol shows the values, called outliers, which are smaller or larger than the whiskers
</p></blockquote>
]]></content:encoded>
			<wfw:commentRss>http://blog.earlh.com/index.php/2011/09/labeled-boxplot-in-r/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Plotting in Grids</title>
		<link>http://blog.earlh.com/index.php/2009/12/plotting-in-grid/</link>
		<comments>http://blog.earlh.com/index.php/2009/12/plotting-in-grid/#comments</comments>
		<pubDate>Wed, 30 Dec 2009 04:03:20 +0000</pubDate>
		<dc:creator>earl</dc:creator>
				<category><![CDATA[Plotting]]></category>
		<category><![CDATA[R]]></category>
		<category><![CDATA[Statistics]]></category>
		<category><![CDATA[Visualization]]></category>
		<category><![CDATA[plotting series]]></category>

		<guid isPermaLink="false">http://blog.earlh.com/?p=474</guid>
		<description><![CDATA[This is post #12 in a running series about plotting in R. I regularly find myself wanting to show arrays or grids of plots in R. This is straightforward using par and mfrow as long as you want a symmetric, &#8230; <a href="http://blog.earlh.com/index.php/2009/12/plotting-in-grid/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<div style="border: 1px solid rgb(230, 219, 85); margin: 0px auto; padding: 10px; width: 70%; background-color: #F5F5F5; font-size: 0.9em; text-align: center;">
	This is post #12 in a running <a href="http://blog.earlh.com/index.php/plotting-in-r-a-series/"> series </a> about plotting in R.
</div>
<p>I regularly find myself wanting to show arrays or grids of plots in R.  This is straightforward using par and mfrow as long as you want a symmetric, evenly spaced grid of plots.  Unfortunately, this often is not what I want.  Even more unfortunately, this is a hard question to google for.  I&#8217;ve tried array of plots, grid of plots, matrix of plots, asymmetric grids of plots, asymmetric arrays, uneven grids of plots, uneven mfrow, uneven mfcol, etc, and nothing worked.  (Searches listed here in the hopes that other people with the same question will find the answer.)</p>
<p>I actually didn&#8217;t think this could be accomplished without using lattice and ggplot2, but I recently discovered that it can be done with R&#8217;s base plotting functions.  The function layout provides what we&#8217;re looking for.  It takes a matrix describing where you want your sequence of plots to go.  After creating your layout, you can use layout.show to visually see where your plots will go.  Let&#8217;s take a look at some examples.</p>
<p>This creates a two by two grid, exactly as mfrow does.<br />
<code>
<pre class="brush: text;">
# 2 by 2 grid, the same as mfrow=c(2,2)
pp <- layout(matrix(c(1,2,3,4), 2, 2, byrow=T))
layout.show(pp)
</pre>
<p></code><br />
<center><br />
<a href="http://blog.earlh.com/wp-content/uploads/2009/12/plot12.00.png"><img src="http://blog.earlh.com/wp-content/uploads/2009/12/plot12.00-300x300.png" alt="plot12.00" title="plot12.00" width="300" height="300" class="aligncenter size-medium wp-image-485" /></a><br />
</center></p>
<p>For comparison, this creates a 2 by 2 grid as mfcol does.  The only difference is the order of the plot numbers in the matrix.<br />
<code>
<pre class="brush: text;">
# 2 by 2 grid, the same as mfcol=c(2,2)
pp <- layout(matrix(c(1,2,3,4), 2, 2, byrow=F))
layout.show(pp)
</pre>
<p></code><br />
<center><br />
<a href="http://blog.earlh.com/wp-content/uploads/2009/12/plot12.01.png"><img src="http://blog.earlh.com/wp-content/uploads/2009/12/plot12.01-300x300.png" alt="plot12.01" title="plot12.01" width="300" height="300" class="aligncenter size-medium wp-image-485" /></a><br />
</center></p>
<p>We can put 0 in any position in the matrix to not plot there.<br />
<code>
<pre class="brush: text;">
# no plotting in the first quadrant
pp <- layout(matrix(c(1,0,2,3), 2, 2, byrow=T))
layout.show(pp)
</pre>
<p></code><br />
<center><br />
<a href="http://blog.earlh.com/wp-content/uploads/2009/12/plot12.02.png"><img src="http://blog.earlh.com/wp-content/uploads/2009/12/plot12.02-300x300.png" alt="plot12.02" title="plot12.02" width="300" height="300" class="aligncenter size-medium wp-image-485" /></a><br />
</center></p>
<p>Now, let's just have one plot use all of the left column.  The trick to spanning columns like this is to repeat the number of the plot that you want to span -- note that 1 occurs twice in the layout matrix.<br />
<code>
<pre class="brush: text;">
# now a fat plot on the left and two small plots in the right column
pp <- layout(matrix(c(1, 1, 2, 3), 2, 2, byrow=F))
layout.show(pp)
</pre>
<p></code><br />
<center><br />
<a href="http://blog.earlh.com/wp-content/uploads/2009/12/plot12.03.png"><img src="http://blog.earlh.com/wp-content/uploads/2009/12/plot12.03-300x300.png" alt="plot12.03" title="plot12.03" width="300" height="300" class="aligncenter size-medium wp-image-485" /></a><br />
</center></p>
<p>Finally, we can set widths for the columns (or for the rows -- just use heights instead of widths).<br />
<code>
<pre class="brush: text;">
# same as above, but with the left column having 3/4 of the width
pp <- layout(matrix(c(1, 1, 2, 3), 2, 2, byrow=F), widths=c(3,1))
layout.show(pp)
</pre>
<p></code><br />
<center><br />
<a href="http://blog.earlh.com/wp-content/uploads/2009/12/plot12.04.png"><img src="http://blog.earlh.com/wp-content/uploads/2009/12/plot12.04-300x300.png" alt="plot12.04" title="plot12.04" width="300" height="300" class="aligncenter size-medium wp-image-485" /></a><br />
</center></p>
<p>Now, let's show off what I originally wanted to do: display a plot of two dimensions of a distribution, along with the marginal distributions.  I'm wrapping the functionality up into a function so it's easy to reuse.  I use plot to show the sample and barplot to show the distribution as calculated by hist.</p>
<p><code>
<pre class="brush: text;">
# now lets demonstrate with a plot of the multivariate normal and histograms of the marginal distributions
# use package MASS to get the mvrnorm function

plotWithMarginals <- function(x, y){

	# find min / max on each dimension
	# then set up breaks so that even if x, y are on very different ranges things work
	mm <- max(abs(range(x, y)))
	breaks <- seq(-mm, mm, by=(2*mm)/1000)

	hist0 <- hist(x, breaks=breaks, plot=F)
	hist1 <- hist(y, breaks=breaks, plot=F)

	# create a grid and check it out to make sure that it's what we want
	pp <- layout(matrix(c(2,0,1,3), 2, 2, byrow=T), c(3,1), c(1,3), T)
	layout.show(pp)

	rang <- c(-mm, mm)

	par(mar=c(3,3,1,1))
	plot(x, y, xlim=rang, ylim=rang, xlab='', ylab='')

	# now plot marginals
	top <- max(hist0$counts, hist1$counts)
	par(mar=c(0,3,1,1))
	barplot(hist0$counts, axes=F, ylim=c(0, top), space=0)

	par(mar=c(3,0,1,1))
	barplot(hist1$counts, axes=F, xlim=c(0,top), space=0, horiz=T)
}

# mvrnorm <-- sample from a multivariate normal distn
library(MASS)
</pre>
<p></code></p>
<p>Now that all the prep is done, this shows a multivariate normal distribution with no correlation between the two variables.  Note the shape of the marginal distributions.<br />
<code>
<pre class="brush: text;">
eye2 <- matrix(c(1,0,0,1), 2, 2)
sample <- mvrnorm(n=10000, mu=c(0,0), Sigma=eye2)
plotWithMarginals(sample[,1], sample[,2])
</pre>
<p></code></p>
<p><center><br />
<a href="http://blog.earlh.com/wp-content/uploads/2009/12/plot12.05.png"><img src="http://blog.earlh.com/wp-content/uploads/2009/12/plot12.05-300x300.png" alt="plot12.05" title="plot12.05" width="300" height="300" class="aligncenter size-medium wp-image-485" /></a><br />
</center></p>
<p>And finally, for contrast, a correlated multivariate normal.<br />
<code>
<pre class="brush: text;">
yescorr <- matrix(c(1, 0.9, 0.9, 1), 2, 2, byrow=T)
sample <- mvrnorm(n=10000, mu=c(0,0), Sigma=yescorr)
plotWithMarginals(sample[,1], sample[,2])
</pre>
<p></code></p>
<p><center><br />
<a href="http://blog.earlh.com/wp-content/uploads/2009/12/plot12.06.png"><img src="http://blog.earlh.com/wp-content/uploads/2009/12/plot12.06-300x300.png" alt="plot12.06" title="plot12.06" width="300" height="300" class="aligncenter size-medium wp-image-485" /></a></center></p>
]]></content:encoded>
			<wfw:commentRss>http://blog.earlh.com/index.php/2009/12/plotting-in-grid/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Visualizing and Comparing Distributions &#8212; Part 8 of a Series</title>
		<link>http://blog.earlh.com/index.php/2009/07/visualizing-and-comparing-distributions-part-8-of-a-series/</link>
		<comments>http://blog.earlh.com/index.php/2009/07/visualizing-and-comparing-distributions-part-8-of-a-series/#comments</comments>
		<pubDate>Mon, 13 Jul 2009 21:16:32 +0000</pubDate>
		<dc:creator>earl</dc:creator>
				<category><![CDATA[Plotting]]></category>
		<category><![CDATA[R]]></category>
		<category><![CDATA[Statistics]]></category>
		<category><![CDATA[Visualization]]></category>
		<category><![CDATA[plotting series]]></category>

		<guid isPermaLink="false">http://blog.earlh.com/?p=156</guid>
		<description><![CDATA[This is post #08 in a running series about plotting in R. Last time, I talked about visualizing the Uniform, Normal, Exponential, and Poisson Distributions. However, there are more useful methods than just plotting the density and distribution functions. Of &#8230; <a href="http://blog.earlh.com/index.php/2009/07/visualizing-and-comparing-distributions-part-8-of-a-series/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<div style="border: 1px solid rgb(230, 219, 85); margin: 0px auto; padding: 10px; width: 70%; background-color: #F5F5F5; font-size: 0.9em; text-align: center;">
	This is post #08 in a running <a href="http://blog.earlh.com/index.php/plotting-in-r-a-series/"> series </a> about plotting in R.
</div>
<p>Last time, I talked about <a href="http://blog.earlh.com/index.php/2009/07/multiple-plots-and-visualizing-distributions-part-7-in-a-series/"> visualizing the  Uniform, Normal, Exponential, and Poisson Distributions</a>.  However, there are more useful methods than just plotting the density and distribution functions.</p>
<p>Of course, you can always simply ask R to output summary statistics:<br />
<code>
<pre class="brush:text;">
> n <- 10000
> d <- data.frame(unif=runif(n=n), norm=rnorm(n=n), exp=rexp(n=n),
+   pois=rpois(n, lambda=1))
>
> summary(d)
> summary(d)
      unif                norm                exp                 pois
 Min.   :2.449e-05   Min.   :-3.603527   Min.   :0.0001143   Min.   :0.000
 1st Qu.:2.489e-01   1st Qu.:-0.657498   1st Qu.:0.2963731   1st Qu.:0.000
 Median :4.991e-01   Median :-0.003342   Median :0.6927739   Median :1.000
 Mean   :5.001e-01   Mean   : 0.008527   Mean   :0.9958194   Mean   :1.006
 3rd Qu.:7.498e-01   3rd Qu.: 0.662084   3rd Qu.:1.3718103   3rd Qu.:2.000
 Max.   :1.000e+00   Max.   : 3.512077   Max.   :9.4726291   Max.   :6.000
>
</pre>
<p></code></p>
<p>Or perhaps, we could be even more detailed and ask for more finely detailed order statistics.  The quantile function will calculate arbitrary order statistics:<br />
<code>
<pre class="brush:text;">
> quantile(d$unif, probs=seq(0,1,by=0.05))
          0%           5%          10%          15%          20%          25%          30%          35%          40%          45%          50%          55%
2.448517e-05 4.870828e-02 9.949762e-02 1.450521e-01 1.953495e-01 2.488873e-01 3.011948e-01 3.541679e-01 4.020943e-01 4.508098e-01 4.991203e-01 5.527021e-01
         60%          65%          70%          75%          80%          85%          90%          95%         100%
6.038723e-01 6.540685e-01 6.990784e-01 7.498041e-01 7.989884e-01 8.480684e-01 8.982745e-01 9.496064e-01 9.999980e-01
> t(t( quantile(d$unif, probs=seq(0,1,by=0.05)) ))
             [,1]
0%   2.448517e-05
5%   4.870828e-02
10%  9.949762e-02
15%  1.450521e-01
20%  1.953495e-01
25%  2.488873e-01
30%  3.011948e-01
35%  3.541679e-01
40%  4.020943e-01
45%  4.508098e-01
50%  4.991203e-01
55%  5.527021e-01
60%  6.038723e-01
65%  6.540685e-01
70%  6.990784e-01
75%  7.498041e-01
80%  7.989884e-01
85%  8.480684e-01
90%  8.982745e-01
95%  9.496064e-01
100% 9.999980e-01
>
> s <- seq(0,1,by=0.02)
> e <- sapply(d, FUN=function(x) quantile(x, probs=s))
> e <- as.data.frame(e)
> e[1:10,]
            unif       norm          exp pois
0%  2.448517e-05 -3.6035270 0.0001142514    0
2%  1.900289e-02 -1.9884255 0.0226651895    0
4%  3.844486e-02 -1.7161347 0.0440634106    0
6%  6.012384e-02 -1.5041045 0.0634013971    0
8%  8.089568e-02 -1.3566198 0.0899735826    0
10% 9.949762e-02 -1.2407524 0.1103189153    0
12% 1.191770e-01 -1.1392377 0.1342348062    0
14% 1.369209e-01 -1.0483029 0.1590086026    0
16% 1.555910e-01 -0.9618975 0.1829207834    0
18% 1.748496e-01 -0.8799168 0.2056880019    0
>
</pre>
<p></code></p>
<p>Plotting the quantile values is worth a shot, just to see what we get:<br />
<code>
<pre class="brush: text;">
>
> plot(x=s, y=e$unif, ylim=c(min(e), max(e)), type='b', pch='.', cex=3, lwd=2, col='black',
+ 	xlab='quantiles [0,100]', ylab='dist values', main='Quantile Plots, Four Distributions')
> for(j in 2:4){
+ 	points(x=s, y=e[,j], type='b', pch='.', cex=3, lwd=2, col=c('', 'red', 'blue', 'green')[j])
+ }
> legend(x=0, y=8, legend=colnames(e), col=c('black', 'red', 'blue', 'green'), lwd=3)
>
</pre>
<p></code><br />
<div id="attachment_164" class="wp-caption aligncenter" style="width: 490px"><a href="http://blog.earlh.com/wp-content/uploads/2009/07/plot08.04.png"><img src="http://blog.earlh.com/wp-content/uploads/2009/07/plot08.04.png" alt="Quantile Plots, Four Distributions" title="plot08.04" width="480" height="480" class="size-full wp-image-164" /></a><p class="wp-caption-text">Quantile Plots, Four Distributions</p></div></p>
<p>Quantile quantile plots are typically used when you want to see if a sample is a particular distribution.  You plot the quantiles of the sample versus the assumed distribution and compare; if they are the same distribution you should get a straight line at roughly a forty five degree angle.<br />
<code>
<pre class="brush: text;">
> par(mfrow=c(1,2), oma=c(0,0,2,0))
>
> qqplot(x=d$exp, y=d$pois,
+ 	xlab='exponential', ylab='poisson', main = 'qq plot: different distributions')
> qqplot(x=d$norm, y=rnorm(n=n),
+ 	xlab='our normal sample in d', ylab='new normal sample', main = 'qq plot: same distribution, resampled')
>
> mtext('QQ plots -- different vs same distributions', side=3, outer=T)
</pre>
<p></code><br />
<div id="attachment_160" class="wp-caption aligncenter" style="width: 685px"><a href="http://blog.earlh.com/wp-content/uploads/2009/07/plot08.01.png"><img src="http://blog.earlh.com/wp-content/uploads/2009/07/plot08.01.png" alt="QQ Plots - Different Distribution and Same Distribution" title="plot08.01" width="675" height="450" class="size-full wp-image-160" /></a><p class="wp-caption-text">QQ Plots - Different Distribution and Same Distribution</p></div></p>
<p>Box and whisker plots<br />
<code>
<pre class="brush: text;">
> dev.set(which=1)
> boxplot(x=d, xlab='distributions', main='Distributions Visualization - boxplot')
</pre>
<p></code><br />
<div id="attachment_161" class="wp-caption aligncenter" style="width: 810px"><a href="http://blog.earlh.com/wp-content/uploads/2009/07/plot08.02.png"><img src="http://blog.earlh.com/wp-content/uploads/2009/07/plot08.02.png" alt="Box and Whisker Plot, Four Distributions" title="plot08.02" width="800" height="900" class="size-full wp-image-161" /></a><p class="wp-caption-text">Box and Whisker Plot, Four Distributions</p></div></p>
<p>Finally, for univariate distributions, I prefer box-percentile plots.  These are similar to boxplots, but the width of the distribution graphs are proportional to the percent of observations more extreme in that direction.  They are also marked at the 25th, 50th, and 75th percentiles.</p>
<p><code>
<pre class="brush: text;">
> library(Hmisc)
>
> dev.set(which=1)
> bpplot(d, xlab='distributions', main='Distributions Visualization - box percentile plot')
>
</pre>
<p></code><br />
<div id="attachment_162" class="wp-caption aligncenter" style="width: 810px"><a href="http://blog.earlh.com/wp-content/uploads/2009/07/plot08.03.png"><img src="http://blog.earlh.com/wp-content/uploads/2009/07/plot08.03.png" alt="Box-Percentile Plot, 4 Distributions" title="plot08.03" width="800" height="900" class="size-full wp-image-162" /></a><p class="wp-caption-text">Box-Percentile Plot, 4 Distributions</p></div></p>
<p>And if the distributions are genuinely different, let&#8217;s examine 5 univariate Normal Distributions: our already sampled N(0,1), a resampled N(0,1), N(1,1), N(0,3), N(-1,0.5).  Side by side, the box percentile plot really draws out the differences in the distributions.<br />
<code>
<pre class="brush: text;">
>
> d2 = data.frame(norm=d$norm, resample=rnorm(n), mean1=rnorm(n=n, mean=1), sd3=rnorm(n=n, sd=3), sdhalf=rnorm(n=n, sd=0.5, mean=-2))
>
> bpplot(d2, xlab='distributions', main='Multiple Normal Distribution Visualization - box percentile plot')
>
</pre>
<p></code><br />
<div id="attachment_171" class="wp-caption aligncenter" style="width: 810px"><a href="http://blog.earlh.com/wp-content/uploads/2009/07/plot08.05.png"><img src="http://blog.earlh.com/wp-content/uploads/2009/07/plot08.05.png" alt="Box Percentile Plot, Five Different Normal Distributions" title="plot08.05" width="800" height="900" class="size-full wp-image-171" /></a><p class="wp-caption-text">Box Percentile Plot, Five Different Normal Distributions</p></div></p>
]]></content:encoded>
			<wfw:commentRss>http://blog.earlh.com/index.php/2009/07/visualizing-and-comparing-distributions-part-8-of-a-series/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Multiple Plots and Visualizing Distributions &#8211; Part 7 in a Series</title>
		<link>http://blog.earlh.com/index.php/2009/07/multiple-plots-and-visualizing-distributions-part-7-in-a-series/</link>
		<comments>http://blog.earlh.com/index.php/2009/07/multiple-plots-and-visualizing-distributions-part-7-in-a-series/#comments</comments>
		<pubDate>Mon, 13 Jul 2009 02:50:18 +0000</pubDate>
		<dc:creator>earl</dc:creator>
				<category><![CDATA[Plotting]]></category>
		<category><![CDATA[R]]></category>
		<category><![CDATA[Statistics]]></category>
		<category><![CDATA[Visualization]]></category>
		<category><![CDATA[plotting series]]></category>

		<guid isPermaLink="false">http://blog.earlh.com/?p=145</guid>
		<description><![CDATA[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&#8217;s basic plotting &#8230; <a href="http://blog.earlh.com/index.php/2009/07/multiple-plots-and-visualizing-distributions-part-7-in-a-series/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<div style="border: 1px solid rgb(230, 219, 85); margin: 0px auto; padding: 10px; width: 70%; background-color: #F5F5F5; font-size: 0.9em; text-align: center;">
	This is post #07 in a running <a href="http://blog.earlh.com/index.php/plotting-in-r-a-series/"> series </a> about plotting in R.
</div>
<p>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&#8217;s basic plotting tools: the ability to easily combine plots into a single plot.</p>
<p>par will allow you to manipulate all sorts of parameters to plots &#8212; 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&#8217;s take a look, and play with the uniform distribution while we&#8217;re at it:<br />
<code>
<pre class="brush:text;">
> # 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]')
>
</pre>
<p></code><br />
 Distribution Density, Distribution, and Quantile Plots Side-by-side&#8221;]<a href="http://blog.earlh.com/wp-content/uploads/2009/07/plot07.01.png"><img src="http://blog.earlh.com/wp-content/uploads/2009/07/plot07.01.png" alt="Uniform[0,1] Distribution Density, Distribution, and Quantile Plots Side-by-side" title="plot07.01" width="600" height="300" class="size-full wp-image-148" /></a></p>
<p>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&#8217;re plotting.  To do this, we&#8217;ll have to adjust the margins for the combined plot, again using par:<br />
<code>
<pre class="brush:text;">
# 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)
>
</pre>
<p></code><br />
<div id="attachment_149" class="wp-caption aligncenter" style="width: 610px"><a href="http://blog.earlh.com/wp-content/uploads/2009/07/plot07.02.png"><img src="http://blog.earlh.com/wp-content/uploads/2009/07/plot07.02.png" alt="Standard Normal Distribution Density, Distribution, and Quantile Plots Side-by-side" title="plot07.02" width="600" height="300" class="size-full wp-image-149" /></a><p class="wp-caption-text">Standard Normal Distribution Density, Distribution, and Quantile Plots Side-by-side</p></div></p>
<p>Now, let&#8217;s suppose we wanted to use something like a LaTeX expression in our plots &#8212; we can do this using expression.  For a complete explanation of the syntax, see plotmath.<br />
<code>
<pre class="brush:text;">
> 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)))
</pre>
<p></code><br />
<div id="attachment_150" class="wp-caption aligncenter" style="width: 610px"><a href="http://blog.earlh.com/wp-content/uploads/2009/07/plot07.03.png"><img src="http://blog.earlh.com/wp-content/uploads/2009/07/plot07.03.png" alt="Poisson (lambda=1) Distribution Density, Distribution, and Quantile Plots Side-by-side" title="plot07.03" width="600" height="300" class="size-full wp-image-150" /></a><p class="wp-caption-text">Poisson (lambda=1) Distribution Density, Distribution, and Quantile Plots Side-by-side</p></div></p>
<p>Now that we&#8217;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:<br />
<code>
<pre class="brush:text;">
> 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)))
</pre>
<p></code><br />
, Normal(0,1), Exponential(1), and Poisson(1) Distribution Plots&#8221;]<a href="http://blog.earlh.com/wp-content/uploads/2009/07/plot07.04.png"><img src="http://blog.earlh.com/wp-content/uploads/2009/07/plot07.04.png" alt="Uniform[0,1], Normal(0,1), Exponential(1), and Poisson(1) Distribution Plots" title="plot07.04" width="800" height="900" class="size-full wp-image-151" /></a></p>
<p>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&#8217;s take a stab at fixing all of the above:<br />
<code>
<pre class="brush:text;">
> #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)
>
</pre>
<p></code><br />
, Normal(0,1), Exponential(1), and Poisson(1) Distribution Plots &#8212; Nicely Formatted and Labeled&#8221;]<a href="http://blog.earlh.com/wp-content/uploads/2009/07/plot07.05.png"><img src="http://blog.earlh.com/wp-content/uploads/2009/07/plot07.05.png" alt="Uniform[0,1], Normal(0,1), Exponential(1), and Poisson(1) Distribution Plots -- Nicely Formatted and Labeled" title="plot07.05" width="800" height="900" class="size-full wp-image-152" /></a></p>
]]></content:encoded>
			<wfw:commentRss>http://blog.earlh.com/index.php/2009/07/multiple-plots-and-visualizing-distributions-part-7-in-a-series/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>

