pjocolors: baby's first package 📦

Apr 11, 2020 00:00 · 858 words · 5 minute read packages r

About a 2 weeks ago, I tweeted out that I had released my first package pjocolors, which contains 5 colour palettes based on the (original) book covers of Percy Jackson and the Olympians by Rick Riordan. Today I released the first update, complete with updated palettes and new functions for use with ggplot2📊!

I decided to develop this simple package, not so much for utility purposes (although I’ve used it in two #TidyTuesday’s now!) or out of sheer nerdiness and nostalgia (although that did play a part as well 😄), but because I have been curious about package development for a while. So I figured: what better time to learn than quarantine?

The process

I was inspired to develop a colour palette package for a few reasons.

  • I had just re-read the Lightning Thief ⚡.
  • I’m a sucker for a pretty colour palette 🎨.
  • It seemed like a simple place to start discovering the world of package development.
  • I had recently become obsessed with a couple different colour palette packages (which I use constantly): fishualize and nationalparkcolors.

I have a lot of thanks and praise to give to these two packages. They’ve provided me with some beautiful palettes that I’ve used in various graphics and I’ve crawled through their code a few times over to help me understand how colour palette packages are set-up.

I still have a lot to learn about developing packages, but I have to say that this was a great little exercise! I’m so grateful that the devtools package exists, it made the whole process so painless.

Some great resources that I consulted to learn about the basics of writing packages were

I highly recommend these resources if you’re looking to learn the basics of R packages too. All in all, I would say getting a handle of the basics (but just the basics) took me less than a day (thanks to these great writers).

These last couple weeks, I’ve spent a lot of time on #rstats twitter and reading other R blogger’s posts. My eyes have opened to R development and I’m definitely cultivating a rising interest in the topic. I’m already working on turning pjocolors into a real package that other people would want to use (and not just my little pet project). I’m excited to dive into it more as quarantine drags on and see what comes out of it 👩 🔧.

A bit about pjocolors

pjocolors v0.1.1 contains 6 colour palettes. Currently all palettes only contain 5 colours (however I hope to expand this in the future). The palette options can be accessed as follows:

library(pjocolors)
names(pjo_palettes)
## [1] "LightningThief"    "SeaOfMonsters"     "TitansCurse"      
## [4] "BatOfTheLabyrinth" "LastOlympian"      "LastOlympian2"

Available palettes

Use

To illustrate how to use the palettes, I downloaded some cool data on library check-outs of Percy Jackson related materials from the Checkouts by Title database from the awesome City of Seattle Open Data Portal.

# Read in data (which I downloaded from the database)
pjo_checkouts <- read_csv('percyjacksoncheckouts.csv')

I’ll use this data to create a couple plots and apply some of the pjo_palettes to them by using scale_color_pjo and scale_fill_pjo.

Discrete Scale

Good palettes to use for discrete scales: LightningThief, TitansCurse, BatOfTheLabyrinth, LastOlympian.

# Make plots
p1 <- 
  pjo_checkouts %>% 
  ggplot(aes(CheckoutYear, checkouts, color = MaterialType)) +
  geom_line(size = 1) +
  labs(x = 'Year', y = 'Number of checkouts') +
  guides(colour = guide_legend(""))

p2 <- 
  pjo_checkouts %>%
  ggplot(aes(reorder(MaterialType, -checkouts), checkouts)) +
  geom_col(aes(fill = MaterialType), show.legend = FALSE, 
           alpha = 0.8, width = 0.65) +
  labs(x = NULL, y = 'Number of checkouts')

# Apply pjocolors (default = LightningThief)
p1 + scale_color_pjo()

# Apply pjocolors (BatOfTheLabyrinth)
p2 + scale_fill_pjo(palette = 'BatOfTheLabyrinth')

Continuous Scale

Good palettes to use for continuous scales: LightningThief (diverging scales), SeaOfMonsters (sequential scales), LastOlympian2 (sequential scales).

p3 <- 
  pjo_checkouts %>%
  ggplot(aes(CheckoutMonth, checkouts, group = CheckoutMonth)) +
  geom_point(aes(color = checkouts), alpha = 0.65) +
  labs(x = NULL, y = 'Checkouts') +
  scale_x_continuous(labels = month.abb, breaks = 1:12) +
  guides(color = guide_colorbar(title = 'Number of checkouts'))

# Apply pjocolors (SeaOfMonsters)
p3 + scale_color_pjo(palette = 'SeaOfMonsters', discrete = FALSE)

A Note about base graphics

These colour palettes are able to be used for base graphics too! Rather than using the scale_color_pjo function, one could also just construct a palette as follows

# A palette for base graphics
pal <- pjo_palettes$TitansCurse

Future plans for pjocolors

The ideal is for this package to be a well-documented, general-use colour palette package. What this means to me right now is:

  • Accessibility! I want to make sure I have some colourblind-safe options.
  • Expanding the numbers of colours in some of the palettes.
  • Expanding colour palette options (There’s more books to add!).
  • Updating the colour palettes to make sure they adhere to general colour rules (aka they look as nice as possible).

Of course I’m open to any and all ideas on how this little package of mine could be improved, so feel free to tweet at me @MaiaPelletier if you have any ideas for me 🧠.