<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/custom-themes-in-ggplot2/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="https://www.statworx.com/de/blog/custom-themes-in-ggplot2/"> r-bloggers – STATWORX</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<p>In the <a rel="nofollow" href="https://www.statworx.com/de/blog/using-themes-in-ggplot2/" target="_blank">last post</a> of this series of the <a rel="nofollow" href="https://www.statworx.com/de/" target="_blank">STATWORX Blog</a>, we explored ggplot2's themes, which control the display of all non-data components of a plot.</p>
<p>Ggplot comes with several inbuilt themes that can be easily applied to any plot. However, one can tweak these out-of-the-box styles using the <code>theme()</code> function. We did this last time. Furthermore, one also can create a complete customized theme, that's what we're going to do in this post. </p>
<h2>How the masters do it</h2>
<p>When we create a theme from scratch we have to define all arguments of the theme function and set the <code>complete</code> argument to <code>TRUE</code>, signaling that the generated object indeed is a complete theme. Setting <code>complete = TRUE</code> also causes all elements to inherit from blank elements. This means, that every object that we want to show up in our plots has to be defined in full detail. </p>
<p>If we are a little lazy, instead of defining each and every argument, we also can start with an existing theme and alter only some of its arguments. Actually, this is exactly what the creators of ggplot did. While <code>theme_gray()</code> is &quot;the mother of all themes&quot; and fully defined, for example <code>theme_bw()</code> builds upon <code>theme_gray()</code> , while <code>theme_minimal</code> in turn builds on <code>theme_bw()</code> .</p>
<p>We can see how tedious it is to define a complete theme, if we sneak a peak at the code of <code>theme_grey</code> on <a rel="nofollow" href="https://github.com/tidyverse/ggplot2/blob/master/R/theme-defaults.r" target="_blank">ggplot2's GitHub repository</a>. Further, it is obvious from the code of <code>theme_bw</code> or <code>theme_minimal</code> how much more convenient it is to create a new theme by building on an existing theme.</p>
<h2>Creating our very own theme</h2>
<p>What's good enough for Hadley and friends, is good enough for me. Therefore, I'm going to create my own theme based on my favourite theme, <code>theme_minimal()</code>. As can be seen on the GitHub repo, we can create a new theme as a function calling an existing theme, which is altered by <code>%+replace% theme()</code> with all alterations defined in <code>theme()</code>.</p>
<p>Several arguments are passed along to the function constituting a new theme and the existing theme called within the function: Specified are the default sizes for text (<code>base_size</code>), lines in general (<code>base_line_size</code>) as well as lines pertaining to rect-objects (<code>base_rect_size</code>), further defined is the font family. To ensure consitent look, all sizes aren't defined in absolute terms but relative to base sizes, using the <code>rel()</code> function. Therefore, for especially big or small plots the base sizes can be in- or decreased, with all other elements being adjusted automatically. </p>
<pre><code lang="python">library(ggplot2)
library(gridExtra)
library(dplyr)

# generating new theme
theme_new &lt;- function(base_size = 11,
                      base_family = &quot;&quot;,
                      base_line_size = base_size / 170,
                      base_rect_size = base_size / 170){
  theme_minimal(base_size = base_size, 
                base_family = base_family,
                base_line_size = base_line_size) %+replace%
    theme(
      plot.title = element_text(
        color = rgb(25, 43, 65, maxColorValue = 255), 
        face = &quot;bold&quot;,
        hjust = 0),
      axis.title = element_text(
        color = rgb(105, 105, 105, maxColorValue = 255),
        size = rel(0.75)),
      axis.text = element_text(
        color = rgb(105, 105, 105, maxColorValue = 255),
        size = rel(0.5)),
      panel.grid.major = element_line(
        rgb(105, 105, 105, maxColorValue = 255),
        linetype = &quot;dotted&quot;),   
      panel.grid.minor = element_line(
        rgb(105, 105, 105, maxColorValue = 255),
        linetype = &quot;dotted&quot;, 
        size = rel(4)),   
      
      complete = TRUE
    )
}
</code></pre>
<p>Other than in <code>theme_minimal()</code> I'm decreasing the base size to 11 and set the base line size and base rect size to base size devided by 170. I don't change the font family. The plot title is changed to a dark blue, bold font in the set base size and is left alinged. Axis text and axis ticks are set to have 75% and 50% of the base size, while their colour is changed to a light grey. Finally, the lines of the grid are defined to be dotted and light grey, with the major grid lines having the base line size and the minor grid lines having four times this size. </p>
<p>The result looks like this:</p>
<pre><code lang="python"># base plot
base_plot &lt;- data.frame(x = rnorm(n = 100, 1.5, 2),
                        y = rnorm(n = 100, 1, 2),
                        z = c(rnorm(n = 60, 0.5, 2), rnorm(n = 40, 5, 3))) %>%
  ggplot(.) +
  geom_jitter(aes(x = x, y = y, color = z, size = z), 
              alpha = 0.5) +
  geom_jitter(aes(x = x, y = y, size = z), 
              alpha = 0.8,
              shape = 21, 
              color = &quot;white&quot;,  
              stroke = 0.4) +
  scale_size_continuous(range = c(1, 18), breaks = c(1,  4, 5, 13, 18)) +
  guides(size = FALSE, color = FALSE) +
  labs(y = &quot;Flight Hight&quot;, x = &quot;Flight Distance&quot;)

# plot with customized theme
p1 &lt;- base_plot +
  ggtitle(&quot;Bubbels - theme_new()&quot;) +
  theme_new()

# plot with theme minimal
p2 &lt;- base_plot +
    ggtitle(&quot;Bubbels - theme_minimal()&quot;) +
    theme_minimal()

grid.arrange(p1, p2, nrow = 2)
</code></pre>
<p><img alt="" src="https://i2.wp.com/www.statworx.com/wp-content/uploads/customized-theme.png?w=450&ssl=1" class="wp-image-15140" data-recalc-dims="1"></p>
<p>For later use, we can save our theme gerenrating script and source our customized theme function whenever we want to make use of our created theme. </p>
<p>So go ahead, be creative and build your signature theme! </p>
<div>
<h6>Über den Autor</h6>
<p><img src="https://i0.wp.com/www.statworx.com/wp-content/uploads/statworx-avatar-female-300x300.png?resize=180%2C180&ssl=1" width="180" alt="Lea Waniek" class="wp-user-avatar wp-user-avatar-180" data-recalc-dims="1" height="180"></p>
<div>
<h4>Lea Waniek</h4>
<p>Lea ist Mitglied im Data Science Team und unterstützt ebenfalls im Bereich Statistik.</p>
</div>
</div>
<p>Der Beitrag <a rel="nofollow" href="https://www.statworx.com/de/blog/custom-themes-in-ggplot2/" target="_blank">Custom themes in ggplot2</a> erschien zuerst auf <a rel="nofollow" href="https://www.statworx.com/de/" target="_blank">STATWORX</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.statworx.com/de/blog/custom-themes-in-ggplot2/"> r-bloggers – STATWORX</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/1JOkCpgU0qQ" height="1"></a></div>