<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/how-to-combine-point-and-boxplots-in-timeline-charts-with-ggplot2-facets/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="https://shirinsplayground.netlify.com/2017/11/combine_point_boxplot_ggplot/"> Shirin's playgRound</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<p>In a recent project, I was looking to plot data from different variables along the same time axis. The difficulty was, that some of these variables I wanted to have as point plots, while others I wanted as box-plots.</p>
<p>Because I work with the tidyverse, I wanted to produce these plots with ggplot2. Faceting was the obvious first step but it took me quite a while to figure out how to best combine facets with point plots (where I have one value per time point) with and box-plots (where I have multiple values per time point).</p>
<p>The reason why this isn’t trivial is that box plots require groups or factors on the x-axis, while points can be plotted over a continuous range of x-values. If your alarm bells are ringing right now, you are absolutely right: before you try to combine plots with different x-axis properties, you should think long and hard whether this is an accurate representation of the data and if its a good idea to do so! Here, I had multiple values per time point for one variable and I wanted to make the median + variation explicitly clear, while also showing the continuous changes of other variables over the same range of time.</p>
<p>So, I am writing this short tutorial here in hopes that it saves the next person trying to do something similar from spending an entire morning on stackoverflow. <img alt="😉" src="https://s.w.org/images/core/emoji/2.3/72x72/1f609.png" class="wp-smiley"></p>
<p>For this demonstration, I am creating some fake data:</p>
<pre><code>library(tidyverse)
dates &lt;- seq(as.POSIXct(&quot;2017-10-01 07:00&quot;), as.POSIXct(&quot;2017-10-01 10:30&quot;), by = 180) # 180 seconds == 3 minutes
fake_data &lt;- data.frame(time = dates,
                        var1_1 = runif(length(dates)),
                        var1_2 = runif(length(dates)),
                        var1_3 = runif(length(dates)),
                        var2 = runif(length(dates))) %>%
  sample_frac(size = 0.33)
head(fake_data)</code></pre>
<pre><code>##                   time    var1_1    var1_2    var1_3       var2
## 8  2017-10-01 07:21:00 0.2359625 0.6121708 0.4114921 0.03327728
## 27 2017-10-01 08:18:00 0.5592436 0.3834683 0.8025474 0.44557932
## 29 2017-10-01 08:24:00 0.7667775 0.4636693 0.7642972 0.97718507
## 18 2017-10-01 07:51:00 0.2819686 0.3995273 0.9127757 0.42115579
## 1  2017-10-01 07:00:00 0.5940754 0.1599054 0.7287677 0.91953437
## 71 2017-10-01 10:30:00 0.2159290 0.2853349 0.7817291 0.57598897</code></pre>
<p>Here, variable 1 (<code>var1</code>) has three measurements per time point, while variable 2 (<code>var2</code>) has one.</p>
<p>First, for plotting with ggplot2 we want our data in a tidy long format. I also add another column for faceting that groups the variables from <code>var1</code> together.</p>
<pre><code>fake_data_long &lt;- fake_data %>%
  gather(x, y, var1_1:var2) %>%
  mutate(facet = ifelse(x %in% c(&quot;var1_1&quot;, &quot;var1_2&quot;, &quot;var1_3&quot;), &quot;var1&quot;, x))
head(fake_data_long)</code></pre>
<pre><code>##                  time      x         y facet
## 1 2017-10-01 07:21:00 var1_1 0.2359625  var1
## 2 2017-10-01 08:18:00 var1_1 0.5592436  var1
## 3 2017-10-01 08:24:00 var1_1 0.7667775  var1
## 4 2017-10-01 07:51:00 var1_1 0.2819686  var1
## 5 2017-10-01 07:00:00 var1_1 0.5940754  var1
## 6 2017-10-01 10:30:00 var1_1 0.2159290  var1</code></pre>
<p>Now, we can plot this the following way:</p>
<ul>
<li>facet by variable</li>
<li>subset data to facets for point plots and give aesthetics in <code>geom_point()</code></li>
<li>subset data to facets for box plots and give aesthetics in <code>geom_boxplot()</code>. Here we also need to set the <code>group</code> aesthetic; if we don’t specifically give that, we will get a plot with one big box, instead of a box for every time point.</li>
</ul>
<pre><code>fake_data_long %>%
  ggplot() +
    facet_grid(facet ~ ., scales = &quot;free&quot;) +
    geom_point(data = subset(fake_data_long, facet == &quot;var2&quot;), 
               aes(x = time, y = y),
               size = 1) +
    geom_line(data = subset(fake_data_long, facet == &quot;var2&quot;), 
               aes(x = time, y = y)) +
    geom_boxplot(data = subset(fake_data_long, facet == &quot;var1&quot;), 
               aes(x = time, y = y, group = time))</code></pre>
<p><img src="https://i0.wp.com/shirinsplayground.netlify.com/post/2017-11-18-combine_point_boxplot_ggplot_files/figure-html/unnamed-chunk-3-1.png?w=450&ssl=1" data-recalc-dims="1"></p>
<pre><code>sessionInfo()</code></pre>
<pre><code>## R version 3.4.2 (2017-09-28)
## Platform: x86_64-apple-darwin15.6.0 (64-bit)
## Running under: macOS High Sierra 10.13.1
## 
## Matrix products: default
## BLAS: /Library/Frameworks/R.framework/Versions/3.4/Resources/lib/libRblas.0.dylib
## LAPACK: /Library/Frameworks/R.framework/Versions/3.4/Resources/lib/libRlapack.dylib
## 
## locale:
## [1] de_DE.UTF-8/de_DE.UTF-8/de_DE.UTF-8/C/de_DE.UTF-8/de_DE.UTF-8
## 
## attached base packages:
## [1] methods   stats     graphics  grDevices utils     datasets  base     
## 
## other attached packages:
##  [1] bindrcpp_0.2    forcats_0.2.0   stringr_1.2.0   dplyr_0.7.4    
##  [5] purrr_0.2.4     readr_1.1.1     tidyr_0.7.2     tibble_1.3.4   
##  [9] ggplot2_2.2.1   tidyverse_1.2.1
## 
## loaded via a namespace (and not attached):
##  [1] tidyselect_0.2.3 reshape2_1.4.2   haven_1.1.0      lattice_0.20-35 
##  [5] colorspace_1.3-2 htmltools_0.3.6  yaml_2.1.14      rlang_0.1.4     
##  [9] foreign_0.8-69   glue_1.2.0       modelr_0.1.1     readxl_1.0.0    
## [13] bindr_0.1        plyr_1.8.4       munsell_0.4.3    blogdown_0.3    
## [17] gtable_0.2.0     cellranger_1.1.0 rvest_0.3.2      psych_1.7.8     
## [21] evaluate_0.10.1  labeling_0.3     knitr_1.17       parallel_3.4.2  
## [25] broom_0.4.2      Rcpp_0.12.13     scales_0.5.0     backports_1.1.1 
## [29] jsonlite_1.5     mnormt_1.5-5     hms_0.3          digest_0.6.12   
## [33] stringi_1.1.5    bookdown_0.5     grid_3.4.2       rprojroot_1.2   
## [37] cli_1.0.0        tools_3.4.2      magrittr_1.5     lazyeval_0.2.1  
## [41] crayon_1.3.4     pkgconfig_2.0.1  xml2_1.1.1       lubridate_1.7.1 
## [45] assertthat_0.2.0 rmarkdown_1.7    httr_1.3.1       rstudioapi_0.7  
## [49] R6_2.2.2         nlme_3.1-131     compiler_3.4.2</code></pre>
	<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://shirinsplayground.netlify.com/2017/11/combine_point_boxplot_ggplot/"> Shirin's playgRound</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/1RYDKfV5nMY" height="1"></a></div>