<p><div><div><div><div data-show-faces="false" data-height="21" data-width="100" data-layout="button_count" data-send="true" data-href="https://www.r-bloggers.com/faceted-graphs-with-cdata-and-ggplot2/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="http://www.win-vector.com/blog/2018/10/faceted-graphs-with-cdata-and-ggplot2/"> R – Win-Vector Blog</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<p>In between client work, John and I have been busy working on our book, <a rel="nofollow" href="http://practicaldatascience.com/" target="_blank"><em>Practical Data Science with R, 2nd Edition</em></a>. To demonstrate a toy example for the section I’m working on, I needed scatter plots of the petal and sepal dimensions of the <code>iris</code> data, like so:</p>
<div>
<img border="0" src="https://i2.wp.com/www.win-vector.com/blog/wp-content/uploads/2018/10/unnamed-chunk-1-1.png?resize=660%2C471" alt="Unnamed chunk 1 1" width="450" data-recalc-dims="1">
</div>
<p>I wanted a plot for petal dimensions and sepal dimensions, but I also felt that two plots took up too much space. So, I thought, why not make a faceted graph that shows both:</p>
<div>
<img border="0" src="https://i2.wp.com/www.win-vector.com/blog/wp-content/uploads/2018/10/unnamed-chunk-2-1.png?resize=660%2C471" alt="Unnamed chunk 2 1" width="450" data-recalc-dims="1">
</div>
<p>Except — which columns do I plot and what do I facet on?</p>
<div>
<pre><code><span>head</span>(iris)</code></pre>
</div>
<pre><code>##   Sepal.Length Sepal.Width Petal.Length Petal.Width Species
## 1          5.1         3.5          1.4         0.2  setosa
## 2          4.9         3.0          1.4         0.2  setosa
## 3          4.7         3.2          1.3         0.2  setosa
## 4          4.6         3.1          1.5         0.2  setosa
## 5          5.0         3.6          1.4         0.2  setosa
## 6          5.4         3.9          1.7         0.4  setosa</code></pre>
<p>Here’s one way to create the plot I want, using the <a rel="nofollow" href="https://cran.r-project.org/package=cdata" target="_blank"><code>cdata</code></a> package along with <a rel="nofollow" href="https://cran.r-project.org/package=ggplot2" target="_blank"><code>ggplot2</code></a>.</p>
<p><span></span></p>
<p>First, load the packages and data:</p>
<div>
<pre><code><span>library</span>(<span>&quot;ggplot2&quot;</span>)
<span>library</span>(<span>&quot;cdata&quot;</span>)

iris &lt;-<span> </span><span>data.frame</span>(iris)</code></pre>
</div>
<p>Now define the data-shaping transform, or <em>control table</em>. The control table is basically a picture that sketches out the final data shape that I want. I want to specify the <code>x</code> and <code>y</code> columns of the plot (call these the <em>value columns</em> of the data frame) and the column that I am faceting by (call this the <em>key column</em> of the data frame). And I also need to specify how the key and value columns relate to the existing columns of the original data frame.</p>
<p>Here’s what the control table looks like:</p>
<p><a rel="nofollow" href="https://i1.wp.com/www.win-vector.com/blog/wp-content/uploads/2018/10/cdata_iris.png" target="_blank"><img src="https://i1.wp.com/www.win-vector.com/blog/wp-content/uploads/2018/10/cdata_iris.png?w=660" data-recalc-dims="1"></a></p>
<p>The control table specifies that the new data frame will have the columns <code>flower_part</code>, <code>Length</code> and <code>Width</code>. Every row of <code>iris</code> will produce two rows in the new data frame: one with a <code>flower_part</code> value of <code>Petal</code>, and another with a <code>flower_part</code> value of <code>Sepal</code>. The <code>Petal</code> row will take the <code>Petal.Length</code> and <code>Petal.Width</code> values in the <code>Length</code> and <code>Width</code> columns respectively. Similarly for the <code>Sepal</code> row.</p>
<p>Here I create the control table in R, using the convenience function <code>wrapr::build_frame()</code> to create the <code>controlTable</code> data frame in a legible way.</p>
<div>
<pre><code>(controlTable &lt;-<span> </span>wrapr::<span>build_frame</span>(
   <span>&quot;flower_part&quot;</span>, <span>&quot;Length&quot;</span>      , <span>&quot;Width&quot;</span>       |
<span> &quot;Petal&quot;</span>      , <span>&quot;Petal.Length&quot;</span>, <span>&quot;Petal.Width&quot;</span> |
<span> &quot;Sepal&quot;</span>      , <span>&quot;Sepal.Length&quot;</span>, <span>&quot;Sepal.Width&quot;</span> ))</code></pre>
</div>
<pre><code>##   flower_part       Length       Width
## 1       Petal Petal.Length Petal.Width
## 2       Sepal Sepal.Length Sepal.Width</code></pre>
<p>Now I apply the transform to <code>iris</code> using the function <code>rowrecs_to_blocks()</code>. I also want to carry along the <code>Species</code> column so I can color the scatterplot points by species.</p>
<div>
<pre><code>iris_aug &lt;-<span> </span><span>rowrecs_to_blocks</span>(
  iris,
  controlTable,
  <span>columnsToCopy =</span> <span>c</span>(<span>&quot;Species&quot;</span>))

<span>head</span>(iris_aug)</code></pre>
</div>
<pre><code>##   Species flower_part Length Width
## 1  setosa       Petal    1.4   0.2
## 2  setosa       Sepal    5.1   3.5
## 3  setosa       Petal    1.4   0.2
## 4  setosa       Sepal    4.9   3.0
## 5  setosa       Petal    1.3   0.2
## 6  setosa       Sepal    4.7   3.2</code></pre>
<p>And now I can create the plot!</p>
<div>
<pre><code><span>ggplot</span>(iris_aug, <span>aes</span>(<span>x=</span>Length, <span>y=</span>Width)) +
<span> </span><span>geom_point</span>(<span>aes</span>(<span>color=</span>Species, <span>shape=</span>Species)) +<span> </span>
<span> </span><span>facet_wrap</span>(~flower_part, <span>labeller =</span> label_both, <span>scale =</span> <span>&quot;free&quot;</span>) +
<span> </span><span>ggtitle</span>(<span>&quot;Iris dimensions&quot;</span>) +<span> </span>
<span> </span><span>scale_color_brewer</span>(<span>palette =</span> <span>&quot;Dark2&quot;</span>)</code></pre>
</div>
<div>
<img border="0" src="https://i0.wp.com/www.win-vector.com/blog/wp-content/uploads/2018/10/unnamed-chunk-7-1.png?resize=660%2C471" alt="Unnamed chunk 7 1" width="450" data-recalc-dims="1">
</div>
<p>In the next post, I will show how to use <code>cdata</code> and <code>ggplot2</code> to create a scatterplot matrix.</p>
	<p>
<div>
<div>To <strong>leave a comment</strong> for the author, please follow the link and comment on their blog: <strong><a href="http://www.win-vector.com/blog/2018/10/faceted-graphs-with-cdata-and-ggplot2/"> R – Win-Vector Blog</a></strong>.</div>
<hr>
<a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers.com</a> offers <strong><a rel="nofollow" href="https://feedburner.google.com/fb/a/mailverify?uri=RBloggers">daily e-mail updates</a></strong> about <a rel="nofollow" href="https://www.r-project.org/" title="The R Project for Statistical Computing">R</a> news and <a rel="nofollow" href="https://www.r-bloggers.com/search/tutorial" title="R tutorials">tutorials</a> on topics such as: <a rel="nofollow" href="https://www.r-bloggers.com/search/data%20science" title="Data science">Data science</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/Big%20Data" title="Big Data">Big Data, <a rel="nofollow" href="https://www.r-users.com/" title="R jobs">R jobs</a>, visualization (<a rel="nofollow" href="https://www.r-bloggers.com/search/ggplot2" title="ggplot and ggplot2 tutorials">ggplot2</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/boxplot" title="Boxplots using lattice and ggplot2 tutorials">Boxplots</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/map" title="Maps and gis">maps</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/animation" title="Animation in R">animation</a>), programming (<a rel="nofollow" href="https://www.r-bloggers.com/search/RStudio" title="RStudio IDE for R">RStudio</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/sweave" title="Sweave and literate programming">Sweave</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/LaTeX" title="LaTeX in R">LaTeX</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/SQL" title="SQL and databases">SQL</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/eclipse" title="Eclipse IDE for R">Eclipse</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/git" title="git and github, Version Control System">git</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/hadoop" title="Large data in R using Hadoop">hadoop</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/Web+Scraping" title="Web Scraping of google, facebook, yahoo, twitter and more using R">Web Scraping</a>) statistics (<a rel="nofollow" href="https://www.r-bloggers.com/search/regression" title="Regressions and ANOVA analysis tutorials">regression</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/PCA" title="principal component analysis tutorial">PCA</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/time+series" title="Time series">time series</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/trading" title="finance trading">trading</a>) and more...
<img width="1" alt="" src="http://feeds.feedburner.com/~r/RBloggers/~4/DwRNEpUOtUE" height="1"></a></div>