{"id":849,"date":"2015-04-07T10:42:33","date_gmt":"2015-04-07T17:42:33","guid":{"rendered":"http:\/\/homepages.uc.edu\/~yaozo\/wordpress\/?p=849"},"modified":"2015-04-07T10:42:33","modified_gmt":"2015-04-07T17:42:33","slug":"ggplot2-quick-reference-themes","status":"publish","type":"post","link":"https:\/\/zhuoyao.net\/index.php\/2015\/04\/07\/ggplot2-quick-reference-themes\/","title":{"rendered":"ggplot2 Quick Reference: Themes"},"content":{"rendered":"<p align=\"center\"><a href=\"http:\/\/sape.inf.usi.ch\/quick-reference\/ggplot2\/themes#theme_grey\"><img decoding=\"async\" title=\"theme_grey()\" src=\"http:\/\/sape.inf.usi.ch\/sites\/default\/files\/imagecache\/width180\/ggplot2-theme-grey.png\" alt=\"\" \/><\/a> <a href=\"http:\/\/sape.inf.usi.ch\/quick-reference\/ggplot2\/themes#theme_bw\"><img decoding=\"async\" title=\"theme_bw()\" src=\"http:\/\/sape.inf.usi.ch\/sites\/default\/files\/imagecache\/width180\/ggplot2-theme-bw.png\" alt=\"\" \/><\/a> <a href=\"http:\/\/sape.inf.usi.ch\/quick-reference\/ggplot2\/themes#theme_invisible\"><img decoding=\"async\" title=\"build your own theme - invisible\" src=\"http:\/\/sape.inf.usi.ch\/sites\/default\/files\/imagecache\/width180\/ggplot2-theme-invisible.png\" alt=\"\" \/><\/a> <a href=\"http:\/\/sape.inf.usi.ch\/quick-reference\/ggplot2\/themes#theme_complete_bw\"><img decoding=\"async\" title=\"build your own theme - complete_bw\" src=\"http:\/\/sape.inf.usi.ch\/sites\/default\/files\/imagecache\/width180\/ggplot2-theme-complete_bw.png\" alt=\"\" \/><\/a><\/p>\n<p>A plot can be themed by adding a theme. ggplot2 provides two built-in themes:<\/p>\n<ul>\n<li>theme_grey() &#8211; the default theme, with a grey background<\/li>\n<li>theme_bw() &#8211; a theme with a white background<\/li>\n<\/ul>\n<p>To be more precise, ggplot2 provides functions that <em>create<\/em> a theme. These functions can be used to add a specific theme to a plot:<\/p>\n<div class=\"geshifilter\">\n<pre class=\"rsplus geshifilter-rsplus\">ggplot() + ... + theme_bw()<\/pre>\n<\/div>\n<p>The theme produced by such a function is simply a structure containing a list of options. These options describe the visual properties of the axes, legends, panels, strips, and the overall plot. Note that these options define everything <em>but<\/em> the layers contained in the plot. If you create a layer to draw red dots and blue lines, the theme will not change these settings.<\/p>\n<p>When adding a theme to a plot, you can override some of the theme&#8217;s options with <span class=\"geshifilter\"><code class=\"rsplus geshifilter-rsplus\">opts(...)<\/code><\/span>. The example below uses<span class=\"geshifilter\"><code class=\"rsplus geshifilter-rsplus\">theme_grey()<\/code><\/span> for the plot, but overrides <span class=\"geshifilter\"><code class=\"rsplus geshifilter-rsplus\">legend.background<\/code><\/span> to fill the legend&#8217;s space with a very light grey (<span class=\"geshifilter\"><code class=\"rsplus geshifilter-rsplus\">theme_grey<\/code><\/span> sets the legend fill to white otherwise).<\/p>\n<div class=\"geshifilter\">\n<pre class=\"rsplus geshifilter-rsplus\">ggplot() + ... + theme_grey() + opts(legend.background = theme_rect(fill=\"grey95\", colour=NA))<\/pre>\n<\/div>\n<p>When using <span class=\"geshifilter\"><code class=\"rsplus geshifilter-rsplus\">opts()<\/code><\/span> <em>and<\/em> adding a theme (like in the command right above), make sure that you add the theme <em>before<\/em>overriding the options (otherwise the <span class=\"geshifilter\"><code class=\"rsplus geshifilter-rsplus\">opts(...)<\/code><\/span> has no effect).<\/p>\n<h3>The Currently Selected Theme<\/h3>\n<p>If you are not explicitly adding a theme to a plot, ggplot2 uses the currently selected theme. At startup, ggplot2 selects<span class=\"geshifilter\"><code class=\"rsplus geshifilter-rsplus\">theme_grey()<\/code><\/span>. You can change the current selection with <span class=\"geshifilter\"><code class=\"rsplus geshifilter-rsplus\">theme_set(...)<\/code><\/span>, you can access the currently selected theme with<span class=\"geshifilter\"><code class=\"rsplus geshifilter-rsplus\">theme_get()<\/code><\/span>, and you can even modify the properties of the currently selected theme with <span class=\"geshifilter\"><code class=\"rsplus geshifilter-rsplus\">theme_update(...)<\/code><\/span>.<\/p>\n<h3><a name=\"theme_grey\"><\/a>theme_grey()<\/h3>\n<p>The <span class=\"geshifilter\"><code class=\"rsplus geshifilter-rsplus\">theme_grey()<\/code><\/span> function creates the default theme of ggplot2. Here is an example plot using this theme:<\/p>\n<p><img decoding=\"async\" src=\"http:\/\/sape.inf.usi.ch\/sites\/default\/files\/ggplot2-theme-grey.png\" alt=\"\" \/><\/p>\n<p>Here is its definition, straight from ggplot2&#8217;s source code:<\/p>\n<div class=\"geshifilter\">\n<pre class=\"rsplus geshifilter-rsplus\">theme_gray &lt;- function(base_size = 12) {\n  structure(list(\n    axis.line =         theme_blank(),\n    axis.text.x =       theme_text(size = base_size * 0.8 , lineheight = 0.9, colour = \"grey50\", vjust = 1),\n    axis.text.y =       theme_text(size = base_size * 0.8, lineheight = 0.9, colour = \"grey50\", hjust = 1),\n    axis.ticks =        theme_segment(colour = \"grey50\"),\n    axis.title.x =      theme_text(size = base_size, vjust = 0.5),\n    axis.title.y =      theme_text(size = base_size, angle = 90, vjust = 0.5),\n    axis.ticks.length = unit(0.15, \"cm\"),\n    axis.ticks.margin = unit(0.1, \"cm\"),\n\u00a0\n    legend.background = theme_rect(colour=\"white\"), \n    legend.key =        theme_rect(fill = \"grey95\", colour = \"white\"),\n    legend.key.size =   unit(1.2, \"lines\"),\n    legend.text =       theme_text(size = base_size * 0.8),\n    legend.title =      theme_text(size = base_size * 0.8, face = \"bold\", hjust = 0),\n    legend.position =   \"right\",\n\u00a0\n    panel.background =  theme_rect(fill = \"grey90\", colour = NA), \n    panel.border =      theme_blank(), \n    panel.grid.major =  theme_line(colour = \"white\"),\n    panel.grid.minor =  theme_line(colour = \"grey95\", size = 0.25),\n    panel.margin =      unit(0.25, \"lines\"),\n\u00a0\n    strip.background =  theme_rect(fill = \"grey80\", colour = NA), \n    strip.text.x =      theme_text(size = base_size * 0.8),\n    strip.text.y =      theme_text(size = base_size * 0.8, angle = -90),\n\u00a0\n    plot.background =   theme_rect(colour = NA, fill = \"white\"),\n    plot.title =        theme_text(size = base_size * 1.2),\n    plot.margin =       unit(c(1, 1, 0.5, 0.5), \"lines\")\n  ), class = \"options\")\n}<\/pre>\n<\/div>\n<h3><a name=\"theme_bw\"><\/a>theme_bw()<\/h3>\n<p>The <span class=\"geshifilter\"><code class=\"rsplus geshifilter-rsplus\">theme_bw()<\/code><\/span> function produces a mostly (but not entirely) black-and-white theme.<\/p>\n<p><img decoding=\"async\" src=\"http:\/\/sape.inf.usi.ch\/sites\/default\/files\/ggplot2-theme-bw.png\" alt=\"\" \/><\/p>\n<p>This is the definition of <span class=\"geshifilter\"><code class=\"rsplus geshifilter-rsplus\">theme_bw()<\/code><\/span> straight from ggplot2&#8217;s source code:<\/p>\n<div class=\"geshifilter\">\n<pre class=\"rsplus geshifilter-rsplus\">theme_bw &lt;- function(base_size = 12) {\n  structure(list(\n    axis.line =         theme_blank(),\n    axis.text.x =       theme_text(size = base_size * 0.8 , lineheight = 0.9, vjust = 1),\n    axis.text.y =       theme_text(size = base_size * 0.8, lineheight = 0.9, hjust = 1),\n    axis.ticks =        theme_segment(colour = \"black\", size = 0.2),\n    axis.title.x =      theme_text(size = base_size, vjust = 1),\n    axis.title.y =      theme_text(size = base_size, angle = 90, vjust = 0.5),\n    axis.ticks.length = unit(0.3, \"lines\"),\n    axis.ticks.margin = unit(0.5, \"lines\"),\n\u00a0\n    legend.background = theme_rect(colour=NA), \n    legend.key =        theme_rect(colour = \"grey80\"),\n    legend.key.size =   unit(1.2, \"lines\"),\n    legend.text =       theme_text(size = base_size * 0.8),\n    legend.title =      theme_text(size = base_size * 0.8, face = \"bold\", hjust = 0),\n    legend.position =   \"right\",\n\u00a0\n    panel.background =  theme_rect(fill = \"white\", colour = NA), \n    panel.border =      theme_rect(fill = NA, colour=\"grey50\"), \n    panel.grid.major =  theme_line(colour = \"grey90\", size = 0.2),\n    panel.grid.minor =  theme_line(colour = \"grey98\", size = 0.5),\n    panel.margin =      unit(0.25, \"lines\"),\n\u00a0\n    strip.background =  theme_rect(fill = \"grey80\", colour = \"grey50\"), \n    strip.text.x =      theme_text(size = base_size * 0.8),\n    strip.text.y =      theme_text(size = base_size * 0.8, angle = -90),\n\u00a0\n    plot.background =   theme_rect(colour = NA),\n    plot.title =        theme_text(size = base_size * 1.2),\n    plot.margin =       unit(c(1, 1, 0.5, 0.5), \"lines\")\n  ), class = \"options\")\n}<\/pre>\n<\/div>\n<h3><a name=\"theme_invisible\"><\/a>Creating Your Own Theme<\/h3>\n<p>You can easily create your own theme by defining such a function on your own, and then adding it to your plot with <span class=\"geshifilter\"><code class=\"rsplus geshifilter-rsplus\">ggplot()+ ... + myOwnTheme()<\/code><\/span>. Here is a theme where all elements have <span class=\"geshifilter\"><code class=\"rsplus geshifilter-rsplus\">color=NA<\/code><\/span> and thus are invisible.<\/p>\n<p><img decoding=\"async\" src=\"http:\/\/sape.inf.usi.ch\/sites\/default\/files\/ggplot2-theme-invisible.png\" alt=\"\" \/><\/p>\n<p>And here is the function creating that theme:<\/p>\n<div class=\"geshifilter\">\n<pre class=\"rsplus geshifilter-rsplus\">theme_invisible &lt;- function(base_size = 12) {\n  structure(list(\n    axis.line =         theme_blank(),\n    axis.text.x =       theme_text(colour = NA,size = base_size * 0.8 , lineheight = 0.9, vjust = 1),\n    axis.text.y =       theme_text(colour = NA,size = base_size * 0.8, lineheight = 0.9, hjust = 1),\n    axis.ticks =        theme_segment(colour = NA, size = 0.2),\n    axis.title.x =      theme_text(colour = NA,size = base_size, vjust = 1),\n    axis.title.y =      theme_text(colour = NA,size = base_size, angle = 90, vjust = 0.5),\n    axis.ticks.length = unit(0.3, \"lines\"),\n    axis.ticks.margin = unit(0.5, \"lines\"),\n\u00a0\n    legend.background = theme_rect(colour=NA), \n    legend.key =        theme_rect(colour = NA, ),\n    legend.key.size =   unit(1.2, \"lines\"),\n    legend.text =       theme_text(colour = NA,size = base_size * 0.8),\n    legend.title =      theme_text(colour = NA,size = base_size * 0.8, face = \"bold\", hjust = 0),\n    legend.position =   \"right\",\n\u00a0\n    panel.background =  theme_rect(fill = NA, colour = NA), \n    panel.border =      theme_rect(fill = NA, colour=NA), \n    panel.grid.major =  theme_line(colour = NA, size = 0.2),\n    panel.grid.minor =  theme_line(colour = NA, size = 0.5),\n    panel.margin =      unit(0.25, \"lines\"),\n\u00a0\n    strip.background =  theme_rect(fill = NA, colour = NA), \n    strip.text.x =      theme_text(colour = NA,size = base_size * 0.8),\n    strip.text.y =      theme_text(colour = NA,size = base_size * 0.8, angle = -90),\n\u00a0\n    plot.background =   theme_rect(colour = NA),\n    plot.title =        theme_text(colour = NA,size = base_size * 1.2),\n    plot.margin =       unit(c(1, 1, 0.5, 0.5), \"lines\")\n  ), class = \"options\")\n}<\/pre>\n<\/div>\n<h3><a name=\"theme_complete_bw\"><\/a>A Truly Black-And-White Theme<\/h3>\n<p>The following theme uses only black and white (unlike <span class=\"geshifilter\"><code class=\"rsplus geshifilter-rsplus\">theme_bw()<\/code><\/span>, which also uses various levels of grey). Only using black and white may be useful when producing plots for black-and-white publications.<\/p>\n<p><img decoding=\"async\" src=\"http:\/\/sape.inf.usi.ch\/sites\/default\/files\/ggplot2-theme-complete_bw.png\" alt=\"\" \/><\/p>\n<p>Here is the corresponding theme function:<\/p>\n<div class=\"geshifilter\">\n<pre class=\"rsplus geshifilter-rsplus\">theme_complete_bw &lt;- function(base_size = 12) {\n  structure(list(\n    axis.line =         theme_blank(),\n    axis.text.x =       theme_text(size = base_size * 0.8 , lineheight = 0.9, colour = \"black\", vjust = 1),\n    axis.text.y =       theme_text(size = base_size * 0.8, lineheight = 0.9, colour = \"black\", hjust = 1),\n    axis.ticks =        theme_segment(colour = \"black\"),\n    axis.title.x =      theme_text(size = base_size, vjust = 0.5),\n    axis.title.y =      theme_text(size = base_size, angle = 90, vjust = 0.5),\n    axis.ticks.length = unit(0.15, \"cm\"),\n    axis.ticks.margin = unit(0.1, \"cm\"),\n\u00a0\n    legend.background = theme_rect(colour=NA), \n    legend.key =        theme_rect(fill = NA, colour = \"black\", size = 0.25),\n    legend.key.size =   unit(1.2, \"lines\"),\n    legend.text =       theme_text(size = base_size * 0.8),\n    legend.title =      theme_text(size = base_size * 0.8, face = \"bold\", hjust = 0),\n    legend.position =   \"right\",\n\u00a0\n    panel.background =  theme_rect(fill = NA, colour = \"black\", size = 0.25), \n    panel.border =      theme_blank(),\n    panel.grid.major =  theme_line(colour = \"black\", size = 0.05),\n    panel.grid.minor =  theme_line(colour = \"black\", size = 0.05),\n    panel.margin =      unit(0.25, \"lines\"),\n\u00a0\n    strip.background =  theme_rect(fill = NA, colour = NA), \n    strip.text.x =      theme_text(colour = \"black\", size = base_size * 0.8),\n    strip.text.y =      theme_text(colour = \"black\", size = base_size * 0.8, angle = -90),\n\u00a0\n    plot.background =   theme_rect(colour = NA, fill = \"white\"),\n    plot.title =        theme_text(size = base_size * 1.2),\n    plot.margin =       unit(c(1, 1, 0.5, 0.5), \"lines\")\n  ), class = \"options\")\n}<\/pre>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>A plot can be themed by adding a theme. ggplot2 provides two built-in themes: theme_grey() &#8211; the default theme, with a grey background theme_bw() &#8211;&hellip; <\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[20],"tags":[],"class_list":["post-849","post","type-post","status-publish","format-standard","hentry","category-r"],"_links":{"self":[{"href":"https:\/\/zhuoyao.net\/index.php\/wp-json\/wp\/v2\/posts\/849","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/zhuoyao.net\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/zhuoyao.net\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/zhuoyao.net\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/zhuoyao.net\/index.php\/wp-json\/wp\/v2\/comments?post=849"}],"version-history":[{"count":0,"href":"https:\/\/zhuoyao.net\/index.php\/wp-json\/wp\/v2\/posts\/849\/revisions"}],"wp:attachment":[{"href":"https:\/\/zhuoyao.net\/index.php\/wp-json\/wp\/v2\/media?parent=849"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zhuoyao.net\/index.php\/wp-json\/wp\/v2\/categories?post=849"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zhuoyao.net\/index.php\/wp-json\/wp\/v2\/tags?post=849"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}