<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-ggplot2-graphics/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="https://www.jumpingrivers.com/blog/styling-ggplot2-r-graphics/"> r ? Jumping Rivers</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<h2>Styling ggplot2 graphics</h2>
<p>In my <a rel="nofollow" href="https://www.jumpingrivers.com/blog/styling-base-r-graphics/" target="_blank">previous post</a>, we demonstrated that contrary to popular opinion, it is possible to generate attractive looking plots using just base graphics. Although we did confess, that it did take a lot of time and effort. In this post, we repeat the same exercise. Using the dreaded <code>iris</code> data set, we'll first create the default ggplot2 graph, before applying a bit of care and attention.</p>
<h2>The standard ggplot version</h2>
<p>The standard scatter plot is straightforward to create. Load the package</p>
<pre><code>library(&quot;ggplot2&quot;)
</code></pre>
<p>Then create a scatter plot with the wonderful grey background</p>
<pre><code>## ggplot2 even spells colour correctly ;)
ggplot(iris, aes(x = Sepal.Length, y = Sepal.Width)) + 
  geom_point(aes(colour = Species))
</code></pre>
<p><img src="https://i2.wp.com/www.jumpingrivers.com/wp-content/uploads/2018/08/blog-2018-ggplot2-unnamed-chunk-3-1.png?w=456&ssl=1" data-recalc-dims="1"></p>
<p>Unlike the base R offering, the list of possible improvements to this plot is pleasingly short. Basically, it's</p>
<ul>
<li>the axis labels (but they come from <strong>our</strong> column headings)</li>
<li>colours (red &amp; blue aren't the best combination)</li>
</ul>
<p>So overall, pretty good. Other aspects that could be improved are</p>
<ul>
<li>grey background</li>
<li>direct labels on the points</li>
<li>starting the x-axis at 4, not 4.2</li>
</ul>
<h2>Styling the plot using only ggplot2</h2>
<p>Using only ggplot2 (and a little bit of <strong>dplyr</strong> love), we can improve significantly and easily improve the graph. First, we'll capitalise the legend key. I find it easier to manipulate the data directly,</p>
<pre><code>library(&quot;dplyr&quot;)
iris = mutate(iris, Species = stringr::str_to_title(Species))
</code></pre>
<p>With the data tweaked, we can get to the serious business of styling the plot. As the plot will contain a number of components it makes sense to create intermediate objects. As the points overlap, we’ll change from <code>geom_point()</code>, to <code>geom_jitter()</code>. This geom wiggles the points and allow us to see overlapping points:</p>
<pre><code>g = ggplot(iris, aes(x = Sepal.Length, y = Sepal.Width)) + 
  geom_jitter(aes(colour = Species)) + 
  xlab(&quot;Sepal length&quot;) + ylab(&quot;Sepal width&quot;) + # Improve axis labels
  ggtitle(&quot;The infamous Iris plot&quot;) # Title
g
</code></pre>
<p><img src="https://i1.wp.com/www.jumpingrivers.com/wp-content/uploads/2018/08/blog-2018-ggplot2-unnamed-chunk-5-1.png?w=456&ssl=1" data-recalc-dims="1"> The changes we've made so far would impossible for any package to do for us – how would the package know the plot title? We can now improve the look and feel of the plot. There are two ways of complementary ways of doing this: scales and themes. The ggplot scales control things like colours and point size. In the latest version of ggplot2, version 3.0.0, the Viridis colour palette was introduced. This palette is particularly useful for creating colour-blind friendly palettes</p>
<pre><code>g + scale_colour_viridis_d() # d for discrete
</code></pre>
<p>The theme controls elements such as grid lines, fonts, labels. I'm partial to <code>theme_minimal()</code></p>
<pre><code>g + scale_colour_viridis_d() + 
  theme_minimal()
</code></pre>
<p><img src="https://i1.wp.com/www.jumpingrivers.com/wp-content/uploads/2018/08/blog-2018-ggplot2-unnamed-chunk-6-1.png?w=456&ssl=1" data-recalc-dims="1"></p>
<h2>The hrbrthemes package</h2>
<p>We don't just have to use the themes that come with <strong>ggplot2</strong>, we can use themes provided by other packages. The <strong>hrbrthemes</strong> packages contain a nice theme called <code>ipsum</code> that’s similar to the minimal theme, but also tweaks the font and allows sub-headings. There is also an associated colour scheme called scale_colour_ipsum()`. An additional improvement we’ll make, is to drop the legend and place the text directly on the chart. After loading the package</p>
<pre><code>library(&quot;hrbrthemes&quot;)
</code></pre>
<p>we create a data frame with the label positions</p>
<pre><code>labels = data.frame(x = c(5, 5.3, 7), y = c(4.2, 2.1, 3.7), 
                    Species = c(&quot;Setosa&quot;, &quot;Versicolor&quot;, &quot;Virginica&quot;))
</code></pre>
<p>We construct the plot as usual</p>
<pre><code>ggplot(iris, aes(x = Sepal.Length, y = Sepal.Width)) + 
  geom_jitter(aes(colour = Species)) + 
  theme_ipsum() + 
  labs(x = &quot;Sepal length&quot;, y=&quot;Sepal width&quot;,
       title = &quot;The infamous Iris data set&quot;,
       subtitle = &quot;Thanks @hrbrmstr for the theme&quot;,
       caption = &quot;jumpingrivers.com&quot;) + 
  scale_colour_ipsum(guide = FALSE) + 
  geom_text(data = labels, aes(x, y, label = Species, colour = Species)) + 
  xlim(c(4, 8))
</code></pre>
<p><img src="https://i0.wp.com/www.jumpingrivers.com/wp-content/uploads/2018/08/blog-2018-ggplot2-unnamed-chunk-8-1.png?w=456&ssl=1" data-recalc-dims="1"></p>
<p>Notice we can add data from two data sets onto a ggplot with relative ease.</p>
<p>Thanks for reading, see you next time!</p>
<p><a rel="nofollow" href="https://www.jumpingrivers.com/?utm_source=blog&utm_medium=image&utm_campaign=2018-ggplot2" target="_blank"><img alt="" src="https://i1.wp.com/blog.jumpingrivers.com/img/logos/300_125.jpg?w=456&ssl=1" data-recalc-dims="1"></a></p>
<p>The post <a rel="nofollow" href="https://www.jumpingrivers.com/blog/styling-ggplot2-r-graphics/" target="_blank">Styling ggplot2 Graphics</a> appeared first on <a rel="nofollow" href="https://www.jumpingrivers.com/" target="_blank">Jumping Rivers</a>.</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="https://www.jumpingrivers.com/blog/styling-ggplot2-r-graphics/"> r ? Jumping Rivers</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/RzKt9XfXmS4" height="1"></a></div>