[This article was first published on R – Nathaniel D. Phillips, PhD, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

When most people think about R, they think of statistics. They don’t think of horror films. I’m here to change that.

Let’s back up. While R is certainly one of the top two (I’m looking over my back at you Python) languages for statistical analysis, it’s just as good for creating beautiful and informative plots – but only if you have the right colors. No matter what kind of plot you create, if you use colors that suck, your plot will look like shit. To pick colors that go well together, you need to use a color palette.

How can you select a color palette in R? Up until now I’ve been using the great RColorBrewer package. However, I’ve lost my enthusiasm for the palettes in the package – not because they’re bad, but because they didn’t mean anything to me. To fix this, I created several palettes either created by graphic artists, or inspired by movies, works of art, and my own aimless Googling. They are all stored in a function called piratepal contained in the yarrr package.

To use piratepal(), you first need to download and install the yarrr package (if you don’t have the devtools package installed, you’ll need to install that first):

install.packages("devtools")
library("devtools")
install_github("ndphillips/yarrr")
library("yarrr")


Once you’ve installed the yarrr package, you can learn about the piratepal() function using the help menu:

?piratepal


Here, you’ll see that piratepal() has three arguments

• palette: A string defining the color palette to use (see examples). To use a random palette, use “random”. To view all palettes, use “all” combined with action = “show”
• action: Either “return” to return a vector of colors, or “show” to show the palette. You can also use “r” or “s” for shorthand.
• trans: A number in the interval [0, 1] indicating how transparent to make the colors. A value of 0 means no transparency and a value of 1 means complete transparency. Defaults to 0.

Let’s start by seeing all the different palettes in the package. To do this, set palette = “all”, and action = “show”:

piratepal(palette = "all", action = "show")


Here, you can see a brief overview of all the palettes in the package. The names of the palettes on are on the left, and the colors in each palette are displayed to the right of the names. If you want to see the colors in a specific palette in more detail, you can see them (combined with an inspirational image) by using action = “show”.

For example, here is a palette called “scholar” that I got from this blog on the Shutterstock website

Now if you want to use the colors in a palette, just use the action = “return” argument to return a vector of the palette colors:

piratepal(palette = "scholar", action = "return")


This code will return the following vector of the colors in the palette:

blue1 yellow blue2 gray
#2B3E56FF #CDA725FF #C5CBD3FF #F1F1E7FF

Once you’ve assigned this vector of colors to an object (like my.colors), you can use them in any plot that you’d like!

You’ll notice that while some of these palettes have vague, generic names (like “scholar”), others are suspiciously recognisable. For example, what is this “monalisa” palette? Well, I discovered this blog post from a site called artvarsity that contains color palettes inspired by classical art. So naturally, I stole some and put them into the package. For example, let’s look at the “monalisa” palette:

piratepal(palette = "monalisa", action = "show")


Oh yeah, I’ve also got several palettes inspired by movies. Oh yes, palettes from movies. I discovered this badass site called Movies in Color which provides color palettes for tons of movies. Don’t get me wrong, these palettes aren’t necessarily the most beautiful in the world – but who doesn’t want to use a palette from a horror movie?!

Here’s a palette from “Eternal Sunshine of the Spotless Mind” – the film shown at every psychology undergraduate social event

piratepal(palette = "eternal", action = "show")


Since I love Pixar movies, and found this great website showing palettes for all the Pixar films, I added lots of palettes from Pixar movies. Let’s take a look at the “up” palette:

piratepal(palette = "up", action = "show")


Just for fun, Let’s draw the house from the movie “Up” using the “google” palette (using my handy digital color meter, I also stole the colors from the Google logo).

google.colors <- piratepal("google", trans = .1)
n.balloons <- 500
x <- rnorm(n.balloons, 0)
y <- rnorm(n.balloons, 2, 1)
plot(1, xlim = c(-7, 7), ylim = c(-7, 7),
xlab = "", ylab = "", type = "n", xaxt = "n", yaxt = "n", bty = "n")

rect(-2, -6, 2, -2)
polygon(c(-2, 0, 2),
c(-2, 0, -2)
)
rect(-7, -7, -2, 100)
rect(2, -7, 7, 100)
rect(-.5, -6, .5, -4)
points(.3, -5)

line.start.x <- rnorm(n.balloons, 0, .4)
line.start.y <- -1 + rnorm(n.balloons, 0, .1)

segments(line.start, line.start.y, x, y, lty = 1, col = gray(.7), lwd = .2)
points(x, y, pch = 21, bg = sample(google.colors, 100, replace = T),
xlim = c(-7, 7), ylim = c(-7, 7), col = gray(.9), cex = rnorm(100, 2, .3))


Here’s the result! Oh and did you know that the Up house really exists in Seattle?!?! Here’s the real one!:

Finally, for all you horror fans out there: here’s the Evil Dead palette. I’ll warn you, it’s pretty bleak – but honestly what did you expect?

piratepal(palette = "evildead", action = "show")


Let’s take advantage of this friendly, colorful palette. Here are the domestic revenues of the 4 Evil Dead films (from the-numbers.com):

evil.colors <- piratepal(
palette = "evildead", action = "return",
trans = .2)

revenues <- c(2400000, 5923044, 11502976, 54239856)

plot(1, xlim = c(.5, 4.5), ylim = c(0, 59000000), type = "n",
xlab = "", ylab = "", xaxt = "n", bty = "n",
yaxt = "n", main = "Evil Dead Movie Domestic Revenues")

mtext("Using the evildead palette in the yarrr package!", side = 3,
cex = .8, line = .5)

segments(1:4, rep(0, 4), 1:4, revenues, lty = 2)

abline(h = seq(0, 55000000, 10000000), col = gray(.9))

labels = c("$2.4 Mil", "$5.9 Mil", "$11.5 Mil", "$54 Mil"), pos = 3)

points(1:4, revenues,
col = evil.colors, cex = 4, pch = 16)

"Evil Deadn(2016)"), at = 1:4, side = 1, line = 1)


Here are a few of the other movie-based palettes:

piratepal(palette = "ghostbusters", action = "show")


Dr Ray Stantz: Everything was fine with our system until the power grid was shut off by dickless here.
Walter Peck: They caused an explosion!
Mayor: Is this true?
Dr. Peter Venkman: Yes it’s true.
[pause]
Dr. Peter Venkman: This man has no dick.

piratepal(palette = "rat", action = "show")


Remy: [as Emile tastes a piece of cheese] Creamy, salty-sweet, an oaky nuttiness… You detect that?
Emile: Oh, I’m detecting nuttiness…

I hope these art / movie / random googling inspired palettes inspire people to make plots with better colors. Oh and if you have a favorite movie that you’d like me to add to the function in the form of a palette, let me know. I can always use a good movie recommendation. Bonus points if it’s available on German Netflix.