{"id":2097,"date":"2018-01-01T16:37:39","date_gmt":"2018-01-01T16:37:39","guid":{"rendered":"http:\/\/pcool.dyndns.org:8080\/statsbook\/?page_id=2097"},"modified":"2025-06-22T08:45:13","modified_gmt":"2025-06-22T07:45:13","slug":"importing-fonts-setting-themes-and-defining-colours","status":"publish","type":"page","link":"https:\/\/pcool.dyndns.org\/index.php\/importing-fonts-setting-themes-and-defining-colours\/","title":{"rendered":"Fonts, Themes and Colours"},"content":{"rendered":"\n<p>The ggplot2 library<sup class='sup-ref-note' id='note-zotero-ref-p2097-r1-o1'><a class='sup-ref-note' href='#zotero-ref-p2097-r1'>1<\/a><\/sup> is very versatile and allows the creation of high quality plots. In this section, it is shown how to import a custom font (ie Google), setting a theme for all subsequent plots and define custom colours \/ palettes.<\/p>\n\n\n\n<p><strong>Importing a custom font<\/strong><\/p>\n\n\n\n<p><a href=\"https:\/\/fonts.google.com\/\" target=\"_blank\" rel=\"noopener\">Google<\/a> has a large collection of fonts that is available for download. For example the Roboto fonts (standard, condensed, slab and monospaced) can be <a href=\"https:\/\/fonts.google.com\/?query=Roboto\" target=\"_blank\" rel=\"noopener\">downloaded<\/a>. Once downloaded, extract the zip file and save it to the fonts directory on you hard drive (Mac OSX: ~\/Library\/Fonts\/). In Windows, right click on the ttf file (true type font) and select install.<\/p>\n\n\n\n<p class=\"is-style-text-annotation is-style-text-annotation--1\"><em>The ~ (tilde) represents your home directory<\/em><\/p>\n\n\n\n<p>Once the font is installed on your system, R will need to know about it and import it to its database. This can be done with the <a href=\"https:\/\/cran.r-project.org\/package=extrafont\" target=\"_blank\" rel=\"noopener\">extrafont package<\/a><sup class='sup-ref-note' id='note-zotero-ref-p2097-r2-o1'><a class='sup-ref-note' href='#zotero-ref-p2097-r2'>2<\/a><\/sup> available from the CRAN website. To install the package and load it:<\/p>\n\n\n\n<pre class=\"wp-block-code has-small-font-size\"><code><span style=\"color: #ff0000;\"><em>install.packages(\"extrafont\")<\/em><\/span>\n<span style=\"color: #ff0000;\"><em>library(extrafont)<\/em><\/span>\n<span style=\"color: #0000ff;\"><em>Registering fonts with R<\/em><\/span><\/code><\/pre>\n\n\n\n<p>To import all available fonts into R&#8217;s font database:<\/p>\n\n\n\n<pre class=\"wp-block-code has-small-font-size\"><code><span style=\"color: #ff0000;\"><em>font_import()<\/em><\/span><\/code><\/pre>\n\n\n\n<p>but this may take a while&#8230;&#8230;<\/p>\n\n\n\n<p>To import a single font, specify the path to the font library (ie Mac OSX):<\/p>\n\n\n\n<pre class=\"wp-block-code has-small-font-size\"><code><em><span style=\"color: #ff0000;\">font_import(paths = \"~\/Library\/Fonts\", pattern = \"Roboto\")<\/span><\/em><\/code><\/pre>\n\n\n\n<p>To show the available \/ installed fonts:<\/p>\n\n\n\n<pre class=\"wp-block-code has-small-font-size\"><code><span style=\"color: #ff0000;\"><em>fonttable()<\/em><\/span><\/code><\/pre>\n\n\n\n<p><strong>Setting a custom theme<\/strong><\/p>\n\n\n\n<p>The ggplot2 library allows customisation of themes to create plots with similar layout. To define a custom theme (paul_theme):<\/p>\n\n\n\n<pre class=\"wp-block-code has-small-font-size\"><code><span style=\"color: #ff0000;\"><em>library(ggplot2)<\/em><\/span>\n<em><span style=\"color: #ff0000;\">paul_theme &lt;- function(base_size = 14, base_family = \"Roboto\"){<\/span><\/em>\n  <em><span style=\"color: #ff0000;\">theme_minimal(base_size = base_size, base_family = base_family) +<\/span><\/em>\n  <em><span style=\"color: #ff0000;\">theme(<\/span><\/em>\n    <em><span style=\"color: #ff0000;\">axis.text = element_text(size=8),<\/span><\/em>\n    <em><span style=\"color: #ff0000;\">axis.text.x = element_text(angle = 0, vjust = 0.5, hjust = 0.5),<\/span><\/em>\n    <em><span style=\"color: #ff0000;\">axis.title = element_text(size =8),<\/span><\/em>\n    <em><span style=\"color: #ff0000;\">panel.grid.major = element_line(color = \"gray\"),<\/span><\/em>\n    <em><span style=\"color: #ff0000;\">panel.grid.minor = element_blank(),<\/span><\/em>\n    <em><span style=\"color: #ff0000;\">panel.background = element_rect(fill = \"#ffffff\"),<\/span><\/em>\n    <em><span style=\"color: #ff0000;\">strip.background = element_rect(fill = \"black\", color = \"black\", <\/span><\/em>\n      <em><span style=\"color: #ff0000;\">linewidth = 0.5),<\/span><\/em>\n    <em><span style=\"color: #ff0000;\">strip.text = element_text(face = \"bold\", size = 8, color = <\/span><\/em>\n      <em><span style=\"color: #ff0000;\">\"white\"),<\/span><\/em>\n    <em><span style=\"color: #ff0000;\">legend.position = \"bottom\",<\/span><\/em>\n    <em><span style=\"color: #ff0000;\">legend.justification = \"center\",<\/span><\/em>\n    <em><span style=\"color: #ff0000;\">legend.background = element_blank(),<\/span><\/em>\n    <em><span style=\"color: #ff0000;\">panel.border = element_rect(color = \"grey5\", fill = NA, linewidth <\/span><\/em>\n      <em><span style=\"color: #ff0000;\">= 0.5)<\/span><\/em>\n  <em><span style=\"color: #ff0000;\">)<\/span><\/em>\n<em><span style=\"color: #ff0000;\">}<\/span><\/em>\n<span style=\"color: #ff0000;\"><em>theme_set(paul_theme())<\/em><\/span><\/code><\/pre>\n\n\n\n<p><strong>Defining colours<\/strong><\/p>\n\n\n\n<p>Once a plot has been created, it is straight forward to change a palette, by using the scale_color_brewer() function:<\/p>\n\n\n\n<pre class=\"wp-block-code has-small-font-size\"><code><span style=\"color: #ff0000;\"><em>library(ggplot2)<\/em><\/span>\n<span style=\"color: #ff0000;\"><em> data(mpg)<\/em><\/span>\n<span style=\"color: #ff0000;\"><em> mpg<\/em><\/span>\n<mark style=\"background-color:rgba(0, 0, 0, 0);color:#0227f7\" class=\"has-inline-color\"># A tibble: 234 \u00d7 11\n   manufacturer model      displ  year   cyl trans      drv     cty   hwy fl    class  \n   &lt;chr&gt;        &lt;chr&gt;      &lt;dbl&gt; &lt;int&gt; &lt;int&gt; &lt;chr&gt;      &lt;chr&gt; &lt;int&gt; &lt;int&gt; &lt;chr&gt; &lt;chr&gt;  \n 1 audi         a4           1.8  1999     4 auto(l5)   f        18    29 p     compact\n 2 audi         a4           1.8  1999     4 manual(m5) f        21    29 p     compact\n 3 audi         a4           2    2008     4 manual(m6) f        20    31 p     compact\n 4 audi         a4           2    2008     4 auto(av)   f        21    30 p     compact\n 5 audi         a4           2.8  1999     6 auto(l5)   f        16    26 p     compact\n 6 audi         a4           2.8  1999     6 manual(m5) f        18    26 p     compact\n 7 audi         a4           3.1  2008     6 auto(av)   f        18    27 p     compact\n 8 audi         a4 quattro   1.8  1999     4 manual(m5) 4        18    26 p     compact\n 9 audi         a4 quattro   1.8  1999     4 auto(l5)   4        16    25 p     compact\n10 audi         a4 quattro   2    2008     4 manual(m6) 4        20    28 p     compact\n# \u2139 224 more rows\n# \u2139 Use `print(n = ...)` to see more rows<\/mark><\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code has-small-font-size\"><code><span style=\"color: #ff0000;\"><em>ggplot(mpg, aes(x = hwy, y = cty, colour = class)) + <\/em><\/span>\n<span style=\"color: #ff0000;\"><em>geom_point() + <\/em><\/span>\n<span style=\"color: #ff0000;\"><em>theme_bw() + <\/em><\/span>\n<span style=\"color: #ff0000;\"><em>scale_color_brewer(palette = \"Set2\") + <\/em><\/span>\n<span style=\"color: #ff0000;\"><em>ggtitle(\"Set 2\")<\/em><\/span><\/code><\/pre>\n\n\n\n<p><img loading=\"lazy\" decoding=\"async\" width=\"300\" height=\"241\" class=\"alignnone size-medium wp-image-3624\" src=\"https:\/\/pcool.dyndns.org\/wp-content\/uploads\/2025\/06\/Set2-300x241.png\" alt=\"\" srcset=\"https:\/\/pcool.dyndns.org\/wp-content\/uploads\/2025\/06\/Set2-300x241.png 300w, https:\/\/pcool.dyndns.org\/wp-content\/uploads\/2025\/06\/Set2-1024x822.png 1024w, https:\/\/pcool.dyndns.org\/wp-content\/uploads\/2025\/06\/Set2-768x616.png 768w, https:\/\/pcool.dyndns.org\/wp-content\/uploads\/2025\/06\/Set2-1536x1233.png 1536w, https:\/\/pcool.dyndns.org\/wp-content\/uploads\/2025\/06\/Set2-2048x1644.png 2048w\" sizes=\"auto, (max-width: 300px) 100vw, 300px\" \/><\/p>\n\n\n\n<pre class=\"wp-block-code has-small-font-size\"><code><span style=\"color: #ff0000;\"><em>ggplot(mpg, aes(x = hwy, y = cty, colour = class)) + <\/em><\/span>\n<span style=\"color: #ff0000;\"><em>geom_point() + <\/em><\/span>\n<span style=\"color: #ff0000;\"><em>theme_bw() + <\/em><\/span>\n<span style=\"color: #ff0000;\"><em>scale_color_brewer(palette = \"Spectral\") + <\/em><\/span>\n<span style=\"color: #ff0000;\"><em>ggtitle(\"Spectral Set\")<\/em><\/span><\/code><\/pre>\n\n\n\n<figure class=\"wp-block-image\"><img loading=\"lazy\" decoding=\"async\" width=\"300\" height=\"241\" src=\"https:\/\/pcool.dyndns.org\/wp-content\/uploads\/2025\/06\/Spectral-300x241.png\" alt=\"\" class=\"wp-image-3653\" srcset=\"https:\/\/pcool.dyndns.org\/wp-content\/uploads\/2025\/06\/Spectral-300x241.png 300w, https:\/\/pcool.dyndns.org\/wp-content\/uploads\/2025\/06\/Spectral-1024x822.png 1024w, https:\/\/pcool.dyndns.org\/wp-content\/uploads\/2025\/06\/Spectral-768x616.png 768w, https:\/\/pcool.dyndns.org\/wp-content\/uploads\/2025\/06\/Spectral-1536x1233.png 1536w, https:\/\/pcool.dyndns.org\/wp-content\/uploads\/2025\/06\/Spectral-2048x1644.png 2048w\" sizes=\"auto, (max-width: 300px) 100vw, 300px\" \/><\/figure>\n\n\n\n<pre class=\"wp-block-code has-small-font-size\"><code><span style=\"color: #ff0000;\"><em>ggplot(mpg, aes(x = hwy, y = cty, colour = class)) + <\/em><\/span>\n<span style=\"color: #ff0000;\"><em>geom_point() + <\/em><\/span>\n<span style=\"color: #ff0000;\"><em>theme_bw() + <\/em><\/span>\n<span style=\"color: #ff0000;\"><em>scale_color_brewer(palette = \"Blues\") + <\/em><\/span>\n<span style=\"color: #ff0000;\"><em>ggtitle(\"Blues\")<\/em><\/span><\/code><\/pre>\n\n\n\n<figure class=\"wp-block-image\"><img loading=\"lazy\" decoding=\"async\" width=\"300\" height=\"240\" src=\"https:\/\/pcool.dyndns.org\/wp-content\/uploads\/2025\/06\/Blues-300x240.png\" alt=\"\" class=\"wp-image-2883\" srcset=\"https:\/\/pcool.dyndns.org\/wp-content\/uploads\/2025\/06\/Blues-300x240.png 300w, https:\/\/pcool.dyndns.org\/wp-content\/uploads\/2025\/06\/Blues-1024x820.png 1024w, https:\/\/pcool.dyndns.org\/wp-content\/uploads\/2025\/06\/Blues-768x615.png 768w, https:\/\/pcool.dyndns.org\/wp-content\/uploads\/2025\/06\/Blues-1536x1229.png 1536w, https:\/\/pcool.dyndns.org\/wp-content\/uploads\/2025\/06\/Blues-2048x1639.png 2048w\" sizes=\"auto, (max-width: 300px) 100vw, 300px\" \/><\/figure>\n\n\n\n<p>It is also possible to define your own palette using the choose_palette() function in the colorspace package <sup class='sup-ref-note' id='note-zotero-ref-p2097-r3-o1'><a class='sup-ref-note' href='#zotero-ref-p2097-r3'>3<\/a><\/sup>. To create a palette and give it a name (paul_palette):<\/p>\n\n\n\n<pre class=\"wp-block-code has-small-font-size\"><code><span style=\"color: #ff0000;\"><em>library(colorspace)<\/em><\/span>\n<span style=\"color: #ff0000;\"><em>paul_palette &lt;- choose_palette()<\/em><\/span><\/code><\/pre>\n\n\n\n<p>The graphical user interface will allow the creation of a custom palette based on the type of data and a default colour scheme. The number of colours to be available in the palette can also be selected:<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img loading=\"lazy\" decoding=\"async\" width=\"183\" height=\"300\" src=\"https:\/\/pcool.dyndns.org\/wp-content\/uploads\/2025\/06\/palette_chooser1-183x300.png\" alt=\"\" class=\"wp-image-3338\" srcset=\"https:\/\/pcool.dyndns.org\/wp-content\/uploads\/2025\/06\/palette_chooser1-183x300.png 183w, https:\/\/pcool.dyndns.org\/wp-content\/uploads\/2025\/06\/palette_chooser1-623x1024.png 623w, https:\/\/pcool.dyndns.org\/wp-content\/uploads\/2025\/06\/palette_chooser1-768x1262.png 768w, https:\/\/pcool.dyndns.org\/wp-content\/uploads\/2025\/06\/palette_chooser1.png 808w\" sizes=\"auto, (max-width: 183px) 100vw, 183px\" \/><\/figure>\n\n\n\n<p>Once finished, select OK and the palette will be available as paul_palette. If 4 colours of the palette are required, use:<\/p>\n\n\n\n<pre class=\"wp-block-code has-small-font-size\"><code><span style=\"color: #ff0000;\">paul_palette(4)<\/span>\n<span style=\"color: #0000ff;\">&#091;1] \"#823960\" \"#BD9AAA\" \"#91AA91\" \"#1C5F1D\"<\/span><\/code><\/pre>\n\n\n\n<p class=\"p1\">You can also save a palette as an R file, by using the menu bar in colour picker. This will save a palette file to the working directory (ie pauls.R).<\/p>\n\n\n\n<p class=\"p1\">To load the palette, use source:<\/p>\n\n\n\n<pre class=\"wp-block-code has-small-font-size\"><code><span style=\"color: #ff0000;\">pauls &lt;- source(\"pauls.R\")<\/span><\/code><\/pre>\n\n\n\n<p class=\"p1\">This is a function and to recreate the palette as paul_palette:<\/p>\n\n\n\n<pre class=\"wp-block-code has-small-font-size\"><code><span style=\"color: #ff0000;\"><em>pauls_palette &lt;- pauls$value<\/em><\/span>\n<span style=\"color: #ff0000;\"><em>pauls_palette(4)<\/em><\/span>\n<span style=\"color: #0000ff;\"><em>&#091;1] \"#0FCFC0\" \"#D5EAE7\" \"#F3E1EB\" \"#F79CD4\"<\/em><\/span><\/code><\/pre>\n\n\n\n<p>To show the palette create a vector of 10:<\/p>\n\n\n\n<pre class=\"wp-block-code has-small-font-size\"><code><span style=\"color: #ff0000;\"><em>vector &lt;- c(1:10)<\/em><\/span>\n<span style=\"color: #ff0000;\"><em>vector<\/em><\/span>\n<span style=\"color: #0000ff;\"><em> &#091;1] 1 2 3 4 5 6 7 8 9 10<\/em><\/span><\/code><\/pre>\n\n\n\n<p>Create a breaks vector:<\/p>\n\n\n\n<pre class=\"wp-block-code has-small-font-size\"><code><span style=\"color: #ff0000;\"><em>breaks &lt;- c(0, vector)<\/em><\/span>\n<span style=\"color: #ff0000;\"><em>breaks<\/em><\/span>\n<span style=\"color: #0000ff;\"><em> &#091;1] 0 1 2 3 4 5 6 7 8 9 10<\/em><\/span><\/code><\/pre>\n\n\n\n<p>A histogram of 5 colours:<\/p>\n\n\n\n<pre class=\"wp-block-code has-small-font-size\"><code><span style=\"color: #ff0000;\"><em>hist(vector, breaks = breaks, col = pauls_palette(5))<\/em><\/span><\/code><\/pre>\n\n\n\n<figure class=\"wp-block-image\"><img loading=\"lazy\" decoding=\"async\" width=\"300\" height=\"296\" src=\"https:\/\/pcool.dyndns.org\/wp-content\/uploads\/2025\/06\/Palette5-300x296.png\" alt=\"\" class=\"wp-image-3343\" srcset=\"https:\/\/pcool.dyndns.org\/wp-content\/uploads\/2025\/06\/Palette5-300x296.png 300w, https:\/\/pcool.dyndns.org\/wp-content\/uploads\/2025\/06\/Palette5-1024x1012.png 1024w, https:\/\/pcool.dyndns.org\/wp-content\/uploads\/2025\/06\/Palette5-768x759.png 768w, https:\/\/pcool.dyndns.org\/wp-content\/uploads\/2025\/06\/Palette5-1536x1518.png 1536w, https:\/\/pcool.dyndns.org\/wp-content\/uploads\/2025\/06\/Palette5.png 1668w\" sizes=\"auto, (max-width: 300px) 100vw, 300px\" \/><\/figure>\n\n\n\n<p>A histogram of 10 colours:<\/p>\n\n\n\n<pre class=\"wp-block-code has-small-font-size\"><code><em><span style=\"color: #ff0000;\">hist(vector, breaks = breaks, col = pauls_palette(10))<\/span><\/em><\/code><\/pre>\n\n\n\n<figure class=\"wp-block-image\"><img loading=\"lazy\" decoding=\"async\" width=\"300\" height=\"298\" src=\"https:\/\/pcool.dyndns.org\/wp-content\/uploads\/2025\/06\/Palette10-300x298.png\" alt=\"\" class=\"wp-image-3349\" srcset=\"https:\/\/pcool.dyndns.org\/wp-content\/uploads\/2025\/06\/Palette10-300x298.png 300w, https:\/\/pcool.dyndns.org\/wp-content\/uploads\/2025\/06\/Palette10-1024x1018.png 1024w, https:\/\/pcool.dyndns.org\/wp-content\/uploads\/2025\/06\/Palette10-150x150.png 150w, https:\/\/pcool.dyndns.org\/wp-content\/uploads\/2025\/06\/Palette10-768x763.png 768w, https:\/\/pcool.dyndns.org\/wp-content\/uploads\/2025\/06\/Palette10-1536x1527.png 1536w, https:\/\/pcool.dyndns.org\/wp-content\/uploads\/2025\/06\/Palette10.png 1660w\" sizes=\"auto, (max-width: 300px) 100vw, 300px\" \/><\/figure>\n","protected":false},"excerpt":{"rendered":"<p>The ggplot2 library is very versatile and allows the creation of high quality plots. In this section, it is shown how to import a custom font (ie Google), setting a theme for all subsequent plots and define custom colours \/ palettes. Importing a custom font Google has a large collection of fonts that is available [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"parent":0,"menu_order":0,"comment_status":"closed","ping_status":"closed","template":"","meta":{"inline_featured_image":false,"footnotes":""},"class_list":["post-2097","page","type-page","status-publish","hentry"],"_links":{"self":[{"href":"https:\/\/pcool.dyndns.org\/index.php\/wp-json\/wp\/v2\/pages\/2097","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/pcool.dyndns.org\/index.php\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/pcool.dyndns.org\/index.php\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/pcool.dyndns.org\/index.php\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/pcool.dyndns.org\/index.php\/wp-json\/wp\/v2\/comments?post=2097"}],"version-history":[{"count":8,"href":"https:\/\/pcool.dyndns.org\/index.php\/wp-json\/wp\/v2\/pages\/2097\/revisions"}],"predecessor-version":[{"id":4262,"href":"https:\/\/pcool.dyndns.org\/index.php\/wp-json\/wp\/v2\/pages\/2097\/revisions\/4262"}],"wp:attachment":[{"href":"https:\/\/pcool.dyndns.org\/index.php\/wp-json\/wp\/v2\/media?parent=2097"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}