<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/make-bland-altman-plots-with-marginal-histograms-using-ggextra/"></div></div><div><a data-url="https://www.r-bloggers.com/make-bland-altman-plots-with-marginal-histograms-using-ggextra/" data-counturl="https://www.r-bloggers.com/make-bland-altman-plots-with-marginal-histograms-using-ggextra/" data-count="horizontal" data-text="Make Bland Altman Plots with Marginal Histograms using ggExtra" 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://labrtorian.com/2016/08/29/make-bland-altman-plots-with-marginal-histograms-using-ggextra/"> The Lab-R-torian</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<h2>The Problem</h2>
<p>As you know in Clinical Chemistry, we are not always writing a major paper but sometimes just preparing a short-report to answer a technical question that we’ve encounted at work. For shorter papers, journals often have more stringent rules about how many figures you can submit and even sometimes forbid multipanelled figures. In these situations, we might want to cram a little more into your figure than we might otherwise. In a recent submission, I wanted to produce a difference plot of immunoassay results before and after storage but I also wanted to show the distribution of the results using a histogram–but this would have counted as two separate figures.</p>
<p>However, thanks to some fine work by <a target="_blank" href="http://deanattali.com/"> Dean Attali</a> of UBC Department of Statistics where he works with R-legend <a target="_blank" href="http://www.stat.ubc.ca/~jenny/">Jenny Bryan</a>, it is quite easy to add marginal histograms to a Bland Altman (or any other scatter) plot using the <a target="_blank" href="https://cran.r-project.org/web/packages/ggExtra/vignettes/overview.html">ggExtra package</a>.</p>
<h2>How To</h2>
<p>Let’s make some fake data for a <a target="_blank" href="http://www.sciencedirect.com/science/article/pii/S0140673686908378">Bland Altman plot</a>. Let’s pretend that we are measuring the same quantity by immunoassay at baseline and after 1 year of storage at -80 degrees. We’ll add some heteroscedastic error and create some apparent degradation of about 20%:</p>
<pre>set.seed(10) #make predictable random data
baseline &lt;- rlnorm(100, 0, 1)
post &lt;- 0.8*baseline + rnorm(100, 0, 0.10*baseline)
plot(baseline,post)
abline(lm(post ~ baseline))
abline(0, 1, col=&quot;red&quot;, lty = 2)</pre>
<p><img alt="plot of chunk unnamed-chunk-1" src="http://i0.wp.com/labrtorian.com/wp-content/uploads/2016/08/unnamed-chunk-1-1.png?w=604" data-recalc-dims="1"></p>
<p>Or if we plot this in the <code>ggplot()</code> paradigm</p>
<pre>library(ggplot2)
my.data &lt;- data.frame(baseline, post)
ggplot(my.data, aes(x=baseline, y=post)) +
    theme_bw() + 
    geom_point(shape=1) +    # Use hollow circles
    geom_smooth(method=lm) +  # Add linear regression line 
    geom_abline(slope = 1, intercept = 0, linetype = 2, colour = &quot;red&quot;)</pre>
<p><img alt="plot of chunk unnamed-chunk-2" src="http://i2.wp.com/labrtorian.com/wp-content/uploads/2016/08/unnamed-chunk-2-1.png?w=604" data-recalc-dims="1"></p>
<p>Now we will prepare the difference data:</p>
<pre>diff &lt;- (post - baseline)
diffp &lt;- (post - baseline)/baseline*100
sd.diff &lt;- sd(diff)
sd.diffp &lt;- sd(diffp)
my.data &lt;- data.frame(baseline, post, diff, diffp)</pre>
<p>In standard Bland Altman plots, one plots the difference between methods against the average of the methods, but in this case, the x-axis should be the baseline result, because that is the closest thing we have to the truth.</p>
<pre>library(ggExtra)
diffplot &lt;- ggplot(my.data, aes(baseline, diff)) + 
  geom_point(size=2, colour = rgb(0,0,0, alpha = 0.5)) + 
  theme_bw() + 
  geom_hline(yintercept = 0, linetype = 3) +
  geom_hline(yintercept = mean(my.data$diff)) +
  geom_hline(yintercept = mean(my.data$diff) + 2*sd.diff, linetype = 2) +
  geom_hline(yintercept = mean(my.data$diff) - 2*sd.diff, linetype = 2) +
  ylab(&quot;Difference pre and post Storage (mg/L)&quot;) +
  xlab(&quot;Baseline Concentration (mg/L)&quot;)

#And now for the magic - we'll use 25 bins
ggMarginal(diffplot, type=&quot;histogram&quot;, bins = 25)</pre>
<p><img alt="plot of chunk unnamed-chunk-4" src="http://i1.wp.com/labrtorian.com/wp-content/uploads/2016/08/unnamed-chunk-4-1.png?w=604" data-recalc-dims="1"></p>
<p>So that is the difference plot for the absolute difference. We can also obviously do the percent difference.</p>
<pre>diffplotp &lt;- ggplot(my.data, aes(baseline, diffp)) + 
  geom_point(size=2, colour = rgb(0,0,0, alpha = 0.5)) + 
  theme_bw() + 
  geom_hline(yintercept = 0, linetype = 3) +
  geom_hline(yintercept = mean(my.data$diffp)) +
  geom_hline(yintercept = mean(my.data$diffp) + 2*sd.diffp, linetype = 2) +
  geom_hline(yintercept = mean(my.data$diffp) - 2*sd.diffp, linetype = 2) +
  ylab(&quot;Difference pre and post Storage (%)&quot;) +
  xlab(&quot;Baseline Concentration (mg/L)&quot;)


ggMarginal(diffplotp, type=&quot;histogram&quot;, bins = 25)</pre>
<p><img alt="plot of chunk unnamed-chunk-5" src="http://i0.wp.com/labrtorian.com/wp-content/uploads/2016/08/unnamed-chunk-5-1.png?w=604" data-recalc-dims="1"></p>
<h2>Kickin’ it Old School</h2>
<p>You can also do this in a non-<code>ggplot()</code> paradigm using base plotting utilities as described <a target="_blank" href="https://www.r-bloggers.com/example-8-41-scatterplot-with-marginal-histograms/">in this R-bloggers post.</a></p>
<h2>Conclusion</h2>
<p>And that, friends, is a way of squishing in a histogram of your sample concentrations into your difference plot which allows you to graphically display your sampling distribution and justify whether you would use parametric or non-parametric statistics to assess the extent of loss of immunoreactivity from storage.</p>
<p><strong>And speaking of scatterplots</strong></p>
<p>“…then the Lord your God will restore your fortunes and have compassion on you and gather you again from all the nations where he scattered you.”<br>
Deut 30:3</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/make-bland-altman-plots-with-marginal-histograms-using-ggextra/"></div></div><div><a data-url="https://www.r-bloggers.com/make-bland-altman-plots-with-marginal-histograms-using-ggextra/" data-counturl="https://www.r-bloggers.com/make-bland-altman-plots-with-marginal-histograms-using-ggextra/" data-count="vertical" data-text="Make Bland Altman Plots with Marginal Histograms using ggExtra" 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://labrtorian.com/2016/08/29/make-bland-altman-plots-with-marginal-histograms-using-ggextra/"> The Lab-R-torian</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/6VYJ-WOy9P8"></a></div>