<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/smoothscatter-with-ggplot2/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="https://www.inwt-statistics.com/read-blog/smoothscatter-with-ggplot2-513.html"> INWT-Blog-RBloggers</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<div>
<p>The motivation for this plot is the function: <em>graphics::smoothScatter,</em> basically a plot of a two dimensional density estimator. In the following I want to reproduce the features with <a rel="nofollow" href="https://cran.r-project.org/web/packages/ggplot2/index.html" target="_blank">ggplot2</a>.</p>
</div>
<div>
<h2>smoothScatter</h2>
<p>To have some data I draw some random numbers from a two dimensional normal distribution:</p>
</div>
<pre><pre><code>library(ggplot2) library(MASS) set.seed(2) dat &lt;- data.frame(   mvrnorm(n = 1000,            mu = c(0, 0),            Sigma = matrix(rep(c(1, 0.2), 2), nrow = 2, ncol = 2))) names(dat) &lt;- c(&quot;x&quot;, &quot;y&quot;) </code></pre></pre>
<div>
<p><em>smoothScatter</em> is basically a scatter plot with a two dimensional density estimation. This is nice especially in the case of a lot of observations and for outlier detection.</p>
</div>
<pre><pre><code>par(mfrow = c(1,2)) plot(dat$x, dat$y) smoothScatter(dat$x, dat$y) </code></pre></pre>
<div>
<p><img alt="" src="https://i1.wp.com/www.inwt-statistics.com/files/INWT/images_blog/smoothScatter/scatterBase.png?w=450&ssl=1" data-recalc-dims="1"></p>
</div>
<div>
<h2>smoothScatter in ggplot2</h2>
<p>OK, very pretty, lets reproduce this feature in ggplot2. First thing is to add the necessary layers, which I already mentioned is a two dimensional density estimation, combined with the <em>geom</em> called ‘tile’. Also I use the fill aesthetic to add colour and a different palette:</p>
</div>
<pre><pre><code>ggplot(data = dat, aes(x, y)) +   stat_density2d(aes(fill = ..density..^0.25), geom = &quot;tile&quot;, contour = FALSE, n = 200) +   scale_fill_continuous(low = &quot;white&quot;, high = &quot;dodgerblue4&quot;)</code></pre></pre>
<div>
<p><img alt="" src="https://i2.wp.com/www.inwt-statistics.com/files/INWT/images_blog/smoothScatter/scatterGgplot2.png?w=450&ssl=1" data-recalc-dims="1"></p>
</div>
<div>
<p>I add one additional layer; a simple scatter plot. To make the points transparent I choose alpha to be <em>1/10</em> which is a relative quantity with respect to the number of observations.</p>
</div>
<pre><pre><code>last_plot() +   geom_point(alpha = 0.1, shape = 20)</code></pre></pre>
<div>
<p><img alt="" src="https://i0.wp.com/www.inwt-statistics.com/files/INWT/images_blog/smoothScatter/scatterNewLayer.png?w=450&ssl=1" data-recalc-dims="1"></p>
</div>
<div>
<p>A similar approach is also discussed on <a rel="nofollow" href="https://stackoverflow.com/questions/13094827/how-to-reproduce-smoothscatters-outlier-plotting-in-ggplot" target="_blank">StackOverflow</a>. Actually that version is closer to <em>smoothScatter</em>.</p>
</div>
<div>
<h2>Changing the theme</h2>
<p>The last step is to tweak the theme-elements. Not that the following adds to any form of information but it looks nice. Starting from a standard theme, <em>theme_classic</em>, which is close to where I want to get, I get rid of all labels, axis and the legend.</p>
</div>
<pre><pre><code>last_plot() +   theme_classic() +   theme(     legend.position = &quot;none&quot;,      axis.line = element_blank(),      axis.ticks = element_blank(),      axis.text = element_blank(),      text = element_blank(),      plot.margin = unit(c(-1, -1, -1, -1), &quot;cm&quot;)   ) </code></pre></pre>
<div>
<p><img alt="" src="https://i1.wp.com/www.inwt-statistics.com/files/INWT/images_blog/smoothScatter/scatterClassic.png?w=450&ssl=1" data-recalc-dims="1"></p>
</div>
<div>
<p>The last thing is to save the plot in the correct format for display:</p>
</div>
<pre><pre><code>ggsave(   &quot;scatterFinal.png&quot;,    plot = last_plot(),    width = 14,    height = 8,    dpi = 300,    units = &quot;in&quot; ) </code></pre></pre>
<div>
<p>And that’s it, a nice picture which used to be a statistical graph.</p>
</div>
	<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://www.inwt-statistics.com/read-blog/smoothscatter-with-ggplot2-513.html"> INWT-Blog-RBloggers</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/CE-Ne8TsL4w" height="1"></a></div>