<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/styling-base-r-graphics/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="http://blog.jumpingrivers.com/posts/2018/2018-01-24-base-r-graphics/"> R on The Jumping Rivers Blog</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<div>
<ul>
<li><a rel="nofollow" href="http://blog.jumpingrivers.com/posts/2018/2018-01-24-base-r-graphics/#publication-quality-base-r-graphics" target="_blank">Publication quality base R graphics</a></li>
<li><a rel="nofollow" href="http://blog.jumpingrivers.com/posts/2018/2018-01-24-base-r-graphics/#fixing-the-problem" target="_blank">Fixing the problem</a></li>
<li><a rel="nofollow" href="http://blog.jumpingrivers.com/posts/2018/2018-01-24-base-r-graphics/#why-not-use-ggplot2-or-something-else" target="_blank">Why not use ggplot2 (or something else)?</a></li>
</ul>
</div>
<p><img src="https://i2.wp.com/blog.jumpingrivers.com/img/2018/baseR.gif?w=400" data-recalc-dims="1"></p>
<div>
<h2>Publication quality base R graphics</h2>
<p>Base R graphics get a bad press (although to be fair, they could have chosen their default values better). In general, they are viewed as a throw back to the dawn of the R era. I think that most people would agree that, in general, there are better graphics techniques in R (e.g. ggplot2). However it is occasionally worthwhile making a plot using base R graphics. For example, if you have a publication and you want to make sure the graphics are reproducible in five years.</p>
<p>In this post we’ll discuss methods for dramatically altering the look and feel of a base R plot. With a bit (ok, a lot) of effort, it is possible to change all aspects of the plot to your liking.</p>
<p>Typically I detest the <code>iris</code> the data set. It’s perhaps the most over used dataset in the entire R world. For this <strong>very</strong> reason, we’ll use it in this post to show what’s possible <img alt="😉" src="https://s.w.org/images/core/emoji/2.3/72x72/1f609.png" class="wp-smiley"></p>
<p>The standard base R scatter plot is</p>
<pre><code>plot(iris$Sepal.Length, iris$Sepal.Width, col = iris$Species)
legend(&quot;topright&quot;, legend = levels(iris$Species), col = 1:3, pch = 21)</code></pre>
<p><img width="450" src="http://blog.jumpingrivers.com/posts/2018/2018-01-24-base-r-graphics_files/figure-html/unnamed-chunk-2-1.svg"></p>
<p>This gives a simple scatter plot with associated legend using the default colour scheme. The list of things wrong with the this plot is fairly lengthy, but not limited to</p>
<ul>
<li>Colours</li>
<li>Margins</li>
<li>Axis labels</li>
<li>Overlapping points</li>
<li>Wasted space</li>
</ul>
<p>However with base R graphics we can fix all of these faults!</p>
</div>
<div>
<h2>Fixing the problem</h2>
<p>What’s not clear in the scatter plot above is that some points lie on top of each other. So the first step is to wiggle the points using the <code>jitter()</code> function to avoid points sitting on top of each other.</p>
<pre><code>## Same as geom_jitter
iris$Sepal.Length = jitter(iris$Sepal.Length)
iris$Sepal.Width = jitter(iris$Sepal.Width)</code></pre>
<p>Next we select nicer colours (I’ve taken this palette from the great <a rel="nofollow" href="http://tools.medialab.sciences-po.fr/iwanthue/" target="_blank">I want hue</a> website). The <code>palette()</code> function allows you to globally change the colour palette used by base R plots</p>
<pre><code>alpha = 150 # Transparent points
palette(c(rgb(200, 79, 178, alpha = alpha, maxColorValue = 255), 
          rgb(105, 147, 45, alpha = alpha, maxColorValue = 255),
          rgb(85, 130, 169, alpha = alpha, maxColorValue = 255)))</code></pre>
<p>Next we alter a few plot characteristics with the <code>par()</code> function</p>
<pre><code>par(mar = c(3, 3, 2, 1), # Dist' from plot to side of page
    mgp = c(2, 0.4, 0), # Dist' plot to label
    las = 1, # Rotate y-axis text
    tck = -.01, # Reduce tick length
    xaxs = &quot;i&quot;, yaxs = &quot;i&quot;) # Remove plot padding</code></pre>
<p>Then it comes to the <code>plot()</code> function itself. This has now become a <strong>lot</strong> more complicated. We create the plot using the <code>plot()</code> function, with a number of arguments</p>
<pre><code>plot(iris$Sepal.Length, iris$Sepal.Width, 
     bg = iris$Species, # Fill colour
     pch = 21, # Shape: circles that can filed
     xlab = &quot;Sepal Length&quot;, ylab = &quot;Sepal Width&quot;, # Labels
     axes = FALSE, # Don't plot the axes
     frame.plot = FALSE, # Remove the frame 
     xlim = c(4, 8), ylim = c(2, 4.5), # Limits
     panel.first = abline(h = seq(2, 4.5, 0.5), col = &quot;grey80&quot;))</code></pre>
<p>then add in the x-axis tick marks</p>
<pre><code>at = pretty(iris$Sepal.Length)
mtext(side = 1, text = at, at = at, 
      col = &quot;grey20&quot;, line = 1, cex = 0.9)</code></pre>
<p>and the y-axis</p>
<pre><code>at = pretty(iris$Sepal.Width)
mtext(side = 2, text = at, at = at, col = &quot;grey20&quot;, line = 1, cex = 0.9)</code></pre>
<p>This just leaves the legend. Instead of using the <code>legend()</code> function, we’ll place the names next to the points via the <code>text()</code> function</p>
<pre><code>text(5, 4.2, &quot;setosa&quot;, col = rgb(200, 79, 178, maxColorValue = 255))
text(5.3, 2.1, &quot;versicolor&quot;, col = rgb(105, 147, 45, maxColorValue = 255))
text(7, 3.7, &quot;virginica&quot;, col = rgb(85, 130, 169, maxColorValue = 255))</code></pre>
<p>Finally, we have the plot title</p>
<pre><code>title(&quot;The infamous IRIS data&quot;, adj = 1, 
      cex.main = 0.8, font.main = 2, col.main = &quot;black&quot;)</code></pre>
<p>Putting it all together gives</p>
<p><img width="450" src="http://blog.jumpingrivers.com/posts/2018/2018-01-24-base-r-graphics_files/figure-html/unnamed-chunk-11-1.svg"></p>
<p>A much better job.</p>
</div>
<div>
<h2>Why not use ggplot2 (or something else)?</h2>
<p>This seems like a lot of work to create a simple scatter plot. Why not use X, Y, or ggplot2? We even have a course on <a rel="nofollow" href="https://www.jumpingrivers.com/courses/4_advanced-graphics-with-r" target="_blank">ggplot2</a> so we’re not biased. The purpose of this article isn’t to get into a religious visualisation war on base R vs … However if you want such a war, have a look at the blog posts by <a rel="nofollow" href="http://flowingdata.com/2016/03/22/comparing-ggplot2-and-r-base-graphics/" target="_blank">Flowing Data</a>, <a rel="nofollow" href="https://simplystatistics.org/2016/02/11/why-i-dont-use-ggplot2/" target="_blank">Jeff Leek</a> and <a rel="nofollow" href="http://varianceexplained.org/r/why-I-use-ggplot2/" target="_blank">David Robinson</a>.</p>
<p>One point that is worth making is that since we are only using base R functions, our plot will almost certainly be reproducible for all future versions of R! Not something to quickly dismiss.</p>
<hr>
<p><a rel="nofollow" href="https://www.jumpingrivers.com/?utm_source=blog&utm_medium=image&utm_campaign=basegraphics2018" target="_blank"><img src="https://i1.wp.com/blog.jumpingrivers.com/img/logos/300_125.jpg?w=456" data-recalc-dims="1"></a></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="http://blog.jumpingrivers.com/posts/2018/2018-01-24-base-r-graphics/"> R on The Jumping Rivers Blog</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/wCanJowvXWs" height="1"></a></div>