<?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/"
	xmlns:georss="http://www.georss.org/georss" xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#" xmlns:media="http://search.yahoo.com/mrss/"
	>

<channel>
	<title>Learning R</title>
	<atom:link href="http://learnr.wordpress.com/feed/" rel="self" type="application/rss+xml" />
	<link>http://learnr.wordpress.com</link>
	<description>Finding my way around R</description>
	<lastBuildDate>Tue, 24 Jan 2012 18:43:09 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.com/</generator>
<cloud domain='learnr.wordpress.com' port='80' path='/?rsscloud=notify' registerProcedure='' protocol='http-post' />
<image>
		<url>http://s2.wp.com/i/buttonw-com.png</url>
		<title>Learning R</title>
		<link>http://learnr.wordpress.com</link>
	</image>
	<atom:link rel="search" type="application/opensearchdescription+xml" href="http://learnr.wordpress.com/osd.xml" title="Learning R" />
	<atom:link rel='hub' href='http://learnr.wordpress.com/?pushpress=hub'/>
		<item>
		<title>Consultants&#8217; Chart in ggplot2</title>
		<link>http://learnr.wordpress.com/2010/08/16/consultants-chart-in-ggplot2/</link>
		<comments>http://learnr.wordpress.com/2010/08/16/consultants-chart-in-ggplot2/#comments</comments>
		<pubDate>Mon, 16 Aug 2010 08:25:00 +0000</pubDate>
		<dc:creator>learnr</dc:creator>
				<category><![CDATA[R]]></category>
		<category><![CDATA[chart]]></category>
		<category><![CDATA[excel]]></category>
		<category><![CDATA[ggplot2]]></category>
		<category><![CDATA[polar]]></category>
		<category><![CDATA[rose]]></category>

		<guid isPermaLink="false">http://learnr.wordpress.com/?p=2445</guid>
		<description><![CDATA[Excel Charts Blog posted a video tutorial of how to create a circumplex or rose or dougnut chart in Excel. Apparently this type of chart is very popular in the consulting industry, hence the &#8220;Consultants&#8217; Chart&#8221;. It is very easy to make this chart in Excel 2010, but it involves countless number of clicks and [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=learnr.wordpress.com&amp;blog=6877108&amp;post=2445&amp;subd=learnr&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>Excel Charts Blog <a href="http://www.excelcharts.com/blog/the-consultants-chart-revisited/">posted</a> a video tutorial of how to create a circumplex or rose or dougnut chart in Excel. Apparently this type of chart is very popular in the consulting industry, hence the &#8220;Consultants&#8217; Chart&#8221;.</p>
<div> <img src="http://learnr.files.wordpress.com/2010/08/consulting.gif?w=600" style="border-width:0;" alt="consulting.gif"> </div>
<p>It is very easy to make this chart in Excel 2010, but it involves countless number of clicks and formulas to format both the source data and the chart itself.</p>
<p>In <a href="http://had.co.nz/ggplot2">ggplot2</a> the same can be achieved with around 10 lines of code, as can be seen below.</p>
<p> <span id="more-2445"></span><br />
<hr />
<h2><a name="_set_up_dummy_dataframe"></a>Set up dummy dataframe</h2>
<table border="0" bgcolor="#e8e8e8" width="100%" style="margin:.2em 0;">
<tr>
<td style="padding:.5em;">
<pre style="margin:0;padding:0;">&gt; set.seed(9876)
&gt; DF &lt;- data.frame(variable = 1:10, value = sample(10,
+     replace = TRUE))
&gt; DF
   variable value
1         1     9
2         2     4
3         3     2
4         4     6
5         5     5
6         6     3
7         7     5
8         8     7
9         9     6
10       10     2</pre>
</td>
</tr>
</table>
<hr />
<h2><a name="_prepare_the_charts"></a>Prepare the charts</h2>
<table border="0" bgcolor="#e8e8e8" width="100%" style="margin:.2em 0;">
<tr>
<td style="padding:.5em;">
<pre style="margin:0;padding:0;">&gt; library(ggplot2)</pre>
</td>
</tr>
</table>
<p>Dougnut chart is essentially a bar chart using a polar coordinate system, so we start off with a simple bar chart. And to make life a bit more colourful will fill each slice with a different colour.</p>
<table border="0" bgcolor="#e8e8e8" width="100%" style="margin:.2em 0;">
<tr>
<td style="padding:.5em;">
<pre style="margin:0;padding:0;">&gt; ggplot(DF, aes(factor(variable), value, fill = factor(variable))) +
+     geom_bar(width = 1)</pre>
</td>
</tr>
</table>
<div> <img src="http://learnr.files.wordpress.com/2010/08/rose_circumplex_chart-005.png?w=600" style="border-width:0;" alt="rose_circumplex_chart-005.png"> </div>
<p>Next the coordinate system is changed and some of the plot elements are removed for a cleaner look.</p>
<table border="0" bgcolor="#e8e8e8" width="100%" style="margin:.2em 0;">
<tr>
<td style="padding:.5em;">
<pre style="margin:0;padding:0;">&gt; last_plot() + scale_y_continuous(breaks = 0:10) +
+     coord_polar() + labs(x = "", y = "") + opts(legend.position = "none",
+     axis.text.x = theme_blank(), axis.text.y = theme_blank(),
+     axis.ticks = theme_blank())</pre>
</td>
</tr>
</table>
<div> <img src="http://learnr.files.wordpress.com/2010/08/rose_circumplex_chart-007.png?w=600" style="border-width:0;" alt="rose_circumplex_chart-007.png"> </div>
<hr />
<h2><a name="_adding_gridlines"></a>Adding gridlines</h2>
<p><strong>Update 17 August 2010</strong></p>
<p>Several commentators asked how to draw gridlines on top of the slices as in the original example.</p>
<p>Putting the gridlines/guides on top of the plot is accomplished by adding a new variable that is purely used for drawing the borders of each slice element. Essentially, it stacks the required number of slices with a white border on top of each other.</p>
<table border="0" bgcolor="#e8e8e8" width="100%" style="margin:.2em 0;">
<tr>
<td style="padding:.5em;">
<pre style="margin:0;padding:0;">&gt; DF &lt;- ddply(DF, .(variable), transform, border = rep(1,
+     value))
&gt; head(DF, 10)
   variable value border
1         1     9      1
2         1     9      1
3         1     9      1
4         1     9      1
5         1     9      1
6         1     9      1
7         1     9      1
8         1     9      1
9         1     9      1
10        2     4      1</pre>
</td>
</tr>
</table>
<table border="0" bgcolor="#e8e8e8" width="100%" style="margin:.2em 0;">
<tr>
<td style="padding:.5em;">
<pre style="margin:0;padding:0;">&gt; ggplot(DF, aes(factor(variable))) + geom_bar(width = 1,
+     aes(y = value, fill = factor(variable))) +
+     geom_bar(aes(y = border, width = 1), position = "stack",
+         stat = "identity", fill = NA, colour = "white") +
+     scale_y_continuous(breaks = 0:10) + coord_polar() +
+     labs(x = "", y = "") + opts(legend.position = "none",
+     axis.text.x = theme_blank(), axis.text.y = theme_blank(),
+     axis.ticks = theme_blank())</pre>
</td>
</tr>
</table>
<div> <img src="http://learnr.files.wordpress.com/2010/08/rose_circumplex_chart-010.png?w=600" style="border-width:0;" alt="rose_circumplex_chart-010.png"> </div>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/learnr.wordpress.com/2445/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/learnr.wordpress.com/2445/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/learnr.wordpress.com/2445/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/learnr.wordpress.com/2445/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/learnr.wordpress.com/2445/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/learnr.wordpress.com/2445/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/learnr.wordpress.com/2445/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/learnr.wordpress.com/2445/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/learnr.wordpress.com/2445/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/learnr.wordpress.com/2445/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/learnr.wordpress.com/2445/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/learnr.wordpress.com/2445/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/learnr.wordpress.com/2445/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/learnr.wordpress.com/2445/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=learnr.wordpress.com&amp;blog=6877108&amp;post=2445&amp;subd=learnr&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://learnr.wordpress.com/2010/08/16/consultants-chart-in-ggplot2/feed/</wfw:commentRss>
		<slash:comments>26</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/971b564fa9d5442aa0bf880c3d768cc2?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">learnr</media:title>
		</media:content>

		<media:content url="http://learnr.files.wordpress.com/2010/08/consulting.gif" medium="image">
			<media:title type="html">consulting.gif</media:title>
		</media:content>

		<media:content url="http://learnr.files.wordpress.com/2010/08/rose_circumplex_chart-005.png" medium="image">
			<media:title type="html">rose_circumplex_chart-005.png</media:title>
		</media:content>

		<media:content url="http://learnr.files.wordpress.com/2010/08/rose_circumplex_chart-007.png" medium="image">
			<media:title type="html">rose_circumplex_chart-007.png</media:title>
		</media:content>

		<media:content url="http://learnr.files.wordpress.com/2010/08/rose_circumplex_chart-010.png" medium="image">
			<media:title type="html">rose_circumplex_chart-010.png</media:title>
		</media:content>
	</item>
		<item>
		<title>ggplot2: Waterfall Charts</title>
		<link>http://learnr.wordpress.com/2010/05/10/ggplot2-waterfall-charts/</link>
		<comments>http://learnr.wordpress.com/2010/05/10/ggplot2-waterfall-charts/#comments</comments>
		<pubDate>Mon, 10 May 2010 12:15:20 +0000</pubDate>
		<dc:creator>learnr</dc:creator>
				<category><![CDATA[R]]></category>
		<category><![CDATA[cascade]]></category>
		<category><![CDATA[ggplot2]]></category>
		<category><![CDATA[waterfall]]></category>

		<guid isPermaLink="false">http://learnr.wordpress.com/?p=2418</guid>
		<description><![CDATA[Waterfall charts are often used for analytical purposes in the business setting to show the effect of sequentially introduced negative and/or positive values. Sometimes waterfall charts are also referred to as cascade charts. In the next few paragraphs I will show how to plot a waterfall chart using ggplot2. Data A very small fictional dataset [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=learnr.wordpress.com&amp;blog=6877108&amp;post=2418&amp;subd=learnr&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>Waterfall charts are often used for analytical purposes in the business setting to show the effect of sequentially introduced negative and/or positive values. Sometimes waterfall charts are also referred to as cascade charts.</p>
<p>In the next few paragraphs I will show how to plot a waterfall chart using <a href="http://had.co.nz/ggplot2/">ggplot2</a>.</p>
<p><span id="more-2418"></span></p>
<hr />
<h2><a name="_data"></a>Data</h2>
<p>A very small fictional dataset depicting the changes to a company cash position, found in a <a href="http://www.freakalytics.com/2009/11/17/wc/">blogpost</a> showing how to prepare a waterfall chart in Tableau.</p>
<table style="margin:.2em 0;" border="0" width="100%" bgcolor="#e8e8e8">
<tbody>
<tr>
<td style="padding:.5em;">
<pre style="margin:0;padding:0;">&gt; balance &lt;- data.frame(desc = c("Starting Cash",
+     "Sales", "Refunds", "Payouts", "Court Losses",
+     "Court Wins", "Contracts", "End Cash"), amount = c(2000,
+     3400, -1100, -100, -6600, 3800, 1400, 2800))
&gt; balance
           desc amount
1 Starting Cash   2000
2         Sales   3400
3       Refunds  -1100
4       Payouts   -100
5  Court Losses  -6600
6    Court Wins   3800
7     Contracts   1400
8      End Cash   2800</pre>
</td>
</tr>
</tbody>
</table>
<p>In order to preserve the order of the lines in a dataframe I convert the <tt>desc</tt> variable to a factor; <tt>id</tt> and <tt>type</tt> variable are also added:</p>
<table style="margin:.2em 0;" border="0" width="100%" bgcolor="#e8e8e8">
<tbody>
<tr>
<td style="padding:.5em;">
<pre style="margin:0;padding:0;">&gt; balance$desc &lt;- factor(balance$desc, levels = balance$desc)
&gt; balance$id &lt;- seq_along(balance$amount)
&gt; balance$type &lt;- ifelse(balance$amount &gt; 0, "in",
+     "out")
&gt; balance[balance$desc %in% c("Starting Cash", "End Cash"),
+     "type"] &lt;- "net"</pre>
</td>
</tr>
</tbody>
</table>
<p>Next the data will be slightly reworked to specify the coordinates for drawing the waterfall bars.</p>
<table style="margin:.2em 0;" border="0" width="100%" bgcolor="#e8e8e8">
<tbody>
<tr>
<td style="padding:.5em;">
<pre style="margin:0;padding:0;">&gt; balance$end &lt;- cumsum(balance$amount)
&gt; balance$end &lt;- c(head(balance$end, -1), 0)
&gt; balance$start &lt;- c(0, head(balance$end, -1))
&gt; balance &lt;- balance[, c(3, 1, 4, 6, 5, 2)]
&gt; balance
  id          desc type start   end amount
1  1 Starting Cash  net     0  2000   2000
2  2         Sales   in  2000  5400   3400
3  3       Refunds  out  5400  4300  -1100
4  4       Payouts  out  4300  4200   -100
5  5  Court Losses  out  4200 -2400  -6600
6  6    Court Wins   in -2400  1400   3800
7  7     Contracts   in  1400  2800   1400
8  8      End Cash  net  2800     0   2800</pre>
</td>
</tr>
</tbody>
</table>
<hr />
<h2><a name="_plotting"></a>Plotting</h2>
<p>Now everything is set to plot the first waterfall chart. <tt>geom_rect</tt> is used to draw the rectangles using the coordinates calculated in the previous step.</p>
<table style="margin:.2em 0;" border="0" width="100%" bgcolor="#e8e8e8">
<tbody>
<tr>
<td style="padding:.5em;">
<pre style="margin:0;padding:0;">&gt; library(ggplot2)</pre>
</td>
</tr>
</tbody>
</table>
<table style="margin:.2em 0;" border="0" width="100%" bgcolor="#e8e8e8">
<tbody>
<tr>
<td style="padding:.5em;">
<pre style="margin:0;padding:0;">&gt; ggplot(balance, aes(desc, fill = type)) + geom_rect(aes(x = desc,
+     xmin = id - 0.45, xmax = id + 0.45, ymin = end,
+     ymax = start))</pre>
</td>
</tr>
</tbody>
</table>
<div><img style="border-width:0;" src="http://learnr.files.wordpress.com/2010/05/waterfall-007.png?w=600" alt="waterfall-007.png" /></div>
<p>The fill mapping could use some tweaking (my preference is to have outflows in red, inflows in green, and net position in blue), for that I change the order of the underlying factor levels.</p>
<table style="margin:.2em 0;" border="0" width="100%" bgcolor="#e8e8e8">
<tbody>
<tr>
<td style="padding:.5em;">
<pre style="margin:0;padding:0;">&gt; balance$type &lt;- factor(balance$type, levels = c("out",
+     "in", "net"))</pre>
</td>
</tr>
</tbody>
</table>
<p>Almost ready, one more tweak to the x-axis labels: the helper function below replaces spaces with new lines, making the labels more readable.</p>
<table style="margin:.2em 0;" border="0" width="100%" bgcolor="#e8e8e8">
<tbody>
<tr>
<td style="padding:.5em;">
<pre style="margin:0;padding:0;">&gt; strwr &lt;- function(str) gsub(" ", "\n", str)</pre>
</td>
</tr>
</tbody>
</table>
<table style="margin:.2em 0;" border="0" width="100%" bgcolor="#e8e8e8">
<tbody>
<tr>
<td style="padding:.5em;">
<pre style="margin:0;padding:0;">&gt; (p1 &lt;- ggplot(balance, aes(fill = type)) + geom_rect(aes(x = desc,
+     xmin = id - 0.45, xmax = id + 0.45, ymin = end,
+     ymax = start)) + scale_y_continuous("", formatter = "comma") +
+     scale_x_discrete("", breaks = levels(balance$desc),
+         labels = strwr(levels(balance$desc))) +
+     opts(legend.position = "none"))</pre>
</td>
</tr>
</tbody>
</table>
<div><img style="border-width:0;" src="http://learnr.files.wordpress.com/2010/05/waterfall-011.png?w=600" alt="waterfall-011.png" /></div>
<p>Finally, the bar labels are also added (the conditional positioning of them is quite a lengthy process, as you can see).</p>
<table style="margin:.2em 0;" border="0" width="100%" bgcolor="#e8e8e8">
<tbody>
<tr>
<td style="padding:.5em;">
<pre style="margin:0;padding:0;">&gt; p1 + geom_text(subset = .(type == "in"), aes(id,
+     end, label = comma(amount)), vjust = 1, size = 3) +
+     geom_text(subset = .(type == "out"), aes(id,
+         end, label = comma(amount)), vjust = -0.3,
+         size = 3) + geom_text(data = subset(balance,
+     type == "net" &amp; id == min(id)), aes(id, end,
+     colour = type, label = comma(end), vjust = ifelse(end &lt;
+         start, 1, -0.3)), size = 3.5) + geom_text(data = subset(balance,
+     type == "net" &amp; id == max(id)), aes(id, start,
+     colour = type, label = comma(start), vjust = ifelse(end &lt;
+         start, -0.3, 1)), size = 3.5)</pre>
</td>
</tr>
</tbody>
</table>
<div><img style="border-width:0;" src="http://learnr.files.wordpress.com/2010/05/waterfall-013.png?w=600" alt="waterfall-013.png" /></div>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/learnr.wordpress.com/2418/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/learnr.wordpress.com/2418/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/learnr.wordpress.com/2418/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/learnr.wordpress.com/2418/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/learnr.wordpress.com/2418/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/learnr.wordpress.com/2418/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/learnr.wordpress.com/2418/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/learnr.wordpress.com/2418/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/learnr.wordpress.com/2418/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/learnr.wordpress.com/2418/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/learnr.wordpress.com/2418/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/learnr.wordpress.com/2418/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/learnr.wordpress.com/2418/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/learnr.wordpress.com/2418/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=learnr.wordpress.com&amp;blog=6877108&amp;post=2418&amp;subd=learnr&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://learnr.wordpress.com/2010/05/10/ggplot2-waterfall-charts/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/971b564fa9d5442aa0bf880c3d768cc2?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">learnr</media:title>
		</media:content>

		<media:content url="http://learnr.files.wordpress.com/2010/05/waterfall-007.png" medium="image">
			<media:title type="html">waterfall-007.png</media:title>
		</media:content>

		<media:content url="http://learnr.files.wordpress.com/2010/05/waterfall-011.png" medium="image">
			<media:title type="html">waterfall-011.png</media:title>
		</media:content>

		<media:content url="http://learnr.files.wordpress.com/2010/05/waterfall-013.png" medium="image">
			<media:title type="html">waterfall-013.png</media:title>
		</media:content>
	</item>
		<item>
		<title>ggplot2: Changing the Default Order of Legend Labels and Stacking of Data</title>
		<link>http://learnr.wordpress.com/2010/03/23/ggplot2-changing-the-default-order-of-legend-labels-and-stacking-of-data/</link>
		<comments>http://learnr.wordpress.com/2010/03/23/ggplot2-changing-the-default-order-of-legend-labels-and-stacking-of-data/#comments</comments>
		<pubDate>Tue, 23 Mar 2010 08:50:37 +0000</pubDate>
		<dc:creator>learnr</dc:creator>
				<category><![CDATA[R]]></category>
		<category><![CDATA[ggplot2]]></category>
		<category><![CDATA[labels]]></category>
		<category><![CDATA[legend]]></category>
		<category><![CDATA[stacking]]></category>

		<guid isPermaLink="false">http://learnr.wordpress.com/?p=2405</guid>
		<description><![CDATA[&#8220;How to change the order of legend labels&#8221; is a question that gets asked relatively often on ggplot2 mailing list. A variation of this question is how to change the order of series in stacked bar/lineplots. While these two questions seem to be related, in fact they are separate as the legend is controlled by [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=learnr.wordpress.com&amp;blog=6877108&amp;post=2405&amp;subd=learnr&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>&#8220;How to change the order of legend labels&#8221; is a question that gets asked relatively often on <a href="http://groups.google.com/group/ggplot2/">ggplot2 mailing list</a>. A variation of this question is how to change the order of series in stacked bar/lineplots.</p>
<p>While these two questions seem to be related, in fact they are separate as the legend is controlled by scales, whereas stacking is controlled by the order of values in the data.</p>
<p>Recently I spent some time getting my head around this, and below is a quick recap.</p>
<p> <span id="more-2405"></span><br />
<hr />
<h2><a name="_changing_the_ordering_of_legend_labels"></a>Changing the Ordering of Legend Labels</h2>
<p>The standard stacked barplot looks like this:</p>
<table border="0" bgcolor="#e8e8e8" width="100%" style="margin:.2em 0;">
<tr>
<td style="padding:.5em;">
<pre style="margin:0;padding:0;">&gt; library(ggplot2)</pre>
</td>
</tr>
</table>
<table border="0" bgcolor="#e8e8e8" width="100%" style="margin:.2em 0;">
<tr>
<td style="padding:.5em;">
<pre style="margin:0;padding:0;">&gt; ggplot(diamonds, aes(clarity, fill = cut)) + geom_bar()</pre>
</td>
</tr>
</table>
<div> <img src="http://learnr.files.wordpress.com/2010/03/order_variable-0041.png?w=600" style="border-width:0;" alt="order_variable-004.png"> </div>
<p>You notice that in the legend &#8220;Fair&#8221; is at the top and &#8220;Ideal&#8221; at the bottom. But what if I would like to order the labels in the reverse order, so that &#8220;Ideal&#8221; would be at the top?</p>
<p>The order of legend labels can be manipulated by reordering the factor levels of the <tt>cut</tt> variable mapped to <tt>fill</tt> aesthetic.</p>
<table border="0" bgcolor="#e8e8e8" width="100%" style="margin:.2em 0;">
<tr>
<td style="padding:.5em;">
<pre style="margin:0;padding:0;">&gt; levels(diamonds$cut)
[1] "Fair"      "Good"      "Very Good" "Premium"
[5] "Ideal"
&gt; diamonds$cut &lt;- factor(diamonds$cut, levels = rev(levels(diamonds$cut)))
&gt; levels(diamonds$cut)
[1] "Ideal"     "Premium"   "Very Good" "Good"
[5] "Fair"</pre>
</td>
</tr>
</table>
<table border="0" bgcolor="#e8e8e8" width="100%" style="margin:.2em 0;">
<tr>
<td style="padding:.5em;">
<pre style="margin:0;padding:0;">&gt; ggplot(diamonds, aes(clarity, fill = cut)) + geom_bar()</pre>
</td>
</tr>
</table>
<div> <img src="http://learnr.files.wordpress.com/2010/03/order_variable-0071.png?w=600" style="border-width:0;" alt="order_variable-007.png"> </div>
<p>The legend entries are now in reverse order (and so is the stacking).</p>
<hr />
<h2><a name="_changing_data_stacking_order"></a>Changing Data Stacking Order</h2>
<p>The <tt>order</tt> aesthetic changes the order in which the areas are stacked on top of each other.</p>
<p>The following aligns the order of both the labels and the stacking.</p>
<table border="0" bgcolor="#e8e8e8" width="100%" style="margin:.2em 0;">
<tr>
<td style="padding:.5em;">
<pre style="margin:0;padding:0;">&gt; ggplot(diamonds, aes(clarity, fill = cut, order = -as.numeric(cut))) +
+     geom_bar()</pre>
</td>
</tr>
</table>
<div> <img src="http://learnr.files.wordpress.com/2010/03/order_variable-0111.png?w=600" style="border-width:0;" alt="order_variable-011.png"> </div>
<p>Or, alternatively, reordering the factor levels again:</p>
<table border="0" bgcolor="#e8e8e8" width="100%" style="margin:.2em 0;">
<tr>
<td style="padding:.5em;">
<pre style="margin:0;padding:0;">&gt; diamonds$cut &lt;- factor(diamonds$cut, levels = rev(levels(diamonds$cut)))
&gt; ggplot(diamonds, aes(clarity, fill = cut, order = -as.numeric(cut))) +
+     geom_bar()</pre>
</td>
</tr>
</table>
<div> <img src="http://learnr.files.wordpress.com/2010/03/order_variable-0131.png?w=600" style="border-width:0;" alt="order_variable-013.png"> </div>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/learnr.wordpress.com/2405/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/learnr.wordpress.com/2405/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/learnr.wordpress.com/2405/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/learnr.wordpress.com/2405/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/learnr.wordpress.com/2405/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/learnr.wordpress.com/2405/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/learnr.wordpress.com/2405/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/learnr.wordpress.com/2405/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/learnr.wordpress.com/2405/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/learnr.wordpress.com/2405/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/learnr.wordpress.com/2405/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/learnr.wordpress.com/2405/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/learnr.wordpress.com/2405/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/learnr.wordpress.com/2405/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=learnr.wordpress.com&amp;blog=6877108&amp;post=2405&amp;subd=learnr&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://learnr.wordpress.com/2010/03/23/ggplot2-changing-the-default-order-of-legend-labels-and-stacking-of-data/feed/</wfw:commentRss>
		<slash:comments>16</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/971b564fa9d5442aa0bf880c3d768cc2?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">learnr</media:title>
		</media:content>

		<media:content url="http://learnr.files.wordpress.com/2010/03/order_variable-0041.png" medium="image">
			<media:title type="html">order_variable-004.png</media:title>
		</media:content>

		<media:content url="http://learnr.files.wordpress.com/2010/03/order_variable-0071.png" medium="image">
			<media:title type="html">order_variable-007.png</media:title>
		</media:content>

		<media:content url="http://learnr.files.wordpress.com/2010/03/order_variable-0111.png" medium="image">
			<media:title type="html">order_variable-011.png</media:title>
		</media:content>

		<media:content url="http://learnr.files.wordpress.com/2010/03/order_variable-0131.png" medium="image">
			<media:title type="html">order_variable-013.png</media:title>
		</media:content>
	</item>
		<item>
		<title>ggplot2: Plotting Dates, Hours and Minutes</title>
		<link>http://learnr.wordpress.com/2010/02/25/ggplot2-plotting-dates-hours-and-minutes/</link>
		<comments>http://learnr.wordpress.com/2010/02/25/ggplot2-plotting-dates-hours-and-minutes/#comments</comments>
		<pubDate>Wed, 24 Feb 2010 20:49:23 +0000</pubDate>
		<dc:creator>learnr</dc:creator>
				<category><![CDATA[R]]></category>
		<category><![CDATA[ggplot2]]></category>
		<category><![CDATA[maptools]]></category>
		<category><![CDATA[timeseries]]></category>

		<guid isPermaLink="false">http://learnr.wordpress.com/?p=2352</guid>
		<description><![CDATA[Plotting timeseries with dates on x-axis and times on y-axis can be a bit tricky in ggplot2. However, with a little trick this problem can be easily overcome. Let’s assume that I wanted to plot when the sun rises in London in 2010. sunriset function in maptools package calculates the sunrise times using algorithms provided [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=learnr.wordpress.com&amp;blog=6877108&amp;post=2352&amp;subd=learnr&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>Plotting timeseries with dates on x-axis and times on y-axis can be a bit tricky in <a href="http://had.co.nz/ggplot2">ggplot2</a>. However, with a little trick this problem can be easily overcome.</p>
<p><span id="more-2352"></span></p>
<p>Let’s assume that I wanted to plot when the sun rises in London in 2010.</p>
<p><tt>sunriset</tt> function in <a href="http://cran.r-project.org/web/packages/maptools/index.html"><tt>maptools</tt></a> package calculates the sunrise times using algorithms provided by the National Oceanic &amp; Atmospheric Administration (NOAA). I need to input the location details in a matrix format and specify for which dates I require the sunrise times.</p>
<table style="margin:.2em 0;" border="0" width="100%" bgcolor="#e8e8e8">
<tbody>
<tr>
<td style="padding:.5em;">
<pre style="margin:0;padding:0;">&gt; library(maptools)</pre>
</td>
</tr>
</tbody>
</table>
<table style="margin:.2em 0;" border="0" width="100%" bgcolor="#e8e8e8">
<tbody>
<tr>
<td style="padding:.5em;">
<pre style="margin:0;padding:0;">&gt; x_seq &lt;- seq(from = as.POSIXct("2010-01-01", tz = "GMT"),
+     length.out = 365, by = "days")
&gt; coord &lt;- matrix(c(-0.13, 51.5), nrow = 1)</pre>
</td>
</tr>
</tbody>
</table>
<table style="margin:.2em 0;" border="0" width="100%" bgcolor="#e8e8e8">
<tbody>
<tr>
<td style="padding:.5em;">
<pre style="margin:0;padding:0;">&gt; sunrise &lt;- sunriset(coord, x_seq, direction = "sunrise",
+     POSIXct.out = TRUE)
&gt; head(sunrise, 3)
   day_frac                time
1 0.3376285 2010-01-01 08:06:11
2 0.3375441 2010-01-02 08:06:03
3 0.3374209 2010-01-03 08:05:53</pre>
</td>
</tr>
</tbody>
</table>
<p>The time variable now includes information about both the date and time of sunrise in class <tt>POSIXct</tt>.</p>
<p>I would like to plot date on x-axis and time on y-axis, thus the time element needs to be extracted first. However, as the times must be in <tt>POSIXct</tt> (only times of class <tt>POSIXct</tt> are supported in <tt>ggplot2</tt>), a two-step conversion is needed.</p>
<p>First the time is converted to a character vector, effectively stripping all the date information. The time is then converted back to <tt>POSIXct</tt> with today’s date &#8211; the date is of no interest to us, only the hours-minutes-seconds are.</p>
<table style="margin:.2em 0;" border="0" width="100%" bgcolor="#e8e8e8">
<tbody>
<tr>
<td style="padding:.5em;">
<pre style="margin:0;padding:0;">&gt; sunrise$hms &lt;- format(sunrise$time, format = "%H:%M:%S")
&gt; sunrise$hms &lt;- as.POSIXct(sunrise$hms, format = "%H:%M:%S")
&gt; head(sunrise, 3)
   day_frac                time                 hms
1 0.3376285 2010-01-01 08:06:11 2010-01-20 08:06:11
2 0.3375441 2010-01-02 08:06:03 2010-01-20 08:06:03
3 0.3374209 2010-01-03 08:05:53 2010-01-20 08:05:53</pre>
</td>
</tr>
</tbody>
</table>
<p>Now everything is set for plotting.</p>
<table style="margin:.2em 0;" border="0" width="100%" bgcolor="#e8e8e8">
<tbody>
<tr>
<td style="padding:.5em;">
<pre style="margin:0;padding:0;">&gt; library(ggplot2)</pre>
</td>
</tr>
</tbody>
</table>
<table style="margin:.2em 0;" border="0" width="100%" bgcolor="#e8e8e8">
<tbody>
<tr>
<td style="padding:.5em;">
<pre style="margin:0;padding:0;">&gt; ggplot(sunrise, aes(time, hms)) + geom_line()</pre>
</td>
</tr>
</tbody>
</table>
<div><img style="border-width:0;" src="http://learnr.files.wordpress.com/2010/01/hours_minutes_sunrise-008.png?w=600" alt="hours_minutes_sunrise-008.png" /></div>
<p>The default x-axis labels could be made somewhat clearer:</p>
<table style="margin:.2em 0;" border="0" width="100%" bgcolor="#e8e8e8">
<tbody>
<tr>
<td style="padding:.5em;">
<pre style="margin:0;padding:0;">&gt; last_plot() + scale_x_datetime("", format = "%b") +
+     ylab("")</pre>
</td>
</tr>
</tbody>
</table>
<div><img style="border-width:0;" src="http://learnr.files.wordpress.com/2010/01/hours_minutes_sunrise-010.png?w=600" alt="hours_minutes_sunrise-010.png" /></div>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/learnr.wordpress.com/2352/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/learnr.wordpress.com/2352/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/learnr.wordpress.com/2352/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/learnr.wordpress.com/2352/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/learnr.wordpress.com/2352/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/learnr.wordpress.com/2352/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/learnr.wordpress.com/2352/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/learnr.wordpress.com/2352/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/learnr.wordpress.com/2352/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/learnr.wordpress.com/2352/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/learnr.wordpress.com/2352/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/learnr.wordpress.com/2352/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/learnr.wordpress.com/2352/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/learnr.wordpress.com/2352/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=learnr.wordpress.com&amp;blog=6877108&amp;post=2352&amp;subd=learnr&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://learnr.wordpress.com/2010/02/25/ggplot2-plotting-dates-hours-and-minutes/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/971b564fa9d5442aa0bf880c3d768cc2?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">learnr</media:title>
		</media:content>

		<media:content url="http://learnr.files.wordpress.com/2010/01/hours_minutes_sunrise-008.png" medium="image">
			<media:title type="html">hours_minutes_sunrise-008.png</media:title>
		</media:content>

		<media:content url="http://learnr.files.wordpress.com/2010/01/hours_minutes_sunrise-010.png" medium="image">
			<media:title type="html">hours_minutes_sunrise-010.png</media:title>
		</media:content>
	</item>
		<item>
		<title>ggplot2: Quick Heatmap Plotting</title>
		<link>http://learnr.wordpress.com/2010/01/26/ggplot2-quick-heatmap-plotting/</link>
		<comments>http://learnr.wordpress.com/2010/01/26/ggplot2-quick-heatmap-plotting/#comments</comments>
		<pubDate>Mon, 25 Jan 2010 21:18:59 +0000</pubDate>
		<dc:creator>learnr</dc:creator>
				<category><![CDATA[R]]></category>
		<category><![CDATA[ggplot2]]></category>
		<category><![CDATA[heatmap]]></category>
		<category><![CDATA[plot]]></category>

		<guid isPermaLink="false">http://learnr.wordpress.com/?p=2380</guid>
		<description><![CDATA[A post on FlowingData blog demonstrated how to quickly make a heatmap below using R base graphics. This post shows how to achieve a very similar result using ggplot2. Data Import FlowingData used last season&#8217;s NBA basketball statistics provided by databasebasketball.com, and the csv-file with the data can be downloaded directly from its website. &#62; [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=learnr.wordpress.com&amp;blog=6877108&amp;post=2380&amp;subd=learnr&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>A post on FlowingData blog <a href="http://flowingdata.com/2010/01/21/how-to-make-a-heatmap-a-quick-and-easy-solution/">demonstrated</a> how to quickly make a heatmap below using R base graphics.</p>
<p>This post shows how to achieve a very similar result using <a href="http://had.co.nz/ggplot2/">ggplot2</a>.</p>
<div> <img src="http://learnr.files.wordpress.com/2010/01/nba_heatmap_revised.png?w=600" style="border-width:0;" alt="nba_heatmap_revised.png"> </div>
<p> <span id="more-2380"></span><br />
<hr />
<h2><a name="_data_import"></a>Data Import</h2>
<p>FlowingData used last season&#8217;s NBA basketball statistics provided by <a href="http://databasebasketball.com/">databasebasketball.com</a>, and the csv-file with the data can be downloaded directly from its website.</p>
<table border="0" bgcolor="#e8e8e8" width="100%" style="margin:.2em 0;">
<tr>
<td style="padding:.5em;">
<pre style="margin:0;padding:0;">&gt; nba &lt;- read.csv("http://datasets.flowingdata.com/ppg2008.csv")</pre>
</td>
</tr>
</table>
<p>The players are ordered by points scored, and the Name variable converted to a factor that ensures proper sorting of the plot.</p>
<table border="0" bgcolor="#e8e8e8" width="100%" style="margin:.2em 0;">
<tr>
<td style="padding:.5em;">
<pre style="margin:0;padding:0;">&gt; nba$Name &lt;- with(nba, reorder(Name, PTS))</pre>
</td>
</tr>
</table>
<p>Whilst FlowingData uses <tt>heatmap</tt> function in the <tt>stats</tt>-package that requires the plotted values to be in matrix format, <tt>ggplot2</tt> operates with dataframes. For ease of processing, the dataframe is converted from wide format to a long format.</p>
<p>The game statistics have very different ranges, so to make them comparable all the individual statistics are rescaled.</p>
<table border="0" bgcolor="#e8e8e8" width="100%" style="margin:.2em 0;">
<tr>
<td style="padding:.5em;">
<pre style="margin:0;padding:0;">&gt; library(ggplot2)</pre>
</td>
</tr>
</table>
<table border="0" bgcolor="#e8e8e8" width="100%" style="margin:.2em 0;">
<tr>
<td style="padding:.5em;">
<pre style="margin:0;padding:0;">&gt; nba.m &lt;- melt(nba)
&gt; nba.m &lt;- ddply(nba.m, .(variable), transform,
+     rescale = rescale(value))</pre>
</td>
</tr>
</table>
<hr />
<h2><a name="_plotting"></a>Plotting</h2>
<p>There is no specific heatmap plotting function in <tt>ggplot2</tt>, but combining <tt>geom_tile</tt> with a smooth gradient fill does the job very well.</p>
<table border="0" bgcolor="#e8e8e8" width="100%" style="margin:.2em 0;">
<tr>
<td style="padding:.5em;">
<pre style="margin:0;padding:0;">&gt; (p &lt;- ggplot(nba.m, aes(variable, Name)) + geom_tile(aes(fill = rescale),
+     colour = "white") + scale_fill_gradient(low = "white",
+     high = "steelblue"))</pre>
</td>
</tr>
</table>
<div> <img src="http://learnr.files.wordpress.com/2010/01/basketball_heatmap-008.png?w=600" style="border-width:0;" alt="basketball_heatmap-008.png"> </div>
<p>A few finishing touches to the formatting, and the heatmap plot is ready for presentation.</p>
<table border="0" bgcolor="#e8e8e8" width="100%" style="margin:.2em 0;">
<tr>
<td style="padding:.5em;">
<pre style="margin:0;padding:0;">&gt; base_size &lt;- 9
&gt; p + theme_grey(base_size = base_size) + labs(x = "",
+     y = "") + scale_x_discrete(expand = c(0, 0)) +
+     scale_y_discrete(expand = c(0, 0)) + opts(legend.position = "none",
+     axis.ticks = theme_blank(), axis.text.x = theme_text(size = base_size *
+         0.8, angle = 330, hjust = 0, colour = "grey50"))</pre>
</td>
</tr>
</table>
<div> <img src="http://learnr.files.wordpress.com/2010/01/basketball_heatmap-010.png?w=600" style="border-width:0;" alt="basketball_heatmap-010.png"> </div>
<hr />
<h2><a name="_rescaling_update"></a>Rescaling Update</h2>
<p>In preparing the data for the above plot all the variables were rescaled so that they were between 0 and 1.</p>
<p>Jim rightly pointed out in the comments (and I did not initally get it) that the <tt>heatmap</tt>-function uses a different scaling method and therefore the plots are not identical. Below is an updated version of the heatmap which looks much more similar to the original.</p>
<table border="0" bgcolor="#e8e8e8" width="100%" style="margin:.2em 0;">
<tr>
<td style="padding:.5em;">
<pre style="margin:0;padding:0;">&gt; nba.s &lt;- ddply(nba.m, .(variable), transform,
+     rescale = scale(value))</pre>
</td>
</tr>
</table>
<table border="0" bgcolor="#e8e8e8" width="100%" style="margin:.2em 0;">
<tr>
<td style="padding:.5em;">
<pre style="margin:0;padding:0;">&gt; last_plot() %+% nba.s</pre>
</td>
</tr>
</table>
<div> <img src="http://learnr.files.wordpress.com/2010/01/basketball_heatmap-013.png?w=600" style="border-width:0;" alt="basketball_heatmap-013.png"> </div>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/learnr.wordpress.com/2380/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/learnr.wordpress.com/2380/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/learnr.wordpress.com/2380/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/learnr.wordpress.com/2380/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/learnr.wordpress.com/2380/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/learnr.wordpress.com/2380/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/learnr.wordpress.com/2380/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/learnr.wordpress.com/2380/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/learnr.wordpress.com/2380/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/learnr.wordpress.com/2380/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/learnr.wordpress.com/2380/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/learnr.wordpress.com/2380/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/learnr.wordpress.com/2380/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/learnr.wordpress.com/2380/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=learnr.wordpress.com&amp;blog=6877108&amp;post=2380&amp;subd=learnr&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://learnr.wordpress.com/2010/01/26/ggplot2-quick-heatmap-plotting/feed/</wfw:commentRss>
		<slash:comments>68</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/971b564fa9d5442aa0bf880c3d768cc2?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">learnr</media:title>
		</media:content>

		<media:content url="http://learnr.files.wordpress.com/2010/01/nba_heatmap_revised.png" medium="image">
			<media:title type="html">nba_heatmap_revised.png</media:title>
		</media:content>

		<media:content url="http://learnr.files.wordpress.com/2010/01/basketball_heatmap-008.png" medium="image">
			<media:title type="html">basketball_heatmap-008.png</media:title>
		</media:content>

		<media:content url="http://learnr.files.wordpress.com/2010/01/basketball_heatmap-010.png" medium="image">
			<media:title type="html">basketball_heatmap-010.png</media:title>
		</media:content>

		<media:content url="http://learnr.files.wordpress.com/2010/01/basketball_heatmap-013.png" medium="image">
			<media:title type="html">basketball_heatmap-013.png</media:title>
		</media:content>
	</item>
		<item>
		<title>ggplot2: Crayola Crayon Colours</title>
		<link>http://learnr.wordpress.com/2010/01/21/ggplot2-crayola-crayon-colours/</link>
		<comments>http://learnr.wordpress.com/2010/01/21/ggplot2-crayola-crayon-colours/#comments</comments>
		<pubDate>Thu, 21 Jan 2010 07:03:04 +0000</pubDate>
		<dc:creator>learnr</dc:creator>
				<category><![CDATA[R]]></category>
		<category><![CDATA[colour]]></category>
		<category><![CDATA[crayola]]></category>
		<category><![CDATA[ggplot2]]></category>

		<guid isPermaLink="false">http://learnr.wordpress.com/?p=2363</guid>
		<description><![CDATA[Statistical Algorithms blog attempted to recreate a graph depicting the growing colour selection of Crayola crayons in ggplot2 (original graph below via FlowingData). He also asked the following questions: Is there an easier way to do this? How can I make the axes more like the original? What about the white lines between boxes and [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=learnr.wordpress.com&amp;blog=6877108&amp;post=2363&amp;subd=learnr&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>Statistical Algorithms blog attempted to <a href="http://www.statalgo.com/2010/01/20/mosaic-time-series-in-r/">recreate</a> a graph depicting the growing colour selection of Crayola crayons in <a href="http://had.co.nz/ggplot2/">ggplot2</a> (<a href="http://www.weathersealed.com/2010/01/15/color-me-a-dinosaur/">original graph</a> below via <a href="http://flowingdata.com/2010/01/19/crayola-crayon-colors-multiply-like-rabits/">FlowingData</a>).</p>
<p>He also asked the following questions: Is there an easier way to do this? How can I make the axes more like the original? What about the white lines between boxes and the gradual change between years? The sort order is also different.</p>
<p>I will present my version in this post, trying to address some of these questions.</p>
<div><img style="border-width:0;" src="http://learnr.files.wordpress.com/2010/01/crayons_small.png?w=600" alt="crayons_small.png" /></div>
<p><span id="more-2363"></span></p>
<p><strong>Data Import</strong></p>
<p>The list of Crayola crayon colours is available on Wikipedia, and also contains one duplicate colour (#FF1DCE) that was excluded to make further processing easier.</p>
<table style="margin:.2em 0;" border="0" width="100%" bgcolor="#e8e8e8">
<tbody>
<tr>
<td style="padding:.5em;">
<pre style="margin:0;padding:0;">&gt; library(XML)
&gt; library(ggplot2)</pre>
</td>
</tr>
</tbody>
</table>
<table style="margin:.2em 0;" border="0" width="100%" bgcolor="#e8e8e8">
<tbody>
<tr>
<td style="padding:.5em;">
<pre style="margin:0;padding:0;">&gt; theurl &lt;- "http://en.wikipedia.org/wiki/List_of_Crayola_crayon_colors"
&gt; html &lt;- htmlParse(theurl)
&gt; crayola &lt;- readHTMLTable(html, stringsAsFactors = FALSE)[[2]]
&gt; crayola &lt;- crayola[, c("Hex Code", "Issued", "Retired")]
&gt; names(crayola) &lt;- c("colour", "issued", "retired")
&gt; crayola &lt;- crayola[!duplicated(crayola$colour),
+     ]
&gt; crayola$retired[crayola$retired == ""] &lt;- 2010</pre>
</td>
</tr>
</tbody>
</table>
<p><strong>Plotting</strong></p>
<p>Instead of <tt>geom_rect()</tt> I will show two options of plotting the same data using <tt>geom_bar()</tt> and <tt>geom_area()</tt> to plot the data, and need to ensure that there’s one entry per colour per year it was(is) in the production.</p>
<table style="margin:.2em 0;" border="0" width="100%" bgcolor="#e8e8e8">
<tbody>
<tr>
<td style="padding:.5em;">
<pre style="margin:0;padding:0;">&gt; colours &lt;- ddply(crayola, .(colour), transform,
+     year = issued:retired)</pre>
</td>
</tr>
</tbody>
</table>
<p>The plot colours are manually mapped to the original colours using <tt>scale_fill_identity()</tt>.</p>
<table style="margin:.2em 0;" border="0" width="100%" bgcolor="#e8e8e8">
<tbody>
<tr>
<td style="padding:.5em;">
<pre style="margin:0;padding:0;">&gt; p &lt;- ggplot(colours, aes(year, 1, fill = colour)) +
+     geom_bar(width = 1, position = "fill", binwidth = 1) +
+     theme_bw() + scale_fill_identity()</pre>
</td>
</tr>
</tbody>
</table>
<div><img style="border-width:0;" src="http://learnr.files.wordpress.com/2010/01/crayola_colours-006.png?w=600" alt="crayola_colours-006.png" /></div>
<p>And now the <tt>geom_area()</tt> version:</p>
<table style="margin:.2em 0;" border="0" width="100%" bgcolor="#e8e8e8">
<tbody>
<tr>
<td style="padding:.5em;">
<pre style="margin:0;padding:0;">&gt; p1 &lt;- ggplot(colours, aes(year, 1, fill = colour)) +
+     geom_area(position = "fill", colour = "white") +
+     theme_bw() + scale_fill_identity()</pre>
</td>
</tr>
</tbody>
</table>
<div><img style="border-width:0;" src="http://learnr.files.wordpress.com/2010/01/crayola_colours-0081.png?w=600" alt="crayola_colours-008.png" /></div>
<p><strong>Final Formatting</strong></p>
<p>Next, the x-axis labels suggested by <tt>ggplot2</tt> will be manualy overridden. Also I use a little trick to make sure that the labels are properly aligned.</p>
<table style="margin:.2em 0;" border="0" width="100%" bgcolor="#e8e8e8">
<tbody>
<tr>
<td style="padding:.5em;">
<pre style="margin:0;padding:0;">&gt; labels &lt;- c(1903, 1949, 1958, 1972, 1990, 1998,
+     2010)
&gt; breaks &lt;- labels - 1
&gt; x &lt;- scale_x_continuous("", breaks = breaks, labels = labels,
+     expand = c(0, 0))
&gt; y &lt;- scale_y_continuous("", expand = c(0, 0))
&gt; ops &lt;- opts(axis.text.y = theme_blank(), axis.ticks = theme_blank())</pre>
</td>
</tr>
</tbody>
</table>
<table style="margin:.2em 0;" border="0" width="100%" bgcolor="#e8e8e8">
<tbody>
<tr>
<td style="padding:.5em;">
<pre style="margin:0;padding:0;">&gt; p + x + y + ops</pre>
</td>
</tr>
</tbody>
</table>
<div><img style="border-width:0;" src="http://learnr.files.wordpress.com/2010/01/crayola_colours-011.png?w=600" alt="crayola_colours-011.png" /></div>
<table style="margin:.2em 0;" border="0" width="100%" bgcolor="#e8e8e8">
<tbody>
<tr>
<td style="padding:.5em;">
<pre style="margin:0;padding:0;">&gt; p1 + x + y + ops</pre>
</td>
</tr>
</tbody>
</table>
<div><img style="border-width:0;" src="http://learnr.files.wordpress.com/2010/01/crayola_colours-0131.png?w=600" alt="crayola_colours-013.png" /></div>
<p><del>The order of colours could be changed by sorting the colours by some common feature, unfortunately I did not find an automated way of doing this.</del></p>
<p><strong>Sorting by Colour</strong></p>
<p>Thanks to Baptiste who showed a way to sort the colours, the final version of the area plot resembles the original even more closely.</p>
<table style="margin:.2em 0;" border="0" width="100%" bgcolor="#e8e8e8">
<tbody>
<tr>
<td style="padding:.5em;">
<pre style="margin:0;padding:0;">&gt; library(colorspace)</pre>
</td>
</tr>
</tbody>
</table>
<table style="margin:.2em 0;" border="0" width="100%" bgcolor="#e8e8e8">
<tbody>
<tr>
<td style="padding:.5em;">
<pre style="margin:0;padding:0;">&gt; sort.colours &lt;- function(col) {
+     c.rgb = col2rgb(col)
+     c.RGB = RGB(t(c.rgb) %*% diag(rep(1/255, 3)))
+     c.HSV = as(c.RGB, "HSV")@coords
+     order(c.HSV[, 1], c.HSV[, 2], c.HSV[, 3])
+ }
&gt; colours = ddply(colours, .(year), function(d) d[rev(sort.colours(d$colour)),
+     ])</pre>
</td>
</tr>
</tbody>
</table>
<table style="margin:.2em 0;" border="0" width="100%" bgcolor="#e8e8e8">
<tbody>
<tr>
<td style="padding:.5em;">
<pre style="margin:0;padding:0;">&gt; last_plot() %+% colours</pre>
</td>
</tr>
</tbody>
</table>
<div><img style="border-width:0;" src="http://learnr.files.wordpress.com/2010/01/crayola_colours-017.png?w=600" alt="crayola_colours-017.png" /></div>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/learnr.wordpress.com/2363/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/learnr.wordpress.com/2363/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/learnr.wordpress.com/2363/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/learnr.wordpress.com/2363/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/learnr.wordpress.com/2363/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/learnr.wordpress.com/2363/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/learnr.wordpress.com/2363/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/learnr.wordpress.com/2363/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/learnr.wordpress.com/2363/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/learnr.wordpress.com/2363/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/learnr.wordpress.com/2363/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/learnr.wordpress.com/2363/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/learnr.wordpress.com/2363/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/learnr.wordpress.com/2363/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=learnr.wordpress.com&amp;blog=6877108&amp;post=2363&amp;subd=learnr&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://learnr.wordpress.com/2010/01/21/ggplot2-crayola-crayon-colours/feed/</wfw:commentRss>
		<slash:comments>20</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/971b564fa9d5442aa0bf880c3d768cc2?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">learnr</media:title>
		</media:content>

		<media:content url="http://learnr.files.wordpress.com/2010/01/crayons_small.png" medium="image">
			<media:title type="html">crayons_small.png</media:title>
		</media:content>

		<media:content url="http://learnr.files.wordpress.com/2010/01/crayola_colours-006.png" medium="image">
			<media:title type="html">crayola_colours-006.png</media:title>
		</media:content>

		<media:content url="http://learnr.files.wordpress.com/2010/01/crayola_colours-0081.png" medium="image">
			<media:title type="html">crayola_colours-008.png</media:title>
		</media:content>

		<media:content url="http://learnr.files.wordpress.com/2010/01/crayola_colours-011.png" medium="image">
			<media:title type="html">crayola_colours-011.png</media:title>
		</media:content>

		<media:content url="http://learnr.files.wordpress.com/2010/01/crayola_colours-0131.png" medium="image">
			<media:title type="html">crayola_colours-013.png</media:title>
		</media:content>

		<media:content url="http://learnr.files.wordpress.com/2010/01/crayola_colours-017.png" medium="image">
			<media:title type="html">crayola_colours-017.png</media:title>
		</media:content>
	</item>
		<item>
		<title>New Features in ggplot2 version 0.8.5</title>
		<link>http://learnr.wordpress.com/2010/01/07/new-features-in-ggplot2-version-0-8-5/</link>
		<comments>http://learnr.wordpress.com/2010/01/07/new-features-in-ggplot2-version-0-8-5/#comments</comments>
		<pubDate>Wed, 06 Jan 2010 22:15:50 +0000</pubDate>
		<dc:creator>learnr</dc:creator>
				<category><![CDATA[R]]></category>
		<category><![CDATA[ggplot2]]></category>

		<guid isPermaLink="false">http://learnr.wordpress.com/?p=2339</guid>
		<description><![CDATA[Just before Christmas ggplot2 version 0.8.5 was released, closely following the release of version 0.8.4 a week or so earlier. Whilst both versions included included numerous bugfixes (25 in 0.8.4 and 17 in 0.8.5), the latest version also incorporated some new features. As ggplot2 is all about graphical display, so I went through the list [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=learnr.wordpress.com&amp;blog=6877108&amp;post=2339&amp;subd=learnr&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>Just before Christmas <a href="http://had.co.nz/ggplot2/">ggplot2</a> version 0.8.5 was <a href="http://groups.google.com/group/ggplot2/msg/30945c996bccb8da">released</a>, closely following the <a href="http://groups.google.com/group/ggplot2/msg/ad83036a709cb4ab">release</a> of version 0.8.4 a week or so earlier. Whilst both versions included included numerous bugfixes (25 in 0.8.4 and 17 in 0.8.5), the latest version also incorporated some new features.</p>
<p>As <tt>ggplot2</tt> is all about graphical display, so I went through the list of new features and below is a visual example of each new feature, plotted most often utilising the code examples included in the respective <a href="http://github.com/hadley/ggplot2/issues">bugtracker</a> issues.</p>
<p> <span id="more-2339"></span>
<p><strong>1)geom_text gains parse argument which makes it possible to display expressions</strong></p>
<table border="0" bgcolor="#e8e8e8" width="100%" style="margin:.2em 0;">
<tr>
<td style="padding:.5em;">
<pre style="margin:0;padding:0;">&gt; library(ggplot2)</pre>
</td>
</tr>
</table>
<table border="0" bgcolor="#e8e8e8" width="100%" style="margin:.2em 0;">
<tr>
<td style="padding:.5em;">
<pre style="margin:0;padding:0;">&gt; set.seed(1)
&gt; mydata &lt;- data.frame(x = sample(10), y = sample(10))
&gt; ggplot(mydata, aes(x, y)) + geom_point() + annotate("text",
+     x = mydata[6, 1], y = mydata[6, 2], label = "beta[1] == 1",
+     parse = T, vjust = 0, hjust = -0.1)</pre>
</td>
</tr>
</table>
<div> <img src="http://learnr.files.wordpress.com/2010/01/ggplot2_0-8-5-004.png?w=600" style="border-width:0;" alt="ggplot2_0.8.5-004.png"> </div>
<p><strong>2) all scales now have legend parameter, which defaults to TRUE. Setting to false will prevent that scale from contributing to the legend.</strong></p>
<table border="0" bgcolor="#e8e8e8" width="100%" style="margin:.2em 0;">
<tr>
<td style="padding:.5em;">
<pre style="margin:0;padding:0;">&gt; ggplot(mydata, aes(x, y, colour = x)) + geom_point(legend = FALSE) +
+     geom_line()</pre>
</td>
</tr>
</table>
<div> <img src="http://learnr.files.wordpress.com/2010/01/ggplot2_0-8-5-006.png?w=600" style="border-width:0;" alt="ggplot2_0.8.5-006.png"> </div>
<p>In previous version the legend of the above plot looked like this and there was now way to change this.</p>
<table border="0" bgcolor="#e8e8e8" width="100%" style="margin:.2em 0;">
<tr>
<td style="padding:.5em;">
<pre style="margin:0;padding:0;">&gt; ggplot(mydata, aes(x, y, colour = x)) + geom_point() +
+     geom_line()</pre>
</td>
</tr>
</table>
<div> <img src="http://learnr.files.wordpress.com/2010/01/ggplot2_0-8-5-008.png?w=600" style="border-width:0;" alt="ggplot2_0.8.5-008.png"> </div>
<p><strong>3) default axis labels and legend titles are now stored in the options, instead of in each scale.</strong></p>
<p>This allows to specify them using the <tt>opts</tt> call.</p>
<table border="0" bgcolor="#e8e8e8" width="100%" style="margin:.2em 0;">
<tr>
<td style="padding:.5em;">
<pre style="margin:0;padding:0;">&gt; p &lt;- ggplot(mtcars, aes(cyl, mpg, colour = cyl)) +
+     geom_point()
&gt; p + opts(labels = c(x = "Cylinders", y = "Miles per gallon",
+     colour = "Colour"))</pre>
</td>
</tr>
</table>
<div> <img src="http://learnr.files.wordpress.com/2010/01/ggplot2_0-8-5-010.png?w=600" style="border-width:0;" alt="ggplot2_0.8.5-010.png"> </div>
<p>The other options to set labels exist as previously:</p>
<table border="0" bgcolor="#e8e8e8" width="100%" style="margin:.2em 0;">
<tr>
<td style="padding:.5em;">
<pre style="margin:0;padding:0;">&gt; p + labs(x = "Cylinders")
&gt; p + xlab("Cylinders")
&gt; p + scale_colour_gradient(name = "Colour")</pre>
</td>
</tr>
</table>
<p><strong>4) coord_equal: when ratio = NULL (the default), it will adjust the aspect ratio of the plot, rather than trying to extend the shortest axis.</strong></p>
<table border="0" bgcolor="#e8e8e8" width="100%" style="margin:.2em 0;">
<tr>
<td style="padding:.5em;">
<pre style="margin:0;padding:0;">&gt; qplot(mpg, wt, data = mtcars) + coord_equal()</pre>
</td>
</tr>
</table>
<div> <img src="http://learnr.files.wordpress.com/2010/01/ggplot2_0-8-5-013.png?w=600" style="border-width:0;" alt="ggplot2_0.8.5-013.png"> </div>
<p>This is what the default looked like in the previous version, and it was impossible to generate the plot above.</p>
<table border="0" bgcolor="#e8e8e8" width="100%" style="margin:.2em 0;">
<tr>
<td style="padding:.5em;">
<pre style="margin:0;padding:0;">&gt; qplot(mpg, wt, data = mtcars) + coord_equal(ratio = 1)</pre>
</td>
</tr>
</table>
<div> <img src="http://learnr.files.wordpress.com/2010/01/ggplot2_0-8-5-015.png?w=600" style="border-width:0;" alt="ggplot2_0.8.5-015.png"> </div>
<p><strong>5)  x and y positions can be set to Inf or -Inf to refer to the top/right and bottom/left extents of the panel.</strong></p>
<p>This is useful for annotations, and for <tt>geom_rect</tt>, <tt>geom_vline</tt> and <tt>geom_hline</tt>.</p>
<table border="0" bgcolor="#e8e8e8" width="100%" style="margin:.2em 0;">
<tr>
<td style="padding:.5em;">
<pre style="margin:0;padding:0;">&gt; ggplot(data.frame(x = 0:1, y = 0:1)) + geom_rect(aes(x = x,
+     y = y), xmin = 0.1, xmax = 0.2, ymin = -Inf,
+     ymax = Inf)</pre>
</td>
</tr>
</table>
<div> <img src="http://learnr.files.wordpress.com/2010/01/ggplot2_0-8-5-017.png?w=600" style="border-width:0;" alt="ggplot2_0.8.5-017.png"> </div>
<p><strong>6)expand_limits(): a new function to make it easy to force the inclusion of any set of values in the limits of any aesthetic.</strong></p>
<table border="0" bgcolor="#e8e8e8" width="100%" style="margin:.2em 0;">
<tr>
<td style="padding:.5em;">
<pre style="margin:0;padding:0;">&gt; p &lt;- qplot(mpg, wt, data = mtcars)</pre>
</td>
</tr>
</table>
<div> <img src="http://learnr.files.wordpress.com/2010/01/ggplot2_0-8-5-019.png?w=600" style="border-width:0;" alt="ggplot2_0.8.5-019.png"> </div>
<table border="0" bgcolor="#e8e8e8" width="100%" style="margin:.2em 0;">
<tr>
<td style="padding:.5em;">
<pre style="margin:0;padding:0;">&gt; p + expand_limits(x = 0)</pre>
</td>
</tr>
</table>
<div> <img src="http://learnr.files.wordpress.com/2010/01/ggplot2_0-8-5-021.png?w=600" style="border-width:0;" alt="ggplot2_0.8.5-021.png"> </div>
<table border="0" bgcolor="#e8e8e8" width="100%" style="margin:.2em 0;">
<tr>
<td style="padding:.5em;">
<pre style="margin:0;padding:0;">&gt; p + expand_limits(y = c(1, 9))</pre>
</td>
</tr>
</table>
<p>is the equivalent of</p>
<table border="0" bgcolor="#e8e8e8" width="100%" style="margin:.2em 0;">
<tr>
<td style="padding:.5em;">
<pre style="margin:0;padding:0;">&gt; p + ylim(c(1, 9))</pre>
</td>
</tr>
</table>
<div> <img src="http://learnr.files.wordpress.com/2010/01/ggplot2_0-8-5-024.png?w=600" style="border-width:0;" alt="ggplot2_0.8.5-024.png"> </div>
<table border="0" bgcolor="#e8e8e8" width="100%" style="margin:.2em 0;">
<tr>
<td style="padding:.5em;">
<pre style="margin:0;padding:0;">&gt; p + expand_limits(x = 0, y = 0)</pre>
</td>
</tr>
</table>
<div> <img src="http://learnr.files.wordpress.com/2010/01/ggplot2_0-8-5-026.png?w=600" style="border-width:0;" alt="ggplot2_0.8.5-026.png"> </div>
<p>It would be good if similar functionality could be extended to <tt>xlim</tt>, so that the following would work. This way the setting of limits would be encapsulated in one function (<tt>xlim</tt>/<tt>ylim</tt>).</p>
<table border="0" bgcolor="#e8e8e8" width="100%" style="margin:.2em 0;">
<tr>
<td style="padding:.5em;">
<pre style="margin:0;padding:0;">&gt; p + xlim(0, Inf)</pre>
</td>
</tr>
</table>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/learnr.wordpress.com/2339/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/learnr.wordpress.com/2339/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/learnr.wordpress.com/2339/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/learnr.wordpress.com/2339/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/learnr.wordpress.com/2339/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/learnr.wordpress.com/2339/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/learnr.wordpress.com/2339/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/learnr.wordpress.com/2339/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/learnr.wordpress.com/2339/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/learnr.wordpress.com/2339/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/learnr.wordpress.com/2339/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/learnr.wordpress.com/2339/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/learnr.wordpress.com/2339/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/learnr.wordpress.com/2339/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=learnr.wordpress.com&amp;blog=6877108&amp;post=2339&amp;subd=learnr&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://learnr.wordpress.com/2010/01/07/new-features-in-ggplot2-version-0-8-5/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/971b564fa9d5442aa0bf880c3d768cc2?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">learnr</media:title>
		</media:content>

		<media:content url="http://learnr.files.wordpress.com/2010/01/ggplot2_0-8-5-004.png" medium="image">
			<media:title type="html">ggplot2_0.8.5-004.png</media:title>
		</media:content>

		<media:content url="http://learnr.files.wordpress.com/2010/01/ggplot2_0-8-5-006.png" medium="image">
			<media:title type="html">ggplot2_0.8.5-006.png</media:title>
		</media:content>

		<media:content url="http://learnr.files.wordpress.com/2010/01/ggplot2_0-8-5-008.png" medium="image">
			<media:title type="html">ggplot2_0.8.5-008.png</media:title>
		</media:content>

		<media:content url="http://learnr.files.wordpress.com/2010/01/ggplot2_0-8-5-010.png" medium="image">
			<media:title type="html">ggplot2_0.8.5-010.png</media:title>
		</media:content>

		<media:content url="http://learnr.files.wordpress.com/2010/01/ggplot2_0-8-5-013.png" medium="image">
			<media:title type="html">ggplot2_0.8.5-013.png</media:title>
		</media:content>

		<media:content url="http://learnr.files.wordpress.com/2010/01/ggplot2_0-8-5-015.png" medium="image">
			<media:title type="html">ggplot2_0.8.5-015.png</media:title>
		</media:content>

		<media:content url="http://learnr.files.wordpress.com/2010/01/ggplot2_0-8-5-017.png" medium="image">
			<media:title type="html">ggplot2_0.8.5-017.png</media:title>
		</media:content>

		<media:content url="http://learnr.files.wordpress.com/2010/01/ggplot2_0-8-5-019.png" medium="image">
			<media:title type="html">ggplot2_0.8.5-019.png</media:title>
		</media:content>

		<media:content url="http://learnr.files.wordpress.com/2010/01/ggplot2_0-8-5-021.png" medium="image">
			<media:title type="html">ggplot2_0.8.5-021.png</media:title>
		</media:content>

		<media:content url="http://learnr.files.wordpress.com/2010/01/ggplot2_0-8-5-024.png" medium="image">
			<media:title type="html">ggplot2_0.8.5-024.png</media:title>
		</media:content>

		<media:content url="http://learnr.files.wordpress.com/2010/01/ggplot2_0-8-5-026.png" medium="image">
			<media:title type="html">ggplot2_0.8.5-026.png</media:title>
		</media:content>
	</item>
		<item>
		<title>directlabels: Adding direct labels to ggplot2 and lattice plots</title>
		<link>http://learnr.wordpress.com/2010/01/03/directlabels-adding-direct-labels-to-ggplot2-and-lattice-plots/</link>
		<comments>http://learnr.wordpress.com/2010/01/03/directlabels-adding-direct-labels-to-ggplot2-and-lattice-plots/#comments</comments>
		<pubDate>Sun, 03 Jan 2010 13:01:35 +0000</pubDate>
		<dc:creator>learnr</dc:creator>
				<category><![CDATA[R]]></category>
		<category><![CDATA[directlabels]]></category>
		<category><![CDATA[ggplot2]]></category>
		<category><![CDATA[lattice]]></category>

		<guid isPermaLink="false">http://learnr.wordpress.com/?p=2313</guid>
		<description><![CDATA[Sometimes it is preferable to label data series instead of using a legend. This post demonstrates one way of using labels instead of legend in a ggplot2 plot. &#62; library(ggplot2) &#62; p &#60;- ggplot(dfm, aes(month, value, group = City, colour = City)) + geom_line(size = 1) + opts(legend.position = "none") &#62; p + geom_text(data = [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=learnr.wordpress.com&amp;blog=6877108&amp;post=2313&amp;subd=learnr&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>Sometimes it is preferable to label data series instead of using a legend. <a href="http://learnr.wordpress.com/2009/04/29/ggplot2-labelling-data-series-and-adding-a-data-table/">This post</a> demonstrates one way of using labels instead of legend in a <a href="http://had.co.nz/ggplot2/">ggplot2</a> plot.</p>
<table border="0" bgcolor="#e8e8e8" width="100%" style="margin:.2em 0;">
<tr>
<td style="padding:.5em;">
<pre style="margin:0;padding:0;">&gt; library(ggplot2)</pre>
</td>
</tr>
</table>
<table border="0" bgcolor="#e8e8e8" width="100%" style="margin:.2em 0;">
<tr>
<td style="padding:.5em;">
<pre style="margin:0;padding:0;">&gt; p &lt;- ggplot(dfm, aes(month, value, group = City,
     colour = City)) + geom_line(size = 1) +
     opts(legend.position = "none")</pre>
</td>
</tr>
</table>
<table border="0" bgcolor="#e8e8e8" width="100%" style="margin:.2em 0;">
<tr>
<td style="padding:.5em;">
<pre style="margin:0;padding:0;">&gt; p + geom_text(data = dfm[dfm$month == "Dec",
     ], aes(label = City), hjust = 0.7, vjust = 1)</pre>
</td>
</tr>
</table>
<div> <img src="http://learnr.files.wordpress.com/2010/01/directlabel-006.png?w=600" style="border-width:0;" alt="directlabel-006.png"> </div>
<p>The addition of labels requires manual calculation of the label positions which are then passed on to <tt>geom_text()</tt>. If one wanted to move the labels around, the code would need manual adjustment &#8211; label positions need to be recalculated..</p>
<p>This problem is easily solved with the help of <a href="http://directlabels.r-forge.r-project.org/"><tt>directlabels</tt> package</a> by Toby Dylan Hocking that &#8220;is an attempt to make direct labeling a reality in everyday statistical practice by making available a body of useful functions that make direct labeling of common plots easy to do with high-level plotting systems such as lattice and ggplot2&#8243;.</p>
<table border="0" bgcolor="#e8e8e8" width="100%" style="margin:.2em 0;">
<tr>
<td style="padding:.5em;">
<pre style="margin:0;padding:0;">&gt; install.packages("directlabels", repos = "http://r-forge.r-project.org")</pre>
</td>
</tr>
</table>
<table border="0" bgcolor="#e8e8e8" width="100%" style="margin:.2em 0;">
<tr>
<td style="padding:.5em;">
<pre style="margin:0;padding:0;">&gt; library(directlabels)</pre>
</td>
</tr>
</table>
<p>The above plot can be reproduced with one line of code.</p>
<table border="0" bgcolor="#e8e8e8" width="100%" style="margin:.2em 0;">
<tr>
<td style="padding:.5em;">
<pre style="margin:0;padding:0;">&gt; direct.label(p, list(last.points, hjust = 0.7,
     vjust = 1))</pre>
</td>
</tr>
</table>
<p>In addition to <a href="http://directlabels.r-forge.r-project.org/docs/index.html">several predefined positioning functions</a>, one can also write their own positioning function. For example, placing the rotated labels at the starting values of each series.</p>
<table border="0" bgcolor="#e8e8e8" width="100%" style="margin:.2em 0;">
<tr>
<td style="padding:.5em;">
<pre style="margin:0;padding:0;">&gt; angled.firstpoints &lt;- list("first.points",
     rot = 45, hjust = 0, vjust = -0.7)
&gt; direct.label(p, angled.firstpoints)</pre>
</td>
</tr>
</table>
<div> <img src="http://learnr.files.wordpress.com/2010/01/directlabel-011.png?w=600" style="border-width:0;" alt="directlabel-011.png"> </div>
<p>I agree with the author&#8217;s <a href="http://directlabels.r-forge.r-project.org/motivation.html">conclusion</a> that the <tt>directlabels</tt> package simplifies and makes more convenient the labeling of data series in both <tt>lattice</tt> and <tt>ggplot2</tt>.</p>
<p>Thanks to Baptiste for <a href="http://learnr.wordpress.com/2009/04/29/ggplot2-labelling-data-series-and-adding-a-data-table/#comment-373">bringing</a> this package to my attention.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/learnr.wordpress.com/2313/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/learnr.wordpress.com/2313/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/learnr.wordpress.com/2313/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/learnr.wordpress.com/2313/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/learnr.wordpress.com/2313/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/learnr.wordpress.com/2313/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/learnr.wordpress.com/2313/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/learnr.wordpress.com/2313/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/learnr.wordpress.com/2313/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/learnr.wordpress.com/2313/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/learnr.wordpress.com/2313/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/learnr.wordpress.com/2313/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/learnr.wordpress.com/2313/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/learnr.wordpress.com/2313/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=learnr.wordpress.com&amp;blog=6877108&amp;post=2313&amp;subd=learnr&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://learnr.wordpress.com/2010/01/03/directlabels-adding-direct-labels-to-ggplot2-and-lattice-plots/feed/</wfw:commentRss>
		<slash:comments>7</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/971b564fa9d5442aa0bf880c3d768cc2?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">learnr</media:title>
		</media:content>

		<media:content url="http://learnr.files.wordpress.com/2010/01/directlabel-006.png" medium="image">
			<media:title type="html">directlabel-006.png</media:title>
		</media:content>

		<media:content url="http://learnr.files.wordpress.com/2010/01/directlabel-011.png" medium="image">
			<media:title type="html">directlabel-011.png</media:title>
		</media:content>
	</item>
		<item>
		<title>Data Profiling in R</title>
		<link>http://learnr.wordpress.com/2009/12/17/data-profiling-in-r/</link>
		<comments>http://learnr.wordpress.com/2009/12/17/data-profiling-in-r/#comments</comments>
		<pubDate>Thu, 17 Dec 2009 09:09:56 +0000</pubDate>
		<dc:creator>learnr</dc:creator>
				<category><![CDATA[R]]></category>
		<category><![CDATA[Hmisc]]></category>
		<category><![CDATA[LaTeX]]></category>
		<category><![CDATA[r2lUniv]]></category>

		<guid isPermaLink="false">http://learnr.wordpress.com/?p=2295</guid>
		<description><![CDATA[In 2006 UserR conference Jim Porzak gave a presentation on data profiling with R. He showed how to draw summary panels of the data using a combination of grid and base graphics. Unfortunately the code has not (yet) been released as a package, so when I recently needed to quickly review several datasets at the [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=learnr.wordpress.com&amp;blog=6877108&amp;post=2295&amp;subd=learnr&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>In 2006 UserR conference Jim Porzak gave a presentation on <a href="http://www.r-project.org/conferences/useR-2006/Slides/Porzak.pdf">data profiling with R</a>. He showed how to draw summary panels of the data using a combination of grid and base graphics.</p>
<p><img style="border-width:0;" src="http://learnr.files.wordpress.com/2009/12/data_profiling_porzak.png?w=600" alt="data_profiling_porzak.png" /></p>
<p>Unfortunately the code has not (yet) been released as a package, so when I recently needed to quickly review several datasets at the beginning of an analysis project I started to look for alternatives. A quick search revealed two options that offer similar functionality: <tt>r2lUniv</tt> package and <tt>describe()</tt> function in <tt>Hmisc</tt> package.</p>
<p><span id="more-2295"></span></p>
<hr />
<h2><a name="_r2luniv"></a>r2lUniv</h2>
<p><a href="http://cran.r-project.org/package=r2lUniv"><tt>r2lUniv</tt> package</a> performs quick analysis either on a single variable or on a dataframe by computing several statistics (frequency, centrality, dispersion, graph) for each variable and outputs the results in a LaTeX format. The output varies depending on the variable type.</p>
<table style="margin:.2em 0;" border="0" width="100%" bgcolor="#e8e8e8">
<tbody>
<tr>
<td style="padding:.5em;">
<pre style="margin:0;padding:0;">&gt; library(r2lUniv)</pre>
</td>
</tr>
</tbody>
</table>
<p>One can specify the text to be inserted in front of each section.</p>
<table style="margin:.2em 0;" border="0" width="100%" bgcolor="#e8e8e8">
<tbody>
<tr>
<td style="padding:.5em;">
<pre style="margin:0;padding:0;">&gt; textBefore &lt;- paste("\\subsection{", names(mtcars),
+     "}", sep = "")
&gt; rtlu(mtcars, "fileOut.tex", textBefore = textBefore)</pre>
</td>
</tr>
</tbody>
</table>
<p>The function <tt>rtluMainFile</tt> generates a LaTeX main document design and allows to further customise the report.</p>
<table style="margin:.2em 0;" border="0" width="100%" bgcolor="#e8e8e8">
<tbody>
<tr>
<td style="padding:.5em;">
<pre style="margin:0;padding:0;">&gt; text &lt;- "\\input{fileOut.tex}"
&gt; rtluMainFile("r2lUniv_report.tex", text = text)</pre>
</td>
</tr>
</tbody>
</table>
<p>The resulting tex-file can then be converted into pdf.</p>
<table style="margin:.2em 0;" border="0" width="100%" bgcolor="#e8e8e8">
<tbody>
<tr>
<td style="padding:.5em;">
<pre style="margin:0;padding:0;">&gt; library(tools)
&gt; texi2dvi("r2lUniv_report.tex", pdf = TRUE, clean = TRUE)</pre>
</td>
</tr>
</tbody>
</table>
<p>A sample output for the mpg-variable:</p>
<p><img style="border-width:0;" src="http://learnr.files.wordpress.com/2009/12/data_profiling_r2luniv.png?w=600" alt="data_profiling_r2lUniv.png" /></p>
<p>The final pdf-output can be seen here: <a href="http://learnr.files.wordpress.com/2009/12/r2luniv_report.pdf">r2lUniv_report.pdf</a>.</p>
<hr />
<h2><a name="_hmisc"></a>Hmisc</h2>
<p>The <tt>describe</tt> function in <a href="http://cran.r-project.org/web/packages/Hmisc/index.html">Hmisc package</a> determines whether the variable is character, factor, category, binary, discrete numeric, and continuous numeric, and prints a concise statistical summary according to each. The latex report also includes a spike histogram displaying the frequency counts.</p>
<table style="margin:.2em 0;" border="0" width="100%" bgcolor="#e8e8e8">
<tbody>
<tr>
<td style="padding:.5em;">
<pre style="margin:0;padding:0;">&gt; library(Hmisc)</pre>
</td>
</tr>
</tbody>
</table>
<table style="margin:.2em 0;" border="0" width="100%" bgcolor="#e8e8e8">
<tbody>
<tr>
<td style="padding:.5em;">
<pre style="margin:0;padding:0;">&gt; db &lt;- describe(mtcars, size = "normalsize")</pre>
</td>
</tr>
</tbody>
</table>
<p>The easiest and fastest way is to print the results to the console.</p>
<table style="margin:.2em 0;" border="0" width="100%" bgcolor="#e8e8e8">
<tbody>
<tr>
<td style="padding:.5em;">
<pre style="margin:0;padding:0;">&gt; db$mpg
mpg
      n missing  unique    Mean     .05     .10     .25     .50
     32       0      25   20.09   12.00   14.34   15.43   19.20
    .75     .90     .95
  22.80   30.09   31.30

lowest : 10.4 13.3 14.3 14.7 15.0
highest: 26.0 27.3 30.4 32.4 33.9</pre>
</td>
</tr>
</tbody>
</table>
<p>Alternatively, one can convert the <tt>describe</tt> object into a LaTeX file.</p>
<table style="margin:.2em 0;" border="0" width="100%" bgcolor="#e8e8e8">
<tbody>
<tr>
<td style="padding:.5em;">
<pre style="margin:0;padding:0;">&gt; x &lt;- latex(db, file = "describe.tex")</pre>
</td>
</tr>
</tbody>
</table>
<p><tt>cat</tt> is used to generate the tex-report.</p>
<table style="margin:.2em 0;" border="0" width="100%" bgcolor="#e8e8e8">
<tbody>
<tr>
<td style="padding:.5em;">
<pre style="margin:0;padding:0;">&gt; text2 &lt;- "\\documentclass{article}\n\\usepackage{relsize,setspace}\n\\begin{document}\n\\input{describe.tex} \n\\end{document}"
&gt; cat(text2, file = "Hmisc_describe_report.tex")</pre>
</td>
</tr>
</tbody>
</table>
<table style="margin:.2em 0;" border="0" width="100%" bgcolor="#e8e8e8">
<tbody>
<tr>
<td style="padding:.5em;">
<pre style="margin:0;padding:0;">&gt; library(tools)
&gt; texi2dvi("Hmisc_describe_report.tex", pdf = TRUE)</pre>
</td>
</tr>
</tbody>
</table>
<p>A sample output for the mpg-variable:</p>
<p><img style="border-width:0;" src="http://learnr.files.wordpress.com/2009/12/data_profiling_describe.png?w=600" alt="data_profiling_describe.png" /></p>
<p>The final pdf-report can be seen here: <a href="http://learnr.files.wordpress.com/2009/12/hmisc_describe_report.pdf">Hmisc_describe_report.pdf</a>.</p>
<hr />
<h2><a name="_conclusion"></a>Conclusion</h2>
<p>Both of the functions provide similar snapshots of the data, however I prefer the <tt>describe</tt> function for its more concise output, and also for the option to print the analysis to the console. Whilst I like the summary plots generated by <tt>r2lUniv</tt> I find them hard to read in the pdf-report because of the small font-size of the labels.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/learnr.wordpress.com/2295/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/learnr.wordpress.com/2295/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/learnr.wordpress.com/2295/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/learnr.wordpress.com/2295/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/learnr.wordpress.com/2295/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/learnr.wordpress.com/2295/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/learnr.wordpress.com/2295/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/learnr.wordpress.com/2295/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/learnr.wordpress.com/2295/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/learnr.wordpress.com/2295/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/learnr.wordpress.com/2295/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/learnr.wordpress.com/2295/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/learnr.wordpress.com/2295/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/learnr.wordpress.com/2295/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=learnr.wordpress.com&amp;blog=6877108&amp;post=2295&amp;subd=learnr&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://learnr.wordpress.com/2009/12/17/data-profiling-in-r/feed/</wfw:commentRss>
		<slash:comments>7</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/971b564fa9d5442aa0bf880c3d768cc2?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">learnr</media:title>
		</media:content>

		<media:content url="http://learnr.files.wordpress.com/2009/12/data_profiling_porzak.png" medium="image">
			<media:title type="html">data_profiling_porzak.png</media:title>
		</media:content>

		<media:content url="http://learnr.files.wordpress.com/2009/12/data_profiling_r2luniv.png" medium="image">
			<media:title type="html">data_profiling_r2lUniv.png</media:title>
		</media:content>

		<media:content url="http://learnr.files.wordpress.com/2009/12/data_profiling_describe.png" medium="image">
			<media:title type="html">data_profiling_describe.png</media:title>
		</media:content>
	</item>
		<item>
		<title>ggplot2: Overplotting In a Faceted Scatterplot</title>
		<link>http://learnr.wordpress.com/2009/12/03/ggplot2-overplotting-in-a-faceted-scatterplot/</link>
		<comments>http://learnr.wordpress.com/2009/12/03/ggplot2-overplotting-in-a-faceted-scatterplot/#comments</comments>
		<pubDate>Thu, 03 Dec 2009 12:06:36 +0000</pubDate>
		<dc:creator>learnr</dc:creator>
				<category><![CDATA[R]]></category>
		<category><![CDATA[facet]]></category>
		<category><![CDATA[ggplot2]]></category>
		<category><![CDATA[plot]]></category>
		<category><![CDATA[scatterplot]]></category>

		<guid isPermaLink="false">http://learnr.wordpress.com/?p=2209</guid>
		<description><![CDATA[Hadley Wickham recently shared a nice tip on how to get a faceted scatterplot plot with all points in the background of each plot. This technique makes a clever use of setting the faceting variable to NULL so that all points are plotted in light grey in all the facets. &#62; library(ggplot2) &#62; ggplot(mtcars, aes(cyl, [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=learnr.wordpress.com&amp;blog=6877108&amp;post=2209&amp;subd=learnr&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>Hadley Wickham recently <a href="http://groups.google.com/group/ggplot2/msg/4c0763732ee5f4e1">shared</a> a nice tip on how to get a faceted scatterplot plot with all points in the background of each plot.</p>
<p>This technique makes a clever use of setting the faceting variable to NULL so that all points are plotted in light grey in all the facets.</p>
<table border="0" bgcolor="#e8e8e8" width="100%" style="margin:.2em 0;">
<tr>
<td style="padding:.5em;">
<pre style="margin:0;padding:0;">&gt; library(ggplot2)</pre>
</td>
</tr>
</table>
<table border="0" bgcolor="#e8e8e8" width="100%" style="margin:.2em 0;">
<tr>
<td style="padding:.5em;">
<pre style="margin:0;padding:0;">&gt; ggplot(mtcars, aes(cyl, mpg)) + geom_point(data = transform(mtcars,
     gear = NULL), colour = "grey80") + geom_point() +
     facet_grid(~gear) + theme_bw()</pre>
</td>
</tr>
</table>
<div> <img src="http://learnr.files.wordpress.com/2010/05/overplotting-004.png?w=600" style="border-width:0;" alt="overplotting-004.png"> </div>
<p><strong>Update 17 May 2010</strong></p>
<p>bch asked in the comments below, how to achieve the same when there are two facets. The method is the same, now one would need to exclude both of the facetting variables from the dataset used to draw the light grey points.</p>
<table border="0" bgcolor="#e8e8e8" width="100%" style="margin:.2em 0;">
<tr>
<td style="padding:.5em;">
<pre style="margin:0;padding:0;">&gt; ggplot(mtcars, aes(cyl, mpg)) + geom_point(data = mtcars[,
     !names(mtcars) %in% c("am", "gear")],
     colour = "grey80") + geom_point() + facet_grid(am ~
     gear) + theme_bw()</pre>
</td>
</tr>
</table>
<div> <img src="http://learnr.files.wordpress.com/2010/05/overplotting-006.png?w=600" style="border-width:0;" alt="overplotting-006.png"> </div>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/learnr.wordpress.com/2209/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/learnr.wordpress.com/2209/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/learnr.wordpress.com/2209/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/learnr.wordpress.com/2209/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/learnr.wordpress.com/2209/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/learnr.wordpress.com/2209/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/learnr.wordpress.com/2209/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/learnr.wordpress.com/2209/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/learnr.wordpress.com/2209/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/learnr.wordpress.com/2209/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/learnr.wordpress.com/2209/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/learnr.wordpress.com/2209/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/learnr.wordpress.com/2209/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/learnr.wordpress.com/2209/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=learnr.wordpress.com&amp;blog=6877108&amp;post=2209&amp;subd=learnr&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://learnr.wordpress.com/2009/12/03/ggplot2-overplotting-in-a-faceted-scatterplot/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/971b564fa9d5442aa0bf880c3d768cc2?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">learnr</media:title>
		</media:content>

		<media:content url="http://learnr.files.wordpress.com/2010/05/overplotting-004.png" medium="image">
			<media:title type="html">overplotting-004.png</media:title>
		</media:content>

		<media:content url="http://learnr.files.wordpress.com/2010/05/overplotting-006.png" medium="image">
			<media:title type="html">overplotting-006.png</media:title>
		</media:content>
	</item>
	</channel>
</rss>
