<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/data-visuals-notes-for-my-talks-in-2017/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="http://www.seascapemodels.org/rstats/2017/06/26/dataviz2017_notes.html"> Bluecology blog</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<h2>Data visuals: notes for my talks in 2017</h2>
<p>Supplementary notes for CJ Brown’s talks on dataviz in 2017 for Griffith<br>
University’s honours students and the <a rel="nofollow" href="http://bioinformatics.org.au/ws17/" target="_blank">UQ Winterschool in<br>
Bioinformatics</a>.</p>
<h3><a rel="nofollow" href="http://www.seascapemodels.org/rstats/2017/06/26/dataviz2017_notes.html#interpreting-rates" target="_blank">Skip to the quiz</a></h3>
<h3>Visualsing sexual dimorphism in elephant seals</h3>
<p>I picked this example to demonstrate a simple barchart for representing<br>
the sizes of different things.</p>
<div>
<pre><code>mnsize &lt;- c(2800, 650)
sesize &lt;- c(800, 50)
nams &lt;- c('Male', 'Female')
cols &lt;- c('steelblue', 'sienna3')

par(oma = c(1,2,1,1), mgp = c(6,1,0), mar = c(5,8,4,2))
barplot(mnsize, names.arg = '', las = 1, ylab = 'Weight (kg)', cex.axis = 2, cex.lab =2, col = cols)

legend('topright', legend = nams, pch = 15, col = cols, cex = 2)
</code></pre>
</div>
<p><img alt="" src="https://i0.wp.com/www.seascapemodels.org/Images/dataviz2017_notes_files/figure-markdown_strict/unnamed-chunk-1-1.png?w=456" data-recalc-dims="1"></p>
<h2>Comparing volume and area</h2>
<p>Compare these. Note that if we compare circles we should use area, not<br>
the radius or diameter to scale their size.</p>
<div>
<pre><code>n &lt;- c(10, 5)
barplot(n, col = 'skyblue', xaxt = 'n', yaxt = 'n')
</code></pre>
</div>
<p><img alt="" src="https://i1.wp.com/www.seascapemodels.org/Images/dataviz2017_notes_files/figure-markdown_strict/unnamed-chunk-2-1.png?w=456" data-recalc-dims="1"></p>
<div>
<pre><code>rad1 &lt;- 1
area1 &lt;- pi*(rad1^2)
area2 &lt;- area1/2
rad2 &lt;- sqrt(area2/pi)

par(mfrow = c(1,2), mar = c(0,0,0,0))
pie(1, col = 'skyblue', labels = NA, border = NA, radius = rad1)
pie(1, col = 'skyblue', labels = NA, border = NA, radius = rad2)
</code></pre>
</div>
<p><img alt="" src="https://i0.wp.com/www.seascapemodels.org/Images/dataviz2017_notes_files/figure-markdown_strict/unnamed-chunk-2-2.png?w=456" data-recalc-dims="1"></p>
<h3>Exploration of data</h3>
<p>Let’s create a point cloud to demonstrate some data exploration<br>
techniques</p>
<div>
<pre><code>set.seed(42)
x &lt;- rnorm(1000)
y &lt;- 5*x + 3 + rnorm(1000, sd = 15)
plot(x,y, pch = 16, col = grey(0.5,0.5), las = 1)
</code></pre>
</div>
<p><img alt="" src="https://i1.wp.com/www.seascapemodels.org/Images/dataviz2017_notes_files/figure-markdown_strict/unnamed-chunk-3-1.png?w=456" data-recalc-dims="1"></p>
<p>Can’t see alot here. A linear model might help us explore if there is<br>
any trend going on:</p>
<div>
<pre><code>mod1 &lt;- lm(y ~ x)
plot(x,y, pch = 16, col = grey(0.5,0.5), las = 1)
abline(mod1, lwd = 2, col = 'red')

xnew &lt;- seq(min(x), max(x), length.out = 100)
pmod &lt;- predict(mod1, newdata =data.frame(x=xnew),  se = T)
lines(xnew, pmod$fit + pmod$se.fit, lwd = 2, col = 'red', lty = 2)
lines(xnew, pmod$fit - pmod$se.fit, lwd = 2, col = 'red', lty = 2)
</code></pre>
</div>
<p><img alt="" src="https://i1.wp.com/www.seascapemodels.org/Images/dataviz2017_notes_files/figure-markdown_strict/unnamed-chunk-4-1.png?w=456" data-recalc-dims="1"></p>
<p>What about identifying extreme points, that may be worth investigating<br>
further? We can pick out points that are greater than 2SDs from the<br>
trend:</p>
<div>
<pre><code>modresid &lt;- resid(mod1)
sd2 &lt;- sd(modresid)*2
ipt &lt;- which(abs(modresid) > sd2)

plot(x,y, pch = 16, col = grey(0.5,0.5), las = 1)
abline(mod1, lwd = 2, col = 'red')
points(x[ipt], y[ipt], pch = 16, col = rgb(1,0,0, 0.6))
</code></pre>
</div>
<p><img alt="" src="https://i1.wp.com/www.seascapemodels.org/Images/dataviz2017_notes_files/figure-markdown_strict/unnamed-chunk-5-1.png?w=456" data-recalc-dims="1"></p>
<h3>Three ways of visualising the same x-y data</h3>
<p>Each of these graphs of the same data has a slightly different<br>
interpretation.</p>
<div>
<pre><code>x &lt;- 1:100
y &lt;- x + rnorm(100, sd=30)
plot(x,y, pch = 16, col = grey(0.5, 0.5))
</code></pre>
</div>
<p><img alt="" src="https://i2.wp.com/www.seascapemodels.org/Images/dataviz2017_notes_files/figure-markdown_strict/unnamed-chunk-6-1.png?w=456" data-recalc-dims="1"></p>
<div>
<pre><code>mod1 &lt;- lm(y ~ x)
plot(x,y, col = 'white')
abline(mod1, lwd = 3, col = 'red')
</code></pre>
</div>
<p><img alt="" src="https://i1.wp.com/www.seascapemodels.org/Images/dataviz2017_notes_files/figure-markdown_strict/unnamed-chunk-6-2.png?w=456" data-recalc-dims="1"></p>
<div>
<pre><code>library(MASS)
filled.contour(kde2d(x,y), scale = F)
</code></pre>
</div>
<p><img alt="" src="https://i0.wp.com/www.seascapemodels.org/Images/dataviz2017_notes_files/figure-markdown_strict/unnamed-chunk-6-3.png?w=456" data-recalc-dims="1"></p>
<div>
<pre><code>plot(x,y, pch = 16, col = grey(0.5, 0.5))
abline(mod1, lwd = 3, col = 'red')
</code></pre>
</div>
<p><img alt="" src="https://i1.wp.com/www.seascapemodels.org/Images/dataviz2017_notes_files/figure-markdown_strict/unnamed-chunk-6-4.png?w=456" data-recalc-dims="1"></p>
<h3>Make your own Datasaurus dozen</h3>
<p>The datasaurus is a great example of why you should view your data,<br>
invented by Alberto Cairo. See Steph Locke’s code and package on<br>
<a rel="nofollow" href="https://github.com/stephlocke/datasauRus" target="_blank">github</a> for making this in R.</p>
<div>
<pre><code>library(datasauRus)
datnames &lt;- rev(unique(datasaurus_dozen$dataset))
nlevels &lt;- length(datnames)

for (i in 1:nlevels){
  i &lt;- which(datasaurus_dozen$dataset == datnames[i])
  plot(datasaurus_dozen$x[i], datasaurus_dozen$y[i],
     xlab = &quot;x&quot;, ylab = &quot;y&quot;,   las = 1)
  Sys.sleep(1)
}
</code></pre>
</div>
<p><img alt="" src="https://i0.wp.com/www.seascapemodels.org/Images/datasaurus_pause.gif?w=456" data-recalc-dims="1"></p>
<p>Convince yourself that the mean, sd and correlation is the same in all<br>
of these plots:</p>
<div>
<pre><code>library(dplyr)
datasaurus_dozen %>% group_by(dataset) %>%
    summarize(meanx = mean(x), meany = mean(y),
              sdx = sd(x), sdy = sd(y),
              corr = cor(x,y))

## # A tibble: 13 × 6
##       dataset    meanx    meany      sdx      sdy        corr
##         </code></pre></div>