<p><div><div><div><div data-width="100" data-show-faces="false" data-send="true" data-layout="button_count" data-height="21" data-href="https://www.r-bloggers.com/it-seems-dplyr-is-overtaking-correlation-heatmaps/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="https://martinsbioblogg.wordpress.com/2017/03/09/it-seems-dplyr-is-overtaking-correlation-heatmaps/"> R – On unicorns and genes</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<p>(… on my blog, that is.)</p>
<p>For a long time, my <a target="_blank" rel="nofollow" href="https://martinsbioblogg.wordpress.com/2013/03/21/using-r-correlation-heatmap-with-ggplot2/">correlation heatmap with ggplot2</a> was the most viewed post on this blog. It still leads the overall top list, but by far the most searched and visited post nowadays is <a target="_blank" rel="nofollow" href="https://martinsbioblogg.wordpress.com/2014/03/26/using-r-quickly-calculating-summary-statistics-with-dplyr/">this one about dplyr</a> (followed by it’s <a target="_blank" rel="nofollow" href="https://martinsbioblogg.wordpress.com/2014/03/25/using-r-quickly-calculating-summary-statistics-from-a-data-frame/">sibling about plyr</a>).</p>
<p>I fully support this, since data wrangling and reorganization logically comes before plotting (especially in the <a target="_blank" rel="nofollow" href="https://martinsbioblogg.wordpress.com/2014/02/20/books-and-lessons-about-ggplot2/">ggplot2 philosophy</a>).</p>
<p>But it’s also kind of a shame, because it’s not a very good dplyr post, and the one about the correlation heatmap is not a very good ggplot2 post. Thankfully, there is a new edition of the <a target="_blank" rel="nofollow" href="http://www.springer.com/la/book/9783319242750">ggplot2 book by Hadley Wickham</a>, and <a target="_blank" rel="nofollow" href="http://r4ds.had.co.nz/">a new book by him and Garrett Grolemund</a> about data analysis with modern R packages. I’m looking forward to reading them.</p>
<p>Personally, I still haven’t made the switch from plyr and reshape2 to dplyr and tidyr. But here is the updated tidyverse-using version of how to quickly calculate summary statistics from a data frame:</p>
<pre>
library(tidyr)
library(dplyr)
library(magrittr)

data &lt;- data.frame(sex = c(rep(1, 1000), rep(2, 1000)),
                   treatment = rep(c(1, 2), 1000),
                   response1 = rnorm(2000, 0, 1),
                   response2 = rnorm(2000, 0, 1))

gather(data, response1, response2, value = &quot;value&quot;, key = &quot;variable&quot;) %>%
  group_by(sex, treatment, variable) %>%
  summarise(mean = mean(value), sd = sd(value))
</pre>
<p>Row by row we:</p>
<p>1-3: Load the packages.</p>
<p>5-8: Simulate some nonsense data.</p>
<p>10: Transform the simulated dataset to long form. This means that the two variables <code>response1</code> and <code>response2</code> get collected to one column, which will be called ”value”. The column ”key” will indicate which variable each row belongs to. (gather is tidyr’s version of melt.)</p>
<p>11: Group the resulting dataframe by sex, treatment and variable. (This is like the second argument to d*ply.)</p>
<p>12: Calculate the summary statistics.</p>
<blockquote>
<pre>
Source: local data frame [8 x 5]
Groups: sex, treatment [?]

    sex treatment  variable        mean        sd
  (dbl)     (dbl)     (chr)       (dbl)     (dbl)
1     1         1 response1 -0.02806896 1.0400225
2     1         1 response2 -0.01822188 1.0350210
3     1         2 response1  0.06307962 1.0222481
4     1         2 response2 -0.01388931 0.9407992
5     2         1 response1 -0.06748091 0.9843697
6     2         1 response2  0.01269587 1.0189592
7     2         2 response1 -0.01399262 0.9696955
8     2         2 response2  0.10413442 0.9417059
</pre>
</blockquote>
<p>Postat i:<a target="_blank" rel="nofollow" href="https://martinsbioblogg.wordpress.com/category/computer-stuff/">computer stuff</a>, <a target="_blank" rel="nofollow" href="https://martinsbioblogg.wordpress.com/category/data-analysis/">data analysis</a>, <a target="_blank" rel="nofollow" href="https://martinsbioblogg.wordpress.com/category/med-mera/">med mera</a> Tagged: <a target="_blank" rel="nofollow" href="https://martinsbioblogg.wordpress.com/tag/dplyr/">dplyr</a>, <a target="_blank" rel="nofollow" href="https://martinsbioblogg.wordpress.com/tag/ggplot2/">ggplot2</a>, <a target="_blank" rel="nofollow" href="https://martinsbioblogg.wordpress.com/tag/r/">R</a> <a target="_blank" rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/martinsbioblogg.wordpress.com/4969/"><img alt="" src="http://feeds.wordpress.com/1.0/delicious/martinsbioblogg.wordpress.com/4969/" border="0"></a> <a target="_blank" rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/martinsbioblogg.wordpress.com/4969/"><img alt="" src="http://feeds.wordpress.com/1.0/facebook/martinsbioblogg.wordpress.com/4969/" border="0"></a> <a target="_blank" rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/martinsbioblogg.wordpress.com/4969/"><img alt="" src="http://feeds.wordpress.com/1.0/twitter/martinsbioblogg.wordpress.com/4969/" border="0"></a> <a target="_blank" rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/martinsbioblogg.wordpress.com/4969/"><img alt="" src="http://feeds.wordpress.com/1.0/stumble/martinsbioblogg.wordpress.com/4969/" border="0"></a> <a target="_blank" rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/martinsbioblogg.wordpress.com/4969/"><img alt="" src="http://feeds.wordpress.com/1.0/digg/martinsbioblogg.wordpress.com/4969/" border="0"></a> <a target="_blank" rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/martinsbioblogg.wordpress.com/4969/"><img alt="" src="http://feeds.wordpress.com/1.0/reddit/martinsbioblogg.wordpress.com/4969/" border="0"></a> <img alt="" src="https://i1.wp.com/pixel.wp.com/b.gif?resize=1%2C1&ssl=1" border="0" data-recalc-dims="1"></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="https://martinsbioblogg.wordpress.com/2017/03/09/it-seems-dplyr-is-overtaking-correlation-heatmaps/"> R – On unicorns and genes</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 title="The R Project for Statistical Computing" rel="nofollow" href="https://www.r-project.org/">R</a> news and <a title="R tutorials" rel="nofollow" href="https://www.r-bloggers.com/search/tutorial">tutorials</a> on topics such as: <a title="Data science" rel="nofollow" href="https://www.r-bloggers.com/search/data%20science">Data science</a>, <a title="Big Data" rel="nofollow" href="https://www.r-bloggers.com/search/Big%20Data">Big Data, <a title="R jobs" rel="nofollow" href="https://www.r-users.com/">R jobs</a>, visualization (<a title="ggplot and ggplot2 tutorials" rel="nofollow" href="https://www.r-bloggers.com/search/ggplot2">ggplot2</a>, <a title="Boxplots using lattice and ggplot2 tutorials" rel="nofollow" href="https://www.r-bloggers.com/search/boxplot">Boxplots</a>, <a title="Maps and gis" rel="nofollow" href="https://www.r-bloggers.com/search/map">maps</a>, <a title="Animation in R" rel="nofollow" href="https://www.r-bloggers.com/search/animation">animation</a>), programming (<a title="RStudio IDE for R" rel="nofollow" href="https://www.r-bloggers.com/search/RStudio">RStudio</a>, <a title="Sweave and literate programming" rel="nofollow" href="https://www.r-bloggers.com/search/sweave">Sweave</a>, <a title="LaTeX in R" rel="nofollow" href="https://www.r-bloggers.com/search/LaTeX">LaTeX</a>, <a title="SQL and databases" rel="nofollow" href="https://www.r-bloggers.com/search/SQL">SQL</a>, <a title="Eclipse IDE for R" rel="nofollow" href="https://www.r-bloggers.com/search/eclipse">Eclipse</a>, <a title="git and github, Version Control System" rel="nofollow" href="https://www.r-bloggers.com/search/git">git</a>, <a title="Large data in R using Hadoop" rel="nofollow" href="https://www.r-bloggers.com/search/hadoop">hadoop</a>, <a title="Web Scraping of google, facebook, yahoo, twitter and more using R" rel="nofollow" href="https://www.r-bloggers.com/search/Web+Scraping">Web Scraping</a>) statistics (<a title="Regressions and ANOVA analysis tutorials" rel="nofollow" href="https://www.r-bloggers.com/search/regression">regression</a>, <a title="principal component analysis tutorial" rel="nofollow" href="https://www.r-bloggers.com/search/PCA">PCA</a>, <a title="Time series" rel="nofollow" href="https://www.r-bloggers.com/search/time+series">time series</a>, <a title="finance trading" rel="nofollow" href="https://www.r-bloggers.com/search/trading">trading</a>) and more...
<img height="1" alt="" width="1" src="http://feeds.feedburner.com/~r/RBloggers/~4/8zgOQF8mQ7Y"></a></div>