“…personal aesthetic preferences can play a role in visualization, and not all design choices that are unrelated to the data can be deemed as useless, whimsical decoration, or chart junk.” Alberto Cairo, The Truthful Art
One of the 5 qualities that constitute a great visualisation, according to Alberto Cairo’s ‘The Truthful Art’, is for it to be beautiful. For someone such as myself, who likes to spend time on the aesthetics of a plot, this is reassuring (the other 4 qualities by the way: truthful, functional, insightful and enlightening). Personally, I find elements such as colour palettes, themes and fonts can help form a compelling visualisation to draw the reader in, and engage with a visualisation they may otherwise have overlooked. In short, some may see funky fonts and colour palettes as needless window-dressing, but I do not!
So what if you have the urge (like me) to venture away from the fonts and palettes that come as standard with R and ggplot2
? In this blog post, I will detail two great packages in R for doing just this: extrafont
and paletteer
.
I’ll use these packages to customise a plot I created for Tidy Tuesday last week, based on data from MyAnimeList.net collected by Tam Nguyen:
Back into #TidyTuesday for some Anime, because I couldn't resist a chance to use the #Ghibli colour palette. I heartily recommend the #paletteer 📦. A one-stop shop for all your colour palette needs. #rstats #ggplot2 #dataviz pic.twitter.com/KsAvmQJOKE
— David Smale 🔎 (@committedtotape) April 25, 2019
I had read that using fonts not already registered with R was a bit of a nightmare. However, I have found that with the use of Winston Chang’s wonderful extrafont that it is fairly painless. So, install the package if required, and then load it:
install.packages("extrafont")
library("extrafont")
## Registering fonts with R
There are then only a handful of functions you’ll require. Firstly, the font_import()
will import all fonts installed on your machine. Where it looks for your fonts my vary depending on your operating system. I have a Mac and I’ve found that as long as you make sure any font you want to use is in your FontBook then it will be imported (if you can’t find FontBook then open LaunchPad and search for it). If you’ve downloaded a font from the web and you can’t see it in FontBook then copy it from your Downloads folder into FontBook. As I’m creating a graphic on Anime films I searched online for an appropriate font (there are many free font websites out there, but be sure to check what usage is permitted):
font_import()
This may take a few minutes, depending on how many fonts need to be imported. Each time you want to use a new font not already imported you will need to run it again. There are then 2 functions to check which fonts are now available to you: fonts()
and fonttable()
. I find that fonttable()
is the most useful as this provides the full font name (not just the font family) that will need to be used in the plotting code:
fonttable()
## package afmfile
## 1 NA Keyboard.afm.gz
## 2 NA SFNSDisplay.afm.gz
## 3 NA SFNSText.afm.gz
## 4 NA SFNSTextItalic.afm.gz
## 5 NA Andale Mono.afm.gz
## 6 NA Apple Braille.afm.gz
## 7 NA Apple Braille Outline 6 Dot.afm.gz
## 8 NA Apple Braille Outline 8 Dot.afm.gz
## 9 NA Apple Braille Pinpoint 6 Dot.afm.gz
## 10 NA Apple Braille Pinpoint 8 Dot.afm.gz
## 11 NA AppleMyungjo.afm.gz
## 12 NA Arial Black.afm.gz
## 13 NA Arial Bold Italic.afm.gz
## 14 NA Arial Bold.afm.gz
## 15 NA Arial Italic.afm.gz
## 16 NA Arial.afm.gz
## 17 NA Arial Narrow.afm.gz
## 18 NA Arial Narrow Bold.afm.gz
## 19 NA Arial Narrow Bold Italic.afm.gz
## 20 NA Arial Narrow Italic.afm.gz
## 21 NA Arial Rounded Bold.afm.gz
## 22 NA Arial Unicode.afm.gz
## 23 NA arsenale_white.afm.gz
## 24 NA AvenirCondensedHand.afm.gz
## 25 NA Bodoni Ornaments.afm.gz
## 26 NA Bodoni 72 Smallcaps Book.afm.gz
## 27 NA Bradley Hand Bold.afm.gz
## 28 NA Brain Flower Euro.afm.gz
## 29 NA Brush Script.afm.gz
## 30 NA clean up your mess.afm.gz
## 31 NA Comic Sans MS.afm.gz
## 32 NA Comic Sans MS Bold.afm.gz
## 33 NA Courier New Bold Italic.afm.gz
## 34 NA Courier New Bold.afm.gz
## 35 NA Courier New Italic.afm.gz
## 36 NA Courier New.afm.gz
## 37 NA DIN Alternate Bold.afm.gz
## 38 NA DIN Condensed Bold.afm.gz
## 39 NA End of the dream.afm.gz
## 40 NA frenchy.afm.gz
## 41 NA Georgia.afm.gz
## 42 NA Georgia Bold.afm.gz
## 43 NA Georgia Bold Italic.afm.gz
## 44 NA Georgia Italic.afm.gz
## 45 NA Impact.afm.gz
## 46 NA KGEyesWideOpen.afm.gz
## 47 NA Khmer Sangam MN.afm.gz
## 48 NA Lao Sangam MN.afm.gz
## 49 NA Luminari.afm.gz
## 50 NA Magnolia.afm.gz
## 51 NA Magnolia Light.afm.gz
## 52 NA Microsoft Sans Serif.afm.gz
## 53 NA Sunshine in my Soul.afm.gz
## 54 NA SverigeScriptDecorated_Demo.afm.gz
## 55 NA SverigeScriptClean_Demo.afm.gz
## 56 NA Tahoma.afm.gz
## 57 NA Tahoma Bold.afm.gz
## 58 NA Thin Skinned.afm.gz
## 59 NA Times New Roman Bold Italic.afm.gz
## 60 NA Times New Roman Bold.afm.gz
## 61 NA Times New Roman Italic.afm.gz
## 62 NA Times New Roman.afm.gz
## 63 NA Trattatello.afm.gz
## 64 NA Trebuchet MS Bold Italic.afm.gz
## 65 NA Trebuchet MS.afm.gz
## 66 NA Trebuchet MS Bold.afm.gz
## 67 NA Trebuchet MS Italic.afm.gz
## 68 NA Verdana.afm.gz
## 69 NA Verdana Bold.afm.gz
## 70 NA Verdana Bold Italic.afm.gz
## 71 NA Verdana Italic.afm.gz
## 72 NA Webdings.afm.gz
## 73 NA Wingdings.afm.gz
## 74 NA Wingdings 2.afm.gz
## 75 NA Wingdings 3.afm.gz
## 76 NA Futura Bold font.afm.gz
## 77 NA Futura Bold Italic font.afm.gz
## 78 NA Futura Book font.afm.gz
## 79 NA Futura Book Italic font.afm.gz
## 80 NA Futura Extra Black font.afm.gz
## 81 NA Futura XBlk BT.afm.gz
## 82 NA Futura Extra Black font.afm.gz
## 83 NA Futura XBlk BT.afm.gz
## 84 NA Futura Heavy font.afm.gz
## 85 NA Futura Heavy Italic font.afm.gz
## 86 NA futura light bt.afm.gz
## 87 NA Futura Light font.afm.gz
## 88 NA Futura Light Italic font.afm.gz
## 89 NA futura medium bt.afm.gz
## 90 NA futura medium condensed bt.afm.gz
## 91 NA Futura Medium Italic font.afm.gz
## 92 NA animeace.afm.gz
## 93 NA animeace_b.afm.gz
## 94 NA animeace_i.afm.gz
## fontfile
## 1 /System/Library/Fonts/Keyboard.ttf
## 2 /System/Library/Fonts/SFNSDisplay.ttf
## 3 /System/Library/Fonts/SFNSText.ttf
## 4 /System/Library/Fonts/SFNSTextItalic.ttf
## 5 /Library/Fonts/Andale Mono.ttf
## 6 /System/Library/Fonts/Apple Braille.ttf
## 7 /System/Library/Fonts/Apple Braille Outline 6 Dot.ttf
## 8 /System/Library/Fonts/Apple Braille Outline 8 Dot.ttf
## 9 /System/Library/Fonts/Apple Braille Pinpoint 6 Dot.ttf
## 10 /System/Library/Fonts/Apple Braille Pinpoint 8 Dot.ttf
## 11 /Library/Fonts/AppleMyungjo.ttf
## 12 /Library/Fonts/Arial Black.ttf
## 13 /Library/Fonts/Arial Bold Italic.ttf
## 14 /Library/Fonts/Arial Bold.ttf
## 15 /Library/Fonts/Arial Italic.ttf
## 16 /Library/Fonts/Arial.ttf
## 17 /Library/Fonts/Arial Narrow.ttf
## 18 /Library/Fonts/Arial Narrow Bold.ttf
## 19 /Library/Fonts/Arial Narrow Bold Italic.ttf
## 20 /Library/Fonts/Arial Narrow Italic.ttf
## 21 /Library/Fonts/Arial Rounded Bold.ttf
## 22 /Library/Fonts/Arial Unicode.ttf
## 23 /Library/Fonts/arsenale_white.ttf
## 24 /Library/Fonts/AvenirCondensedHand.ttf
## 25 /Library/Fonts/Bodoni Ornaments.ttf
## 26 /Library/Fonts/Bodoni 72 Smallcaps Book.ttf
## 27 /Library/Fonts/Bradley Hand Bold.ttf
## 28 /Library/Fonts/Brain Flower Euro.ttf
## 29 /Library/Fonts/Brush Script.ttf
## 30 /Library/Fonts/clean up your mess.ttf
## 31 /Library/Fonts/Comic Sans MS.ttf
## 32 /Library/Fonts/Comic Sans MS Bold.ttf
## 33 /Library/Fonts/Courier New Bold Italic.ttf
## 34 /Library/Fonts/Courier New Bold.ttf
## 35 /Library/Fonts/Courier New Italic.ttf
## 36 /Library/Fonts/Courier New.ttf
## 37 /Library/Fonts/DIN Alternate Bold.ttf
## 38 /Library/Fonts/DIN Condensed Bold.ttf
## 39 /Library/Fonts/End of the dream.ttf
## 40 /Library/Fonts/frenchy.ttf
## 41 /Library/Fonts/Georgia.ttf
## 42 /Library/Fonts/Georgia Bold.ttf
## 43 /Library/Fonts/Georgia Bold Italic.ttf
## 44 /Library/Fonts/Georgia Italic.ttf
## 45 /Library/Fonts/Impact.ttf
## 46 /Library/Fonts/KGEyesWideOpen.ttf
## 47 /Library/Fonts/Khmer Sangam MN.ttf
## 48 /Library/Fonts/Lao Sangam MN.ttf
## 49 /Library/Fonts/Luminari.ttf
## 50 /Library/Fonts/Magnolia.ttf
## 51 /Library/Fonts/Magnolia Light.ttf
## 52 /Library/Fonts/Microsoft Sans Serif.ttf
## 53 /Library/Fonts/Sunshine in my Soul.ttf
## 54 /Library/Fonts/SverigeScriptDecorated_Demo.ttf
## 55 /Library/Fonts/SverigeScriptClean_Demo.ttf
## 56 /Library/Fonts/Tahoma.ttf
## 57 /Library/Fonts/Tahoma Bold.ttf
## 58 /Library/Fonts/Thin Skinned.ttf
## 59 /Library/Fonts/Times New Roman Bold Italic.ttf
## 60 /Library/Fonts/Times New Roman Bold.ttf
## 61 /Library/Fonts/Times New Roman Italic.ttf
## 62 /Library/Fonts/Times New Roman.ttf
## 63 /Library/Fonts/Trattatello.ttf
## 64 /Library/Fonts/Trebuchet MS Bold Italic.ttf
## 65 /Library/Fonts/Trebuchet MS.ttf
## 66 /Library/Fonts/Trebuchet MS Bold.ttf
## 67 /Library/Fonts/Trebuchet MS Italic.ttf
## 68 /Library/Fonts/Verdana.ttf
## 69 /Library/Fonts/Verdana Bold.ttf
## 70 /Library/Fonts/Verdana Bold Italic.ttf
## 71 /Library/Fonts/Verdana Italic.ttf
## 72 /Library/Fonts/Webdings.ttf
## 73 /Library/Fonts/Wingdings.ttf
## 74 /Library/Fonts/Wingdings 2.ttf
## 75 /Library/Fonts/Wingdings 3.ttf
## 76 /Users/davidsmale/Library/Fonts/Futura Bold font.ttf
## 77 /Users/davidsmale/Library/Fonts/Futura Bold Italic font.ttf
## 78 /Users/davidsmale/Library/Fonts/Futura Book font.ttf
## 79 /Users/davidsmale/Library/Fonts/Futura Book Italic font.ttf
## 80 /Users/davidsmale/Library/Fonts/Futura Extra Black font.ttf
## 81 /Users/davidsmale/Library/Fonts/Futura Extra Black font.ttf
## 82 /Users/davidsmale/Library/Fonts/Futura XBlk BT.ttf
## 83 /Users/davidsmale/Library/Fonts/Futura XBlk BT.ttf
## 84 /Users/davidsmale/Library/Fonts/Futura Heavy font.ttf
## 85 /Users/davidsmale/Library/Fonts/Futura Heavy Italic font.ttf
## 86 /Users/davidsmale/Library/Fonts/futura light bt.ttf
## 87 /Users/davidsmale/Library/Fonts/Futura Light font.ttf
## 88 /Users/davidsmale/Library/Fonts/Futura Light Italic font.ttf
## 89 /Users/davidsmale/Library/Fonts/futura medium bt.ttf
## 90 /Users/davidsmale/Library/Fonts/futura medium condensed bt.ttf
## 91 /Users/davidsmale/Library/Fonts/Futura Medium Italic font.ttf
## 92 /Library/Fonts/animeace.ttf
## 93 /Library/Fonts/animeace_b.ttf
## 94 /Library/Fonts/animeace_i.ttf
## FullName FamilyName
## 1 .Keyboard .Keyboard
## 2 System Font System Font
## 3 System Font System Font
## 4 System Font Italic System Font
## 5 Andale Mono Andale Mono
## 6 Apple Braille Apple Braille
## 7 Apple Braille Outline 6 Dot Apple Braille
## 8 Apple Braille Outline 8 Dot Apple Braille
## 9 Apple Braille Pinpoint 6 Dot Apple Braille
## 10 Apple Braille Pinpoint 8 Dot Apple Braille
## 11 AppleMyungjo Regular AppleMyungjo
## 12 Arial Black Arial Black
## 13 Arial Bold Italic Arial
## 14 Arial Bold Arial
## 15 Arial Italic Arial
## 16 Arial Arial
## 17 Arial Narrow Arial Narrow
## 18 Arial Narrow Bold Arial Narrow
## 19 Arial Narrow Bold Italic Arial Narrow
## 20 Arial Narrow Italic Arial Narrow
## 21 Arial Rounded MT Bold Arial Rounded MT Bold
## 22 Arial Unicode MS Arial Unicode MS
## 23 Arsenale White Arsenale White
## 24 AvenirCondensedHand AvenirCondensedHand
## 25 Bodoni Ornaments Bodoni Ornaments
## 26 Bodoni 72 Smallcaps Book Bodoni 72 Smallcaps
## 27 Bradley-Hand-Bold
## 28 Brain Flower Euro Brain Flower
## 29 Brush Script MT Italic Brush Script MT
## 30 clean up your mess clean up your mess
## 31 Comic Sans MS Comic Sans MS
## 32 Comic Sans MS Bold Comic Sans MS
## 33 Courier New Bold Italic Courier New
## 34 Courier New Bold Courier New
## 35 Courier New Italic Courier New
## 36 Courier New Courier New
## 37 DIN Alternate Bold DIN Alternate
## 38 DIN Condensed Bold DIN Condensed
## 39 End of the dream End of the dream
## 40 Frenchy Frenchy
## 41 Georgia Georgia
## 42 Georgia Bold Georgia
## 43 Georgia Bold Italic Georgia
## 44 Georgia Italic Georgia
## 45 Impact Impact
## 46 KG Eyes Wide Open KG Eyes Wide Open
## 47 Khmer Sangam MN Khmer Sangam MN
## 48 Lao Sangam MN Lao Sangam MN
## 49 Luminari Luminari
## 50 Magnolia Italic Magnolia
## 51 Magnolia Light Magnolia Light
## 52 Microsoft Sans Serif Microsoft Sans Serif
## 53 Sunshine In My Soul Sunshine In My Soul
## 54 Sverige Script Decorated Demo Sverige Script Decorated Demo
## 55 Sverige Script Demo Sverige Script Demo
## 56 Tahoma Tahoma
## 57 Tahoma Bold Tahoma
## 58 Thin Skinned Thin Skinned
## 59 Times New Roman Bold Italic Times New Roman
## 60 Times New Roman Bold Times New Roman
## 61 Times New Roman Italic Times New Roman
## 62 Times New Roman Times New Roman
## 63 Trattatello Trattatello
## 64 Trebuchet MS Bold Italic Trebuchet MS
## 65 Trebuchet MS Trebuchet MS
## 66 Trebuchet MS Bold Trebuchet MS
## 67 Trebuchet MS Italic Trebuchet MS
## 68 Verdana Verdana
## 69 Verdana Bold Verdana
## 70 Verdana Bold Italic Verdana
## 71 Verdana Italic Verdana
## 72 Webdings Webdings
## 73 Wingdings Wingdings
## 74 Wingdings 2 Wingdings 2
## 75 Wingdings 3 Wingdings 3
## 76 Futura Bold Condensed BT Futura BdCn BT
## 77 Futura Bold Italic BT Futura Md BT
## 78 Futura Book BT Futura Bk BT
## 79 Futura Book Italic BT Futura Bk BT
## 80 Futura Extra Black BT Futura XBlk BT
## 81 Futura Extra Black BT Futura XBlk BT
## 82 Futura Extra Black BT Futura XBlk BT
## 83 Futura Extra Black BT Futura XBlk BT
## 84 Futura Heavy BT Futura Hv BT
## 85 Futura Heavy Italic BT Futura Hv BT
## 86 Futura Light BT Futura Lt BT
## 87 Futura Light Condensed BT Futura LtCn BT
## 88 Futura Light Italic BT Futura Lt BT
## 89 Futura Medium BT Futura Md BT
## 90 Futura Medium Condensed BT Futura MdCn BT
## 91 Futura Medium Italic BT Futura Md BT
## 92 Anime Ace Anime Ace
## 93 Anime Ace Bold Anime Ace
## 94 Anime Ace Italic Anime Ace
## FontName Bold Italic Symbol afmsymfile
## 1 -Keyboard FALSE FALSE FALSE NA
## 2 -SFNSDisplay FALSE FALSE FALSE NA
## 3 -SFNSText FALSE FALSE FALSE NA
## 4 -SFNSText-Italic FALSE TRUE FALSE NA
## 5 AndaleMono FALSE FALSE FALSE NA
## 6 AppleBraille FALSE FALSE FALSE NA
## 7 AppleBraille-Outline6Dot FALSE FALSE FALSE NA
## 8 AppleBraille-Outline8Dot FALSE FALSE FALSE NA
## 9 AppleBraille-Pinpoint6Dot FALSE FALSE FALSE NA
## 10 AppleBraille-Pinpoint8Dot FALSE FALSE FALSE NA
## 11 AppleMyungjo FALSE FALSE FALSE NA
## 12 Arial-Black FALSE FALSE FALSE NA
## 13 Arial-BoldItalicMT TRUE TRUE FALSE NA
## 14 Arial-BoldMT TRUE FALSE FALSE NA
## 15 Arial-ItalicMT FALSE TRUE FALSE NA
## 16 ArialMT FALSE FALSE FALSE NA
## 17 ArialNarrow FALSE FALSE FALSE NA
## 18 ArialNarrow-Bold TRUE FALSE FALSE NA
## 19 ArialNarrow-BoldItalic TRUE TRUE FALSE NA
## 20 ArialNarrow-Italic FALSE TRUE FALSE NA
## 21 ArialRoundedMTBold FALSE FALSE FALSE NA
## 22 ArialUnicodeMS FALSE FALSE FALSE NA
## 23 ArsenaleWhite FALSE FALSE FALSE NA
## 24 AvenirCondensedHand FALSE FALSE FALSE NA
## 25 BodoniOrnamentsITCTT FALSE FALSE FALSE NA
## 26 BodoniSvtyTwoSCITCTT-Book FALSE FALSE FALSE NA
## 27 Bradley-Hand-Bold TRUE FALSE FALSE NA
## 28 BrainFlower FALSE FALSE FALSE NA
## 29 BrushScriptMT FALSE TRUE FALSE NA
## 30 cleanupyourmess FALSE FALSE FALSE NA
## 31 ComicSansMS FALSE FALSE FALSE NA
## 32 ComicSansMS-Bold TRUE FALSE FALSE NA
## 33 CourierNewPS-BoldItalicMT TRUE TRUE FALSE NA
## 34 CourierNewPS-BoldMT TRUE FALSE FALSE NA
## 35 CourierNewPS-ItalicMT FALSE TRUE FALSE NA
## 36 CourierNewPSMT FALSE FALSE FALSE NA
## 37 DINAlternate-Bold TRUE FALSE FALSE NA
## 38 DINCondensed-Bold TRUE FALSE FALSE NA
## 39 Endofthedream FALSE FALSE FALSE NA
## 40 Frenchy FALSE FALSE FALSE NA
## 41 Georgia FALSE FALSE FALSE NA
## 42 Georgia-Bold TRUE FALSE FALSE NA
## 43 Georgia-BoldItalic TRUE TRUE FALSE NA
## 44 Georgia-Italic FALSE TRUE FALSE NA
## 45 Impact FALSE FALSE FALSE NA
## 46 KGEyesWideOpen FALSE FALSE FALSE NA
## 47 KhmerSangamMN FALSE FALSE FALSE NA
## 48 LaoSangamMN FALSE FALSE FALSE NA
## 49 Luminari-Regular FALSE FALSE FALSE NA
## 50 Magnolia-Italic FALSE TRUE FALSE NA
## 51 MagnoliaLight FALSE FALSE FALSE NA
## 52 MicrosoftSansSerif FALSE FALSE FALSE NA
## 53 SunshineInMySoul FALSE FALSE FALSE NA
## 54 SverigeScriptDecoratedDemo FALSE FALSE FALSE NA
## 55 SverigeScriptDemo FALSE FALSE FALSE NA
## 56 Tahoma FALSE FALSE FALSE NA
## 57 Tahoma-Bold TRUE FALSE FALSE NA
## 58 ThinSkinned FALSE FALSE FALSE NA
## 59 TimesNewRomanPS-BoldItalicMT TRUE TRUE FALSE NA
## 60 TimesNewRomanPS-BoldMT TRUE FALSE FALSE NA
## 61 TimesNewRomanPS-ItalicMT FALSE TRUE FALSE NA
## 62 TimesNewRomanPSMT FALSE FALSE FALSE NA
## 63 Trattatello FALSE FALSE FALSE NA
## 64 Trebuchet-BoldItalic TRUE TRUE FALSE NA
## 65 TrebuchetMS FALSE FALSE FALSE NA
## 66 TrebuchetMS-Bold TRUE FALSE FALSE NA
## 67 TrebuchetMS-Italic FALSE TRUE FALSE NA
## 68 Verdana FALSE FALSE FALSE NA
## 69 Verdana-Bold TRUE FALSE FALSE NA
## 70 Verdana-BoldItalic TRUE TRUE FALSE NA
## 71 Verdana-Italic FALSE TRUE FALSE NA
## 72 Webdings FALSE FALSE FALSE NA
## 73 Wingdings-Regular FALSE FALSE FALSE NA
## 74 Wingdings2 FALSE FALSE FALSE NA
## 75 Wingdings3 FALSE FALSE FALSE NA
## 76 FuturaBT-BoldCondensed TRUE FALSE FALSE NA
## 77 FuturaBT-BoldItalic TRUE TRUE FALSE NA
## 78 FuturaBT-Book FALSE FALSE FALSE NA
## 79 FuturaBT-BookItalic FALSE TRUE FALSE NA
## 80 FuturaBT-ExtraBlack FALSE FALSE FALSE NA
## 81 FuturaBT-ExtraBlack FALSE FALSE FALSE NA
## 82 FuturaBT-ExtraBlack FALSE FALSE FALSE NA
## 83 FuturaBT-ExtraBlack FALSE FALSE FALSE NA
## 84 FuturaBT-Heavy FALSE FALSE FALSE NA
## 85 FuturaBT-HeavyItalic FALSE TRUE FALSE NA
## 86 FuturaBT-Light FALSE FALSE FALSE NA
## 87 FuturaBT-LightCondensed FALSE FALSE FALSE NA
## 88 FuturaBT-LightItalic FALSE TRUE FALSE NA
## 89 FuturaBT-Medium FALSE FALSE FALSE NA
## 90 FuturaBT-MediumCondensed FALSE FALSE FALSE NA
## 91 FuturaBT-MediumItalic FALSE TRUE FALSE NA
## 92 AnimeAce FALSE FALSE FALSE NA
## 93 AnimeAceBold TRUE FALSE FALSE NA
## 94 AnimeAceItalic FALSE TRUE FALSE NA
I find that the new fonts I’ve added to my machine are placed at the bottom of the table. I’m going to use AnimeAce and AnimeAceBold in my final plot (scroll right to the bottom in the above output to see them).
There are many things to consider when choosing a colour palette for your plot, especially ensuring it is colour-blind safe, and it probably deserves a blog post of its own. But for this task, I couldn’t resist the opportunity to use the Ghibli palette by Ewen. You can use the ghibli
package itself, however, the paletteer package by Emil Hvitfeldt collects a bunch of palettes created in R and gives them a standard implementation for easier use (I’m particularly fond of the wesanderson and NineteenEightyR palettes!). You can apply your palette of choice to your aesthetics in R by using variants on the scale_color
and scale_fill
functions in ggplot2
. These take the package name as the 1st argument and the palette name as the 2nd argument (you can opionally reverse the order of the colours too). You’ll see in my final plot that these 2 lines are added:
scale_fill_paletteer_d(ghibli, PonyoMedium, direction = -1)
scale_color_paletteer_d(ghibli, PonyoMedium, direction = -1)
Let’s get the data ready now for plotting. I won’t go into this here, as we are focussing on the plot customisation.
library(tidyverse)
library(lubridate)
library(paletteer)
tidy_anime <- readr::read_csv("https://raw.githubusercontent.com/rfordatascience/tidytuesday/master/data/2019/2019-04-23/tidy_anime.csv")
# remove duplicate entries because of multiple genres
# only keep those scored by at least 100 users
tidy_anime_unique <- tidy_anime %>%
distinct(animeID, .keep_all = TRUE) %>%
filter(scored_by >= 100, !is.na(score), !is.na(start_date)) %>%
mutate(decade = (year(start_date) %/% 10) * 10)
# focus just on films since 1960
tidy_anime_movie <- tidy_anime_unique %>%
filter(type == "Movie", start_date >= as.Date("1960-01-01")) %>%
mutate(decade = paste0(decade, "s"))
# top 5 most rated films to be highlighted in plot
movie_most_scored <- tidy_anime_movie %>%
top_n(5, scored_by)
One final step before the plot, I’m going to set a background plot colour to be used in a few theme elements. The paletteer
package also enables you to extract a vector of colours from a selected package and palette:
# background colour
back_col <- paletteer_d(ghibli, MarnieLight1)[2]
As a little experiment, let’s see how the plot would look without the customisation:
ggplot(tidy_anime_movie, aes(x = decade, y = score)) +
geom_jitter(aes(size = scored_by, fill = decade),
width = 0.1, alpha = 0.4, shape = 21) +
geom_boxplot(aes(fill = decade), colour = "white", width = 0.4,
show.legend = FALSE, outlier.shape = NA, alpha = 0.4) +
geom_text(aes(x = "2000s", y = 9.5,
label = "Movies with the most user ratings\ntend to have the highest scores"),
size = 2.5, hjust = 0.5) +
scale_y_continuous(limits = c(0, 10), breaks = seq(0, 10, 2)) +
scale_size_continuous(breaks = seq(100000, 500000, 100000),
labels = paste0(seq(100, 500, 100), "k")) +
labs(title = "Are Anime Movies getting better?",
subtitle = "Anime movies since the 1960s as scored by MyAnimeList users",
caption = "Movies scored by less than 100 users have been removed\n@committedtotape") +
guides(colour = FALSE,
fill = FALSE)
There’s nothing wrong with this, but I think it could be better! Now, let’s add the font and palette changes, along with further customisations of theme
elements:
ggplot(tidy_anime_movie, aes(x = decade, y = score)) +
geom_jitter(aes(size = scored_by, fill = decade),
width = 0.1, alpha = 0.4, shape = 21,
colour = ifelse(tidy_anime_movie$animeID %in% movie_most_scored$animeID, "white", "black")) +
geom_boxplot(aes(fill = decade), colour = "white", width = 0.4,
show.legend = FALSE, outlier.shape = NA, alpha = 0.4) +
geom_text(aes(x = "2000s", y = 9.5,
label = "Movies with the most user ratings\ntend to have the highest scores"),
size = 2.5, colour = "white", hjust = 0.5,
family = "AnimeAce") +
scale_fill_paletteer_d(ghibli, PonyoMedium, direction = -1) +
scale_color_paletteer_d(ghibli, PonyoMedium, direction = -1) +
scale_y_continuous(limits = c(0, 10), breaks = seq(0, 10, 2)) +
scale_size_continuous(breaks = seq(100000, 500000, 100000),
labels = paste0(seq(100, 500, 100), "k")) +
labs(x = "", y = "Score",
size = "Bubble size represents no. of users that scored movie",
title = "Are Anime Movies getting better?",
subtitle = "Anime movies since the 1960s as scored by MyAnimeList users",
caption = "Movies scored by less than 100 users have been removed\n@committedtotape") +
theme(text = element_text(colour = "white", family = "AnimeAce"),
plot.title = element_text(family = "AnimeAceBold", size = rel(1.5)),
plot.caption = element_text(size = rel(0.7)),
plot.background = element_rect(fill = back_col),
panel.background = element_rect(fill = back_col),
panel.grid = element_line(colour = "gray40"),
panel.grid.major.x = element_blank(),
axis.text = element_text(colour = "white"),
legend.position = c(0.5, 0.15),
legend.direction = "horizontal",
legend.background = element_rect(fill = back_col),
legend.box.background = element_rect(fill = back_col),
legend.key = element_rect(fill = back_col, colour = back_col),
legend.title = element_text(size = rel(0.8))) +
guides(colour = FALSE,
fill = FALSE,
size = guide_legend(title.position = "top", title.hjust = 0.5,
override.aes = list(colour = "white", alpha = 1)))
Hopefully you agree that it’s had quite the transformation and the extra effort creates a plot that is far more engaging. What do you think?
One final note on the chart type chosen. I really like to show the data. The jitter geom, along with a lower alpha level, is great for doing this (reducing the problem of over-plotting). When coupled with a boxplot you get the best of both worlds, enabling you to see each data point along with the general spread of the data. I’ve also sized the bubbles based on how many users scored the movie, which reveals that the highest scoring movies tend to have more users rating them, perhaps skewing things in favour of more recent films. So let’s finish where we started, with a quote from Alberto Cairo’s fantastic book.
“To understand our data well, we may want to explore them in all its glorious detail.” Alberto Cairo, The Truthful Art
Thanks for reading!