<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/plot-some-variables-against-many-others-with-tidyr-and-ggplot2/"></div></div><div><a data-url="https://www.r-bloggers.com/plot-some-variables-against-many-others-with-tidyr-and-ggplot2/" data-counturl="https://www.r-bloggers.com/plot-some-variables-against-many-others-with-tidyr-and-ggplot2/" data-count="horizontal" data-text="Plot some variables against many others with tidyr and ggplot2" class="twitter-share-button" data-via="rbloggers" href="https://twitter.com/share"></a></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="http://drsimonj.svbtle.com/plot-some-variables-against-many-others"> blogR</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<p>Want to see how some of your variables relate to many others? Here’s an example of just this:</p>
<pre><code>library(tidyr)
library(ggplot2)

mtcars %>%
  gather(-mpg, -hp, -cyl, key = &quot;var&quot;, value = &quot;value&quot;) %>% 
  ggplot(aes(x = value, y = mpg, color = hp, shape = factor(cyl))) +
    geom_point() +
    facet_wrap(~ var, scales = &quot;free&quot;) +
    theme_bw()
</code></pre>
<p><a target="_blank" href="https://i1.wp.com/svbtleusercontent.com/qx6qe77ajmc5w.png?ssl=1"><img alt="init-example-1.png" src="https://i1.wp.com/svbtleusercontent.com/qx6qe77ajmc5w_small.png?w=456&ssl=1" data-recalc-dims="1"></a></p>
<p>This plot shows a separate scatter plot panel for each of many variables against <code>mpg</code>; all points are coloured by <code>hp</code>, and the shapes refer to <code>cyl</code>.</p>
<p>Let’s break it down.</p>
<h2><a target="_blank" href="http://drsimonj.svbtle.com/plot-some-variables-against-many-others#some-previous-advice_2"> </a>Some previous advice</h2>
<p>This post is an extension of a previous one that appears here: <a target="_blank" href="https://drsimonj.svbtle.com/quick-plot-of-all-variables">https://drsimonj.svbtle.com/quick-plot-of-all-variables</a>.</p>
<p>In that prior post, I explained a method for plotting the univariate distributions of many numeric variables in a data frame. This post does something very similar, but with a few tweaks that produce a very useful result. So, in general, I’ll skip over a few minor parts that appear in the previous post (e.g., how to use <code>purrr::keep()</code> if you want only variables of a particular type).</p>
<h2><a target="_blank" href="http://drsimonj.svbtle.com/plot-some-variables-against-many-others#tidying-our-data_2"> </a>Tidying our data</h2>
<p>As in the previous post, I’ll mention that you might be interested in using something like a <code>for</code> loop to create each plot. Personally, however, I think this is a messy way to do it. Instead, we’ll make use of the <code>facet_wrap()</code> function in the <code>ggplot2</code> package, but doing so requires some careful data prep. Thus, assuming our data frame has all the variables we’re interested in, the first step is to get our data into a tidy form that is suitable for plotting.</p>
<p>We’ll do this using <code>gather()</code> from the <code>tidyr</code> package. In the previous post, we gathered all of our variables as follows (using <code>mtcars</code> as our example data set):</p>
<pre><code>library(tidyr)
mtcars %>% gather() %>% head()
#>   key value
#> 1 mpg  21.0
#> 2 mpg  21.0
#> 3 mpg  22.8
#> 4 mpg  21.4
#> 5 mpg  18.7
#> 6 mpg  18.1
</code></pre>
<p>This gives us a <code>key</code> column with the variable names and a <code>value</code> column with their corresponding values. This works well if we only want to plot each variable by itself (e.g., to get univariate information).</p>
<p>However, here we’re interested in visualising multivariate information, with a particular focus on one or two variables. We’ll start with the bivariate case. Within <code>gather()</code>, we’ll first drop our variable of interest (say <code>mpg</code>) as follows:</p>
<pre><code>mtcars %>% gather(-mpg, key = &quot;var&quot;, value = &quot;value&quot;) %>% head()
#>    mpg var value
#> 1 21.0 cyl     6
#> 2 21.0 cyl     6
#> 3 22.8 cyl     4
#> 4 21.4 cyl     6
#> 5 18.7 cyl     8
#> 6 18.1 cyl     6
</code></pre>
<p>We now have an <code>mpg</code> column with the values of <code>mpg</code> repeated for each variable in the <code>var</code> column. The <code>value</code> column contains the values corresponding to the variable in the <code>var</code> column. This simple extension is how we can use <code>gather()</code> to get our data into shape.</p>
<h2><a target="_blank" href="http://drsimonj.svbtle.com/plot-some-variables-against-many-others#creating-the-plot_2"> </a>Creating the plot</h2>
<p>We now move to the <code>ggplot2</code> package in much the same way we did in the previous post. We want a scatter plot of <code>mpg</code> with each variable in the <code>var</code> column, whose values are in the <code>value</code> column. Creating a scatter plot is handled by <code>ggplot()</code> and <code>geom_point()</code>. Getting a separate panel for each variable is handled by <code>facet_wrap()</code>. We also want the scales for each panel to be “free”. Otherwise, <code>ggplot</code> will constrain them all the be equal, which doesn’t make sense for plotting different variables. For a clean look, let’s also add <code>theme_bw()</code>.</p>
<pre><code>mtcars %>%
  gather(-mpg, key = &quot;var&quot;, value = &quot;value&quot;) %>%
  ggplot(aes(x = value, y = mpg)) +
    geom_point() +
    facet_wrap(~ var, scales = &quot;free&quot;) +
    theme_bw()
</code></pre>
<p><a target="_blank" href="https://i0.wp.com/svbtleusercontent.com/bzguupb09y6aw.png?ssl=1"><img alt="step1-1.png" src="https://i2.wp.com/svbtleusercontent.com/bzguupb09y6aw_small.png?w=456&ssl=1" data-recalc-dims="1"></a></p>
<p>We now have a scatter plot of every variable against <code>mpg</code>. Let’s see what else we can do.</p>
<h2><a target="_blank" href="http://drsimonj.svbtle.com/plot-some-variables-against-many-others#extracting-more-than-one-variable_2"> </a>Extracting more than one variable</h2>
<p>We can layer other variables into these plots. For example, say we want to colour the points based on <code>hp</code>. To do this, we also drop <code>hp</code> within <code>gather()</code>, and then include it appropriately in the plotting stage:</p>
<pre><code>mtcars %>%
  gather(-mpg, -hp, key = &quot;var&quot;, value = &quot;value&quot;) %>% 
  head()
#>    mpg  hp var value
#> 1 21.0 110 cyl     6
#> 2 21.0 110 cyl     6
#> 3 22.8  93 cyl     4
#> 4 21.4 110 cyl     6
#> 5 18.7 175 cyl     8
#> 6 18.1 105 cyl     6

mtcars %>%
  gather(-mpg, -hp, key = &quot;var&quot;, value = &quot;value&quot;) %>%
  ggplot(aes(x = value, y = mpg, color = hp)) +
    geom_point() +
    facet_wrap(~ var, scales = &quot;free&quot;) +
    theme_bw()
</code></pre>
<p><a target="_blank" href="https://i2.wp.com/svbtleusercontent.com/kohbk7wqagwxag.png?ssl=1"><img alt="extra1-1.png" src="https://i1.wp.com/svbtleusercontent.com/kohbk7wqagwxag_small.png?w=456&ssl=1" data-recalc-dims="1"></a></p>
<p>Let’s go crazy and change the point shape by <code>cyl</code>:</p>
<pre><code>mtcars %>%
  gather(-mpg, -hp, -cyl, key = &quot;var&quot;, value = &quot;value&quot;) %>% 
  head()
#>    mpg cyl  hp  var value
#> 1 21.0   6 110 disp   160
#> 2 21.0   6 110 disp   160
#> 3 22.8   4  93 disp   108
#> 4 21.4   6 110 disp   258
#> 5 18.7   8 175 disp   360
#> 6 18.1   6 105 disp   225

mtcars %>%
  gather(-mpg, -hp, -cyl, key = &quot;var&quot;, value = &quot;value&quot;) %>%
  ggplot(aes(x = value, y = mpg, color = hp, shape = factor(cyl))) +
    geom_point() +
    facet_wrap(~ var, scales = &quot;free&quot;) +
    theme_bw()
</code></pre>
<p><a target="_blank" href="https://i1.wp.com/svbtleusercontent.com/qhfm03l181mia.png?ssl=1"><img alt="extra2-1.png" src="https://i0.wp.com/svbtleusercontent.com/qhfm03l181mia_small.png?w=456&ssl=1" data-recalc-dims="1"></a></p>
<h2><a target="_blank" href="http://drsimonj.svbtle.com/plot-some-variables-against-many-others#perks-of-ggplot2_2"> </a>Perks of ggplot2</h2>
<p>If you’re familiar with <code>ggplot2</code>, you can go to town. For example, let’s add loess lines with <code>stat_smooth()</code>:</p>
<pre><code>mtcars %>%
  gather(-mpg, key = &quot;var&quot;, value = &quot;value&quot;) %>%
  ggplot(aes(x = value, y = mpg)) +
    geom_point() +
    stat_smooth() +
    facet_wrap(~ var, scales = &quot;free&quot;) +
    theme_bw()
</code></pre>
<p><a target="_blank" href="https://i0.wp.com/svbtleusercontent.com/olcnissgkknqpw.png?ssl=1"><img alt="bonus-1.png" src="https://i0.wp.com/svbtleusercontent.com/olcnissgkknqpw_small.png?w=456&ssl=1" data-recalc-dims="1"></a></p>
<p>The options are nearly endless at this point, so I’ll stop here.</p>
<h2><a target="_blank" href="http://drsimonj.svbtle.com/plot-some-variables-against-many-others#sign-off_2"> </a>Sign off</h2>
<p>Thanks for reading and I hope this was useful for you.</p>
<p>For updates of recent blog posts, follow <a target="_blank" href="https://twitter.com/drsimonj">@drsimonj</a> on Twitter, or email me at <a target="_blank" href="mailto:drsimonjackson@gmail.com">drsimonjackson@gmail.com</a> to get in touch.</p>
<p>If you’d like the code that produced this blog, check out the <a target="_blank" href="https://github.com/drsimonj/blogR">blogR GitHub repository</a>.</p>
<p><div><div><div><div data-width="55" data-show-faces="false" data-send="true" data-layout="box_count" data-height="62" data-href="https://www.r-bloggers.com/plot-some-variables-against-many-others-with-tidyr-and-ggplot2/"></div></div><div><a data-url="https://www.r-bloggers.com/plot-some-variables-against-many-others-with-tidyr-and-ggplot2/" data-counturl="https://www.r-bloggers.com/plot-some-variables-against-many-others-with-tidyr-and-ggplot2/" data-count="vertical" data-text="Plot some variables against many others with tidyr and ggplot2" class="twitter-share-button" data-via="rbloggers" href="https://twitter.com/share"></a></div><div></div></div><div></div></div>
<div>
<div>To <strong>leave a comment</strong> for the author, please follow the link and comment on their blog: <strong><a href="http://drsimonj.svbtle.com/plot-some-variables-against-many-others"> blogR</a></strong>.</div>
<hr>
<a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers.com</a> offers <strong><a rel="nofollow" href="http://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="http://www.r-project.org/">R</a> news and <a title="R tutorials" rel="nofollow" href="https://www.r-bloggers.com/?s=tutorial">tutorials</a> on topics such as: <a title="Data science" rel="nofollow" href="https://www.r-bloggers.com/?s=data%20science">Data science</a>, <a title="Big Data" rel="nofollow" href="https://www.r-bloggers.com/?s=Big%20Data">Big Data, <a title="R jobs" rel="nofollow" href="http://www.r-users.com/">R jobs</a>, visualization (<a title="ggplot and ggplot2 tutorials" rel="nofollow" href="https://www.r-bloggers.com/?s=ggplot2">ggplot2</a>, <a title="Boxplots using lattice and ggplot2 tutorials" rel="nofollow" href="https://www.r-bloggers.com/?s=boxplot">Boxplots</a>, <a title="Maps and gis" rel="nofollow" href="https://www.r-bloggers.com/?s=map">maps</a>, <a title="Animation in R" rel="nofollow" href="https://www.r-bloggers.com/?s=animation">animation</a>), programming (<a title="RStudio IDE for R" rel="nofollow" href="https://www.r-bloggers.com/?s=RStudio">RStudio</a>, <a title="Sweave and literate programming" rel="nofollow" href="https://www.r-bloggers.com/?s=sweave">Sweave</a>, <a title="LaTeX in R" rel="nofollow" href="https://www.r-bloggers.com/?s=LaTeX">LaTeX</a>, <a title="SQL and databases" rel="nofollow" href="https://www.r-bloggers.com/?s=SQL">SQL</a>, <a title="Eclipse IDE for R" rel="nofollow" href="https://www.r-bloggers.com/?s=eclipse">Eclipse</a>, <a title="git and github, Version Control System" rel="nofollow" href="https://www.r-bloggers.com/?s=git">git</a>, <a title="Large data in R using Hadoop" rel="nofollow" href="https://www.r-bloggers.com/?s=hadoop">hadoop</a>, <a title="Web Scraping of google, facebook, yahoo, twitter and more using R" rel="nofollow" href="https://www.r-bloggers.com/?s=Web+Scraping">Web Scraping</a>) statistics (<a title="Regressions and ANOVA analysis tutorials" rel="nofollow" href="https://www.r-bloggers.com/?s=regression">regression</a>, <a title="principal component analysis tutorial" rel="nofollow" href="https://www.r-bloggers.com/?s=PCA">PCA</a>, <a title="Time series" rel="nofollow" href="https://www.r-bloggers.com/?s=time+series">time series</a>, <a title="finance trading" rel="nofollow" href="https://www.r-bloggers.com/?s=trading">trading</a>) and more...
<img height="1" alt="" width="1" src="http://feeds.feedburner.com/~r/RBloggers/~4/fHtoZ8qm7Ag"></a></div>