<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/pipe-friendly-workflow-with-sjplot-sjmisc-and-sjstats-part-1-rstats-tidyverse/"></div></div><div><a data-url="https://www.r-bloggers.com/pipe-friendly-workflow-with-sjplot-sjmisc-and-sjstats-part-1-rstats-tidyverse/" data-counturl="https://www.r-bloggers.com/pipe-friendly-workflow-with-sjplot-sjmisc-and-sjstats-part-1-rstats-tidyverse/" data-count="horizontal" data-text="Pipe-friendly workflow with sjPlot, sjmisc and sjstats, part 1 #rstats #tidyverse" 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="https://strengejacke.wordpress.com/2016/11/14/pipe-friendly-workflow-with-sjplot-sjmisc-and-sjstats-part-1-rstats-tidyverse/"> R – Strenge Jacke!</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<p>Recent development in R packages are increasingly focussing on the philosophy of <a target="_blank" rel="nofollow" href="http://tidyverse.org/">tidy data and a common package design and api</a>. <em>Tidy data</em> is an important part of data exploration and analysis, as shown in the following figure:</p>
<figure data-shortcode="caption" class="wp-caption"><img sizes="(max-width: 517px) 100vw, 517px" alt="(Source: http://r4ds.had.co.nz/explore-intro.html)" class="wp-image-1164" src="https://i1.wp.com/strengejacke.files.wordpress.com/2016/11/data-science-explore.png?w=456&ssl=1" data-recalc-dims="1"><br>
<figcaption class="wp-caption-text">(Source: <a target="_blank" rel="nofollow" href="http://r4ds.had.co.nz/explore-intro.html">http://r4ds.had.co.nz/explore-intro.html</a>)</figcaption>
</figure>
<p>Tidying data not only includes data cleaning, but also data transformation, both being necessary to perform the core steps of data analysis and visualization. This is a complex process, which involves many steps. You need many packages and functions to perfom those tasks. This is where a common package design and api comes into play: „A powerful strategy for solving complex problems is to combine many simple pieces“, says the <a target="_blank" rel="nofollow" href="https://github.com/tidyverse/tidyverse/blob/master/vignettes/manifesto.Rmd#compose-simple-functions-with-the-pipe">tidyverse manifesto</a>. For a coding workflow, this means:</p>
<ul>
<li>compose single functions with the pipe</li>
<li>design your API so that it is easy to use by humans</li>
</ul>
<p>The latter bullet point is helpful to achieve the first bullet point.</p>
<p>The sj-packages, namely <a target="_blank" rel="nofollow" href="https://cran.r-project.org/package=sjPlot">sjPlot</a> <em>(data visualization)</em>, <a target="_blank" rel="nofollow" href="https://cran.r-project.org/package=sjmisc">sjmisc</a> <em>(labelled data and data transformation)</em> and <a target="_blank" rel="nofollow" href="https://cran.r-project.org/package=sjstats">sjstats</a> <em>(common statistical functions, also for regression modelling)</em>, are revised accordingly, to follow this design philosophy (as far as possible and feasible).</p>
<h2>Pipe-friendly functions and tidy data</h2>
<p>The „pipe-operator“ (<code>%>%</code>) was introduced by the <a target="_blank" rel="nofollow" href="https://cran.r-project.org/web/packages/magrittr/vignettes/magrittr.html">magrittr-package</a> and aims at a semantical change of your code, making reading and writing code more intuive. The <code>%>%</code> simply takes the left-hand side of an input and <em>puts its result as first argument to the right-hand side</em>.</p>
<pre>library(sjmisc)
library(magrittr) # for pipe
data(efc)
# show head (first 6 observations) of efc-data
efc %>% head()</pre>
<p>When doing data „tidying“ and transformation, the result of a left-hand side function is usually a data frame (for instance, when working with packages like <a target="_blank" rel="nofollow" href="https://cran.r-project.org/package=dplyr">dplyr</a> or <a target="_blank" rel="nofollow" href="https://cran.r-project.org/package=tidyr">tidyr</a>). Hence, <em>the first argument of a function following the tidyverse-philosophy should always be the data</em>.</p>
<p>In this blog post, I’ll focus on some functions of the <strong>sjPlot</strong>-package.</p>
<h2>Using sjPlot in a pipe-workflow</h2>
<p>The sjPlot-package for data visualization (see comprehensive documentation <a target="_blank" rel="nofollow" href="http://strengejacke.de/sjPlot/">here</a>) already included some functions that required the data as first argument (e.g. <a target="_blank" rel="nofollow" href="http://strengejacke.de/sjPlot/sjp.likert/">sjp.likert()</a> or <a target="_blank" rel="nofollow" href="http://strengejacke.de/sjPlot/sjp.pca/">sjp.pca()</a>). Other commonly used function, however, do not follow this design: <a target="_blank" rel="nofollow" href="http://strengejacke.de/sjPlot/sjp.frq/">sjp.frq()</a> requires a vector, and <a target="_blank" rel="nofollow" href="http://strengejacke.de/sjPlot/sjp.xtab/">sjp.xtab()</a> requires two vectors, but no data frame, impossible to seamlessly integrate in a pipe-workflow.</p>
<pre>library(sjmisc)
data(efc)
sjp.frq(efc$c172code)</pre>
<p><img sizes="(max-width: 600px) 100vw, 600px" alt="" class="wp-image-1175" src="https://i1.wp.com/strengejacke.files.wordpress.com/2016/11/rplot.png?resize=456%2C304&ssl=1" data-recalc-dims="1"></p>
<h3>The sjplot()-function</h3>
<p>On the other hand, to quickly create figures for data exploration, it is often more feasible to just pass a vector to a function, instead of a prepared data frame. For this reason, I decided not to revise these functions and change their argument-structure. Instead, the latest sjPlot-update got a new wrapper function, <code>sjplot()</code>, which allows an easy integration of sjPlot-functions into a pipe-workflow. Since <code>sjplot()</code> is generic, you have to specify the plot-function via the <code>fun</code>-argument. The following code gives you the same figure as above:</p>
<pre>library(sjmisc)
library(dplyr)
data(efc)
efc %>%
  select(c172code) %>%
  sjplot()</pre>
<p>Functions that require multiple vectors as input – like <a target="_blank" rel="nofollow" href="http://strengejacke.de/sjPlot/sjp.scatter/">sjp.scatter()</a> or <a target="_blank" rel="nofollow" href="http://strengejacke.de/sjPlot/sjp.grpfrq/">sjp.grpfrq()</a> – work in the same manner:</p>
<pre>efc %>% 
  select(e17age, c172code, c161sex) %>% 
  sjplot(fun = &quot;scatter&quot;)</pre>
<p><img sizes="(max-width: 600px) 100vw, 600px" alt="" class="wp-image-1181" src="https://i1.wp.com/strengejacke.files.wordpress.com/2016/11/rplot01.png?w=456&ssl=1" data-recalc-dims="1"></p>
<pre>efc %>% 
  select(e17age, c172code) %>% 
  sjplot(fun = &quot;grpfrq&quot;, 
         type = &quot;box&quot;, 
         geom.colors = &quot;Set1&quot;)</pre>
<p><img sizes="(max-width: 600px) 100vw, 600px" alt="" class="wp-image-1183" src="https://i2.wp.com/strengejacke.files.wordpress.com/2016/11/rplot02.png?w=456&ssl=1" data-recalc-dims="1"></p>
<h3>The plot_grid() function</h3>
<p>Another convenient function is <code>plot_grid()</code>, which arranges multiple plots into a single grid-layout plot.</p>
<pre>efc %>% 
  select(e42dep, c172code, e16sex, c161sex) %>% 
  sjplot() %>% 
  plot_grid()</pre>
<p><a target="_blank" rel="nofollow" href="https://i2.wp.com/strengejacke.files.wordpress.com/2016/11/rplot1.png?ssl=1"><img sizes="(max-width: 616px) 100vw, 616px" alt="" class="wp-image-1190" src="https://i2.wp.com/strengejacke.files.wordpress.com/2016/11/rplot1.png?resize=456%2C380&ssl=1" data-recalc-dims="1"></a></p>
<p>This function also accepts plot lists that are returned by some of the package’s function, which create multiple plots. It is an alternative to facetting plots. For example, plotting marginal effects of model predictors, by default arranges the plots in a facet-grid:</p>
<pre>fit &lt;- lm(tot_sc_e ~ c12hour + e17age + 
          e42dep + neg_c_7, data = efc)
# plot marginal effects, as facet grid
sjp.lm(fit, type = &quot;eff&quot;)</pre>
<p><a target="_blank" rel="nofollow" href="https://i0.wp.com/strengejacke.files.wordpress.com/2016/11/rplot2.png?ssl=1"><img sizes="(max-width: 616px) 100vw, 616px" alt="" class="wp-image-1194" src="https://i0.wp.com/strengejacke.files.wordpress.com/2016/11/rplot2.png?resize=456%2C342&ssl=1" data-recalc-dims="1"></a></p>
<p>One disadvantage is the common axis-scale – all scales are continuous, although „dependency“ is a factor. Using <code>plot_grid()</code> solves this problem:</p>
<pre># plot marginal effects for each 
# predictor, each as single plot
p &lt;- sjp.lm(fit, type = &quot;eff&quot;, 
            facet.grid = FALSE, 
            prnt.plot = FALSE)
# &quot;p&quot; has an element &quot;$plot.list&quot;, which 
# will automatically be used by plot_grid
plot_grid(p)</pre>
<p><a target="_blank" rel="nofollow" href="https://i0.wp.com/strengejacke.files.wordpress.com/2016/11/pg1.png?ssl=1"><img sizes="(max-width: 616px) 100vw, 616px" alt="" class="wp-image-1195" src="https://i0.wp.com/strengejacke.files.wordpress.com/2016/11/pg1.png?resize=456%2C365&ssl=1" data-recalc-dims="1"></a></p>
<h2>Final words</h2>
<p>These were some examples of how to use the <a target="_blank" rel="nofollow" href="https://cran.r-project.org/package=sjPlot">sjPlot-package</a> in a pipe-workflow. Feel free to add your comments, further suggestions, either here or (preferably) <a target="_blank" rel="nofollow" href="https://github.com/sjPlot/devel/">at GitHub</a>.</p>
<p>The next posting will cover new aspects of the <strong>sjmisc</strong> and <strong>sjstats</strong> packages.</p>
<p> Tagged: <a target="_blank" rel="nofollow" href="https://strengejacke.wordpress.com/tag/data-visualization/">data visualization</a>, <a target="_blank" rel="nofollow" href="https://strengejacke.wordpress.com/tag/ggplot/">ggplot</a>, <a target="_blank" rel="nofollow" href="https://strengejacke.wordpress.com/tag/r/">R</a>, <a target="_blank" rel="nofollow" href="https://strengejacke.wordpress.com/tag/rstats/">rstats</a>, <a target="_blank" rel="nofollow" href="https://strengejacke.wordpress.com/tag/sjplot/">sjPlot</a>, <a target="_blank" rel="nofollow" href="https://strengejacke.wordpress.com/tag/tidyverse/">tidyverse</a> <a target="_blank" rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/strengejacke.wordpress.com/1163/"><img alt="" src="http://feeds.wordpress.com/1.0/comments/strengejacke.wordpress.com/1163/" 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><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/pipe-friendly-workflow-with-sjplot-sjmisc-and-sjstats-part-1-rstats-tidyverse/"></div></div><div><a data-url="https://www.r-bloggers.com/pipe-friendly-workflow-with-sjplot-sjmisc-and-sjstats-part-1-rstats-tidyverse/" data-counturl="https://www.r-bloggers.com/pipe-friendly-workflow-with-sjplot-sjmisc-and-sjstats-part-1-rstats-tidyverse/" data-count="vertical" data-text="Pipe-friendly workflow with sjPlot, sjmisc and sjstats, part 1 #rstats #tidyverse" 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="https://strengejacke.wordpress.com/2016/11/14/pipe-friendly-workflow-with-sjplot-sjmisc-and-sjstats-part-1-rstats-tidyverse/"> R – Strenge Jacke!</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/?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="https://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/_AycoPEZ0tc"></a></div>