<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/using-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/using-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>As noted elsewhere, sometimes <a rel="nofollow" href="https://www.statworx.com/blog/make-rstudio-look-the-way-you-want-because-beauty-matters/" target="_blank">beauty matters</a>. A plot that’s pleasing to the eye will be considered more<br>
gladly, and thus might be understood more thoroughly. Also, since we at <a rel="nofollow" href="https://www.statworx.com/de/" target="_blank">STATWORX</a> oftentimes need to<br>
subsume and communicate our results, we have come to appreciate how a nice plot can upgrade any presentation. </p>
<p>So how make a plot look good? How make it accord with given style guidelines? In ggplot2 the display of all non-data components is controlled by the theme system. Other than in some other packages, the appearance of plots is edited after all the data-related elements of the plot have been determined. The theme system of ggplot2 allows the manipulation of titles, labels, legends, grid lines and backgrounds. There are various build-in themes available that already have an all-around consistent style, pertaining to any detail of a plot. </p>
<h2>Pre-defined themes</h2>
<p>There are two ways to apply bulid-in (or otherwise predefined) themes (e.g. <code>theme_grey</code>, <code>theme_bw</code>, <code>theme_linedraw</code>, <code>theme_light</code>, <code>theme_dark</code>, <code>theme_minimal</code> or <code>theme_classic</code>).<br>
For one, they can be added as an additional layer to individual plots:</p>
<pre><code lang="python">rm(list = ls())
library(gridExtra)
library(ggplot2)

# generating a fictional data set containing hours of sunshine and temperature
sun_hours &lt;- sample(seq(from = 1, to = 8, by = 0.1), size = 40, replace = TRUE)
noise &lt;- sample(seq(from = 17, to = 24, by = 0.1), size = 40, replace = TRUE)
temperature &lt;-  sun_hours + noise
df_sun &lt;- data.frame(sun_hours, temperature)

# generate the plot base
base_plot &lt;- ggplot(df_sun) +
  geom_point(aes(x = sun_hours, y = temperature, color = temperature), 
             shape = 6, size = 5, stroke = 2) +
  geom_point(aes(x = sun_hours, y = temperature, color = temperature), 
             shape = 21, size = 3.3, fill = &quot;white&quot;, stroke = 2) +
  labs(x = &quot;Hours of Sun&quot;, y = &quot;Temperature&quot;) +
  scale_color_gradient(high = &quot;firebrick&quot;, low = &quot;#ffce00&quot;, name = &quot; &quot;) +
  ggtitle(&quot;Base Plot&quot;)
</code></pre>
<p><img alt="base-plot" src="https://i0.wp.com/www.statworx.com/wp-content/uploads/base-plot.jpg?w=450&ssl=1" class="wp-image-14837" data-recalc-dims="1"></p>
<pre><code lang="python"># adding predefined themes
p1 &lt;- base_plot +
  theme_classic() +
  ggtitle(&quot;Plot with theme_classic()&quot;)

p2 &lt;- base_plot +
  theme_bw() +
  ggtitle(&quot;Plot with theme_bw()&quot;)

p3 &lt;- base_plot +
  theme_dark() +
  ggtitle(&quot;Plot with theme_dark()&quot;)

p4 &lt;- base_plot +
  theme_light() +
  ggtitle(&quot;Plot with theme_light()&quot;)

gridExtra::grid.arrange(p1, p2, p3, p4)
</code></pre>
<p><img alt="different-themes" src="https://i0.wp.com/www.statworx.com/wp-content/uploads/different-themes.jpg?w=450&ssl=1" class="wp-image-14840" data-recalc-dims="1"></p>
<p>Alternatively, the default theme that’s automatically added to any plot, can be set or get with the functions <code>theme_set()</code> or <code>theme_get()</code>.</p>
<pre><code lang="python"># making the classic theme the default
theme_set(theme_classic())

base_plot +
  ggtitle(&quot;Plot with theme_set(theme_classic())&quot;)
</code></pre>
<p><img alt="theme-set" src="https://i2.wp.com/www.statworx.com/wp-content/uploads/theme-set.jpg?w=450&ssl=1" class="wp-image-14849" data-recalc-dims="1"></p>
<p>While predefined themes are very convenient, there’s always the option to (additionally) tweak the appearance of any non-data detail of a plot via the various arguments of <code>theme()</code>. This can be done for a specific plot, or the currently active default theme. The default theme can be updated or partly replaced via <code>theme_update</code> and <code>theme_replace</code>, respectively.</p>
<pre><code lang="python"># changing the default theme
theme_update(legend.position = &quot;none&quot;)

base_plot +
  ggtitle(&quot;Plot with theme_set(theme_classic()) \n&amp; theme_update(legend.position = \&quot;none\&quot;)&quot;)
</code></pre>
<p><img alt="theme-update" src="https://i0.wp.com/www.statworx.com/wp-content/uploads/theme-update.jpg?w=450&ssl=1" class="wp-image-14852" data-recalc-dims="1"></p>
<pre><code lang="python"># changing the theme directly applied to the plot
base_plot +
  theme(legend.position = &quot;bottom&quot;) +
  ggtitle(&quot;Plot with theme(legend.position = \&quot;bottom\&quot;)&quot;)
</code></pre>
<p><img alt="plus-theme" src="https://i0.wp.com/www.statworx.com/wp-content/uploads/plus-theme.jpg?w=450&ssl=1" class="wp-image-14846" data-recalc-dims="1"></p>
<h2>Element functions</h2>
<p>There’s a wide range of arguments for <code>theme()</code>, in fact such a wide range, that not all arguments can be discussed here. Therefore, this blog post is far from exhaustive and only deals with the general principles of the theme system and only provides some illustrative examples for a few of all the available arguments. The appearance of many elements needs to be specified via one of the four element functions: <code>element_blank</code>, <code>element_text</code>, <code>element_line</code> or <code>element_rect</code>.</p>
<ul>
<li>How labels and titles are displayed, is controlled by the <code>element_text</code> function. For example, we can make the title of the y axis bold and increase its size. </li>
<li>Borders and backgrounds can be manipulated using <code>element_rect</code>. For example, we can choose the color of the plot’s background. </li>
<li>Lines can be defined via the <code>element_line</code> function. For example, we can change the line types of the mayor and minor grid. </li>
<li>Further, with <code>element_blank()</code> it is possible to remove an object completely, without having any space dedicated to the plot element. </li>
</ul>
<pre><code lang="python"># using element_text, element_rect, element_line, element_blank
base_plot +
  theme(axis.title.y = element_text(face = &quot;bold&quot;, size = 16),
        plot.background = element_rect(fill = &quot;#FED633&quot;),
        panel.grid.major = element_line(linetype = &quot;dashed&quot;),
        panel.grid.minor = element_line(linetype = &quot;dotted&quot;),
        axis.text.y = element_blank(),
        axis.text.x = element_blank()) +
  ggtitle(&quot;Plot altered using element functions&quot;)
</code></pre>
<p><img alt="element-function" src="https://i1.wp.com/www.statworx.com/wp-content/uploads/element-function.jpg?w=450&ssl=1" class="wp-image-14843" data-recalc-dims="1"></p>
<p>If we don’t want to change the display of some specific plot elements, but of all text, lines, titles or rectangular elements we can do so by specifying the arguments <code>text</code>, <code>line</code>, <code>rect</code> and <code>title</code>. Specifications passed to these arguments are inherited by all elements of the respective type. This inheritance principle also holds true for other 'parent' arguments. 'Parent' arguments oftentimes are easily identifiable, as their names are used as prefixes for all subordinate arguments. </p>
<pre><code lang="python"># using overreaching arguments #1
base_plot +
  theme(line = element_line(linetype = &quot;dashed&quot;)) +
  ggtitle(&quot;Plot with all lines altered by using line&quot;)
</code></pre>
<p><img alt="using-line" src="https://i2.wp.com/www.statworx.com/wp-content/uploads/using-line.jpg?w=450&ssl=1" class="wp-image-14858" data-recalc-dims="1"></p>
<pre><code lang="python"># using overreaching arguments #2
base_plot +
  theme(axis.title = element_text(size = 6)) + # here axis.title is the parent
  ggtitle(&quot;Plot with both axis titles altered by using axis.title&quot;)
</code></pre>
<p><img alt="using-axistitle" src="https://i0.wp.com/www.statworx.com/wp-content/uploads/using-axistitle.jpg?w=450&ssl=1" class="wp-image-14855" data-recalc-dims="1"></p>
<h2>Outlook</h2>
<p>Margins, spaces, sizes and orientations of elements are not specified with element functions but have their own sets of possible parameters. For example, the display of legends is controlled by such arguments and specific parameters. </p>
<pre><code lang="python"># using parameters instead of element functions
base_plot +
  theme(legend.position = &quot;top&quot;) 
</code></pre>
<p><img alt="using-otherparameters" src="https://i2.wp.com/www.statworx.com/wp-content/uploads/using-otherparameters.jpg?w=450&ssl=1" class="wp-image-14861" data-recalc-dims="1"></p>
<p>Since ggplot2 enables to manipulate the appearance of non-data elements of plots in great detail, there is a multitude of arguments. This blog post only tries to give a first impression of the many, many possibilities to design a plot. Some additional occupation with the topic, might be advisable, but any time invested in understanding how to style plots, surely is well spent. If you want read more on making pretty plots in ggplot2, check out my other posts on <a rel="nofollow" href="https://www.statworx.com/blog/coordinate-systems-in-ggplot2-easily-overlooked-and-rather-underrated/" target="_blank">coordinate stystems</a> or <a rel="nofollow" href="https://www.statworx.com/blog/customizing-time-and-date-scales-in-ggplot2/" target="_blank">customizing date and time scales</a>.</p>
<p> </p>
<h2>Referenzen</h2>
<ul>
<li>Wickham, H. (2009). <em>ggplot2: elegant graphics for data analysis</em>. Springer.</li>
</ul>
<p> </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/using-themes-in-ggplot2/" target="_blank">Using 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/using-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/IX85wfMECGw" height="1"></a></div>