<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/afex_plot-publication-ready-plots-for-factorial-designs/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="http://singmann.org/afex_plot/"> Computational Psychology - Henrik Singmann</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<p>I am happy to announce that a new version of <a rel="nofollow" href="https://cran.r-project.org/package=afex" target="_blank"><code>afex</code> (version 0.22-1)</a> has appeared on CRAN. This version comes with two major changes, for more see <a rel="nofollow" href="https://cran.r-project.org/web/packages/afex/NEWS" target="_blank">the NEWS file</a>. To get the new version including all packages used in the examples run:</p>
<pre><code>install.packages(&quot;afex&quot;, dependencies = TRUE)
</code></pre>
<p>First, <code>afex</code> does not load or attach package <a rel="nofollow" href="https://cran.r-project.org/package=emmeans" target="_blank"><code>emmeans</code></a> automatically anymore. This reduces the package footprint and makes it more lightweight. If you want to use <code>afex</code> without using <code>emmeans</code>, you can do this now. The consequence of this is that you have to attach <code>emmeans</code> explicitly if you want to continue using <code>emmeans()</code> et al. in the same manner. Simply add <code>library(&quot;emmeans&quot;)</code> to the top of your script just below <code>library(&quot;afex&quot;)</code> and things remain unchanged. Alternatively, you can use <code>emmeans::emmeans()</code> without attaching the package.</p>
<p>Second and more importantly, I have added a new plotting function to <code>afex</code>. <code>afex_plot()</code> visualizes results from factorial experiments combining estimated marginal means and associated uncertainties (i.e., error bars) in the foreground with a depiction of the raw data in the background. Currently, <code>afex_plots()</code> supports ANOVAs and mixed models fitted with <code>afex</code> as well as mixed models fitted with <code>lme4</code> (support for more models will come in the next version). As shown in the example below, <code>afex_plots()</code> makes it easy to produce nice looking plots that are ready to be incorporated into publications. Importantly, <code>afex_plots()</code> allows different types of error bars, including within-subjects confidence intervals, which makes it particularly useful for fields where such designs are very common (e.g., psychology). Furthermore, <code>afex_plots()</code> is built on <code>ggplot2</code> and designed in a modular manner, making it easy to customize the plot to ones personal preferences.</p>
<p><code>afex_plot()</code> requires the fitted model object as first argument and then has three arguments determining which factor or factors are displayed how:<br>
– <code>x</code> is necessary and specifies the factor(s) plotted on the x-axis<br>
– <code>trace</code> is optional and specifies the factor(s) plotted as separate lines (i.e., with each factor-level present at each x-axis tick)<br>
– <code>panel</code> is optional and specifies the factor(s) which separate the plot into different panels.</p>
<p>The further arguments make it easy to customize the plot in various ways. A comprehensive overview is provided in the <a rel="nofollow" href="https://singmann.github.io/afex_plot_introduction.html" target="_blank">new vignette</a>, further details, specifically regarding the question of which type of error bars are supported, is given on <a rel="nofollow" href="https://www.rdocumentation.org/packages/afex/versions/0.22-1/topics/afex_plot?tap_a=5644-dce66f&tap_s=10907-287229" target="_blank">its help page</a> (which also has many more examples).</p>
<p>Let us look at an example. We take data from a 3 by 2 within-subject experiment that also features prominently in the vignette. Note that we plot within-subjects confidence intervals (by setting <code>error = &quot;within&quot;</code>) and then customize the plot quite a bit by changing the theme, using nicer labels, removing some y-axis ticks, adding colour, and using a customized <code>geom</code> (<code>geom_boxjitter</code> from the <a rel="nofollow" href="https://cran.r-project.org/package=ggpol" target="_blank"><code>ggpol</code></a> package) for displaying the data in the background.</p>
<pre><code>library(&quot;afex&quot;) 
library(&quot;ggplot2&quot;) 
data(md_12.1)
aw &lt;- aov_ez(&quot;id&quot;, &quot;rt&quot;, md_12.1, within = c(&quot;angle&quot;, &quot;noise&quot;))

afex_plot(aw, x = &quot;angle&quot;, trace = &quot;noise&quot;, error = &quot;within&quot;,
          mapping = c(&quot;shape&quot;, &quot;fill&quot;), dodge = 0.7,
          data_geom = ggpol::geom_boxjitter, 
          data_arg = list(
            width = 0.5, 
            jitter.width = 0,
            jitter.height = 10,
            outlier.intersect = TRUE),
          point_arg = list(size = 2.5), 
          error_arg = list(size = 1.5, width = 0),
          factor_levels = list(angle = c(&quot;0°&quot;, &quot;4°&quot;, &quot;8°&quot;),
                               noise = c(&quot;Absent&quot;, &quot;Present&quot;)), 
          legend_title = &quot;Noise&quot;) +
  labs(y = &quot;RTs (in ms)&quot;, x = &quot;Angle (in degrees)&quot;) +
  scale_y_continuous(breaks=seq(400, 900, length.out = 3)) +
  theme_bw(base_size = 15) + 
  theme(legend.position=&quot;bottom&quot;, panel.grid.major.x = element_blank())

ggsave(&quot;afex_plot.png&quot;, device = &quot;png&quot;, dpi = 600,
       width = 8.5, height = 8, units = &quot;cm&quot;) 
</code></pre>
<p><img alt="" src="https://i1.wp.com/singmann.org/wp-content/uploads/2018/09/afex_plot.png?w=620" data-recalc-dims="1"></p>
<p>In the plot, the black dots are the means and the thick black lines the 95% within-subject confidence intervals. The raw data is displayed in the background with a half box plot showing the median and upper and lower quartile as well as the raw data. The raw data is jittered on the y-axis to avoid perfect overlap.</p>
<hr>
<p>One final thing to note. In the vignette on CRAN as well as the help page there is an error in the code. The name of the argument for changing the labels of the factor-levels is <code>factor_levels</code> and not <code>new_levels</code>. The vignette linked above and <a rel="nofollow" href="https://singmann.github.io/afex_plot_introduction.html" target="_blank">here</a> uses the correct argument name. This is already corrected on <a rel="nofollow" href="https://github.com/singmann/afex/" target="_blank">github</a> and will be corrected on CRAN with the next release.</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://singmann.org/afex_plot/"> Computational Psychology - Henrik Singmann</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/QkEygN1yExI" height="1"></a></div>