<?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>scienceoss.com &#187; ggplot2</title>
	<atom:link href="http://scienceoss.com/categories/r/ggplot2/feed/" rel="self" type="application/rss+xml" />
	<link>http://scienceoss.com</link>
	<description>useful tidbits for using open source software in science</description>
	<lastBuildDate>Wed, 26 May 2010 03:34:19 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.2</generator>
		<item>
		<title>Restructure or reformat dataframes in R with melt</title>
		<link>http://scienceoss.com/restructure-or-reformat-dataframes-in-r-with-melt/</link>
		<comments>http://scienceoss.com/restructure-or-reformat-dataframes-in-r-with-melt/#comments</comments>
		<pubDate>Sun, 23 Mar 2008 22:06:17 +0000</pubDate>
		<dc:creator>ryan</dc:creator>
				<category><![CDATA[ggplot2]]></category>
		<category><![CDATA[R]]></category>
		<category><![CDATA[data management]]></category>
		<category><![CDATA[dataframe]]></category>
		<category><![CDATA[melt]]></category>

		<guid isPermaLink="false">http://scienceoss.com/restructure-or-reformat-dataframes-in-r-with-melt/</guid>
		<description><![CDATA[The basic idea is to take an R dataframe like this one containing abundance of three species at each site, and elevation at each site site sp1 sp2 sp3 elev a 3 4 9 100 b 1 8 10 210 c 4 8 15 165 and reorganize into something like this (perhaps so we can [...]]]></description>
			<content:encoded><![CDATA[<p>The basic idea is to take an R dataframe like this one containing abundance of three species at each site, and elevation at each site</p>
<pre class="prettyprint"><code class="code">site  sp1 sp2 sp3 elev
a      3   4   9   100
b      1   8   10  210
c      4   8   15  165
</code></pre>
<p>and reorganize into something like this (perhaps so we can do an ANOVA using species as a factor):</p>
<pre class="prettyprint"><code class="code">site  elev  sp  abundance
a     100  sp1  3
a     100  sp2  4
a     100  sp3  9
b     210  sp1  1
b     210  sp2  8
b     210  sp3  10
c     165  sp1  4
c     165  sp2  8
c     165  sp3  15</code></pre>
<p>Assuming the first dataframe above is called <span class="c">d</span>, the second dataframe can be obtained using the following code:</p>
<pre class="prettyprint"><code class="code">> library(ggplot2)
> m = melt(d, id=c('site','elev'))
</code></pre>
<p><span class="c">melt</span> works like this: You specify the ID variables, which are those variables that will REMAIN as dataframe variables.  Any others will be considered measured variables.  If it&#8217;s easier for your data, you can do it the other way: specify the measured variables and the others will be considered ID variables.</p>
<p>Melting results in two new variables, <span class="c">variable</span> and <span class="c">value</span>.  <span class="c">variable</span> contains the names of the original columns of the dataframe as factors, and <span class="c">value</span> contains the corresponding values.</p>
<h3>Another example</h3>
<p>Here&#8217;s another example using the built-in dataset, airquality.  First, unmelted:</p>
<pre class="prettyprint"><code class="code"># make all the variable names lowercase
names(airquality) <- tolower(names(airquality))
head(airquality)
  ozone solar.r wind temp month day
1    41     190  7.4   67     5   1
2    36     118  8.0   72     5   2
3    12     149 12.6   74     5   3
4    18     313 11.5   62     5   4
5    NA      NA 14.3   56     5   5
6    28      NA 14.9   66     5   6
</code></pre>
<p>and melted:</p>
<pre class="prettyprint"><code class="code">> head(melt(airquality,id=c('month','day')))
  month day variable value
1     5   1    ozone    41
2     5   2    ozone    36
3     5   3    ozone    12
4     5   4    ozone    18
5     5   5    ozone    NA
6     5   6    ozone    28</code></pre>
]]></content:encoded>
			<wfw:commentRss>http://scienceoss.com/restructure-or-reformat-dataframes-in-r-with-melt/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Reorder factors for ggplot</title>
		<link>http://scienceoss.com/reorder-factors-for-ggplot/</link>
		<comments>http://scienceoss.com/reorder-factors-for-ggplot/#comments</comments>
		<pubDate>Sun, 23 Mar 2008 21:35:45 +0000</pubDate>
		<dc:creator>ryan</dc:creator>
				<category><![CDATA[ggplot2]]></category>
		<category><![CDATA[R]]></category>
		<category><![CDATA[facet grid]]></category>
		<category><![CDATA[factors]]></category>
		<category><![CDATA[ggplot]]></category>
		<category><![CDATA[reorder]]></category>

		<guid isPermaLink="false">http://scienceoss.com/reorder-factors-for-ggplot/</guid>
		<description><![CDATA[A somewhat contrived example . . . first, illustrate the problem: library(ggplot2) ggplot(iris)+aes(x=Sepal.Width)+geom_histogram()+facet_grid(Species~.) How do we get these histograms to be better sorted? The following will reorder the factor variable, Species, by the mean of Sepal.Width: iris$Species = reorder(iris$Species, iris$Sepal.Width, mean) ggplot(iris)+aes(x=Sepal.Width)+geom_histogram()+facet_grid(Species~.) Now the histograms are sorted by the mean sepal width.]]></description>
			<content:encoded><![CDATA[<p>A somewhat contrived example . . . first, illustrate the problem:</p>
<pre class="prettyprint"><code class="code">library(ggplot2)
ggplot(iris)+aes(x=Sepal.Width)+geom_histogram()+facet_grid(Species~.)</code></pre>
<p><a href='http://scienceoss.com/wp-content/uploads/2008/03/hist-unsorted.png' title='Unsorted histogram'><img src='http://scienceoss.com/wp-content/uploads/2008/03/hist-unsorted.png' alt='Unsorted histogram' /></a><br />
How do we get these histograms to be better sorted?  The following will reorder the factor variable, <span class="c">Species</span>, by the mean of <span class="c">Sepal.Width</span>:</p>
<pre class="prettyprint"><code class="code">iris$Species = reorder(iris$Species, iris$Sepal.Width, mean)
ggplot(iris)+aes(x=Sepal.Width)+geom_histogram()+facet_grid(Species~.)</code></pre>
<p><a href='http://scienceoss.com/wp-content/uploads/2008/03/hist-sorted.png' title='Sorted histogram'><img src='http://scienceoss.com/wp-content/uploads/2008/03/hist-sorted.png' alt='Sorted histogram' /></a><br />
Now the histograms are sorted by the mean sepal width.</p>
]]></content:encoded>
			<wfw:commentRss>http://scienceoss.com/reorder-factors-for-ggplot/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Change tick locations and labels in ggplot</title>
		<link>http://scienceoss.com/change-tick-locations-and-labels-in-ggplot/</link>
		<comments>http://scienceoss.com/change-tick-locations-and-labels-in-ggplot/#comments</comments>
		<pubDate>Mon, 14 Jan 2008 01:00:00 +0000</pubDate>
		<dc:creator>ryan</dc:creator>
				<category><![CDATA[ggplot2]]></category>
		<category><![CDATA[R]]></category>
		<category><![CDATA[axis]]></category>
		<category><![CDATA[tick labels]]></category>

		<guid isPermaLink="false">http://scienceoss.com/?p=61</guid>
		<description><![CDATA[Use the scale_x_continuous (or scale_y_continuous) to change tick locations and labels in ggplot. Change the ticks: p = ggplot(diamonds)+aes(x=carat,y=price) + scale_x_continuous(breaks=c(1,1.2,1.5,2.5)) Change the ticks and tick labels: p = ggplot(diamonds)+aes(x=carat,y=price) + scale_x_continuous(breaks=c(1,1.2,1.5,2.5), labels=c('a','b','c','d'))]]></description>
			<content:encoded><![CDATA[<p>Use the scale_x_continuous (or scale_y_continuous) to change tick locations and labels in <a href="http://had.co.nz/ggplot2/">ggplot</a>.</p>
<p>Change the ticks:</p>
<pre class = "prettyprint"><code class = "code">
p = ggplot(diamonds)+aes(x=carat,y=price) +
   scale_x_continuous(breaks=c(1,1.2,1.5,2.5))
</code></pre>
<p>Change the ticks and tick labels:</p>
<pre class = "prettyprint"><code class = "code">p = ggplot(diamonds)+aes(x=carat,y=price) +
   scale_x_continuous(breaks=c(1,1.2,1.5,2.5), labels=c('a','b','c','d'))</code></pre>
]]></content:encoded>
			<wfw:commentRss>http://scienceoss.com/change-tick-locations-and-labels-in-ggplot/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Change axis labels in ggplot</title>
		<link>http://scienceoss.com/change-axis-labels-in-ggplot/</link>
		<comments>http://scienceoss.com/change-axis-labels-in-ggplot/#comments</comments>
		<pubDate>Mon, 14 Jan 2008 00:17:39 +0000</pubDate>
		<dc:creator>ryan</dc:creator>
				<category><![CDATA[ggplot2]]></category>
		<category><![CDATA[R]]></category>

		<guid isPermaLink="false">http://scienceoss.com/?p=60</guid>
		<description><![CDATA[By default, ggplot uses the variable name as the axis labels. Change it to something else using scale_x_continuous or scale_y_continuous p = ggplot(diamonds)+geom_point()+aes(x=carat,y=price) p + scale_x_continuous('x axis label') + scale_y_continuous('y axis label')]]></description>
			<content:encoded><![CDATA[<p>By default, ggplot uses the variable name as the axis labels.  Change it to something else using <span class="c">scale_x_continuous</span> or <code>scale_y_continuous</code></p>
<pre class = "prettyprint"><code class = "code">
p = ggplot(diamonds)+geom_point()+aes(x=carat,y=price)

p + scale_x_continuous('x axis label') + scale_y_continuous('y axis label')
</code></pre>
]]></content:encoded>
			<wfw:commentRss>http://scienceoss.com/change-axis-labels-in-ggplot/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>

