My First R Package: harrypotter

cran version rstudio mirror per-month downloads rstudio mirror total downloads

I want to present you my first R package, already published in CRAN.

I have to say that has been a challenging but very exciting experience. I’ve learned a lot of stuff and now I can appreciate that the way packages are structured, and the existence of the CRAN is one of the highlights of R, and one of its strongest advantages over other languages.

This package provides the first round of palettes derived from the Harry Potter film series.

At its first version, it simply contains the palettes of the Hogwarts Houses. They have been chosen manually, taking into account its consistency with all the existing branding of the franchise, but its suitability for data visualisation.

The colour palette should be beautiful, useful for plotting data and shoulr relate to desired style; in this case, should relate to the Harry Potter world. Some of the colours might change in future versions, in order to find this balance between suitability for plotting and relatable to the Harry Potter universe.

Most of us need to listen to the music to understand how beautiful it is. But often that’s how statistics are presented: we show the notes instead of playing the music.

Installation

Just copy and execute this bunch of code and you’ll have the last version of the package installed:

install.packages("harrypotter")

And you can now use it:

library(harrypotter)

Usage

The default colour scale of the package is the one of the house Hufflepuff. If you prefer to choose another one, you’ll need to specify which house you want the palette from.

Let’s say that you want a palette made from the house Gryffindor.

pal <- hp(25, house = "Gryffindor")
image(volcano, col = pal)

Or a bit more like me, you prefer to be a Ravenclaw.

pal <- hp(25, house = "Ravenclaw")
image(volcano, col = pal)

Or put them all together

pal_gryff <- hp(25, house = "Gryffindor")
pal_rav   <- hp(25, house = "Ravenclaw")
pal_huff  <- hp(25, house = "Hufflepuff")
pal_sly   <- hp(25, house = "Slytherin")

par(mfrow = c(2,2))
image(volcano, col = pal_gryff)
image(volcano, col = pal_rav)
image(volcano, col = pal_huff)
image(volcano, col = pal_sly)

ggplot2

Of course, this package has specific functions to behave seamlessly with the best data visiualisation library available. The package contains colour scale functions for ggplot2 plots: scale_color_hp() and scale_fill_hp().

Here is a made up example using the colours from the house of Hufflepuff,

library(ggplot2)
ggplot(data.frame(x = rnorm(1e4), y = rnorm(1e4)), aes(x = x, y = y)) +
  geom_hex() + 
	coord_fixed() +
  scale_fill_hp(house = "hufflepuff") + 
	theme_bw()

and Ravenclaw

library(ggplot2)
ggplot(data.frame(x = rnorm(1e4), y = rnorm(1e4)), aes(x = x, y = y)) +
  geom_hex() + 
	coord_fixed() +
  scale_fill_hp(house = "ravenclaw") + 
	theme_bw()

or more made-up heatmaps

Using the same function we can also plot these cloropleth maps of U.S. unemployment:

But what if you want discrete scales? These functions also can be used for discrete scales with the argument discrete = TRUE. This argument, when TRUE, sets a finite number of sufficiently spaced colours within the selected palette to plot your data.

library("ggplot2")
ggplot(mtcars, aes(factor(cyl), fill=factor(vs))) +  
	geom_bar() +
  scale_fill_hp(discrete = TRUE, house = "Ravenclaw")
ggplot(mpg, aes(class)) +
	geom_bar(aes(fill = drv), position = position_stack(reverse = TRUE)) +
 coord_flip() +
	scale_fill_hp(discrete = TRUE, house = "Gryffindor") +
 theme(legend.position = "top") +
	ylab("") +
	xlab("Class")
x <- y <- seq(-8*pi, 8*pi, len = 40)
r <- sqrt(outer(x^2, y^2, "+"))
filled.contour(cos(r^2)*exp(-r/(2*pi)), 
               axes=FALSE,
               color.palette=hp,
               asp=1)
comments powered by Disqus