Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.
I’m a long-term fan of Weezer. Such was the brilliance of their first two albums that I have stuck with them through thick and thin. And dear me, there has been some very thin music. Nonetheless I own every album – thirteen of them. Among them are six albums entitled “Weezer”.
These records are colloquially referred to by the colour of the album. In chronological order: blue, green, red, white, teal and black. It struck me that these colours have a dataviz quality to them. They could be used for making colour palettes in R.
What are the colours?
Title | Year | rgb | hex | hsl | RYM |
blue | 1994 | rgb(24,155,204) | #189BCC | hsl(196,79,45) | 3.91 |
green | 2001 | rgb(190,204,65) | #BECC41 | hsl(66,58,53) | 3.02 |
red | 2008 | rgb(234,33,58) | #EA213A | hsl(353,83,52) | 2.51 |
white | 2016 | rgb(243,243,243) | #F3F3F3 | hsl(0,0,95) | 3.46 |
teal | 2018 | rgb(43,188,187) | #2BBCBB | hsl(180,63,45) | 2.18 |
black | 2019 | rgb(13,13,13) | #0D0D0D | hsl(0,0,5) | 2.18 |
Blue, green, red and teal are all plain colour. White and black albums are a gradient and the exact colour depends on where you sample.
Let’s use them in R!
We can specify the colours in hex format as a character vector and use them direct in ggplot, as shown below. Here I am plotting each “Weezer” album’s rating on rateyourmusic.
library(tidyverse) library(extra) # weezer colours taken from weezer albums weezer_album_colours <- c("#189BCC", "#BECC41", "#EA213A", "#F3F3F3", "#2BBCBB", "#0D0D0D") # ratings of weezer albums from rateyourmusic.com taken on 2020-05-09 df <- data.frame(year=c("1994", "2001", "2008", "2016", "2018", "2019"), rym=c(3.91,3.02,2.51,3.46,2.18,2.18)) # make a plot using these colours p1 <- ggplot(df, aes(x = year, y = rym, fill = year)) + scale_fill_manual(values = weezer_album_colours) + geom_bar(stat="identity", colour = "black") + labs(title = "Ratings for Weezer's Weezer albums", subtitle = "Data from rateyourmusic.com", x = "weezer", y = "Rating") + theme(text = element_text(family = "Futura-Medium"), legend.position="none") ggsave("ratings.png",p1,dpi = 300)
I used the extras package to load in Futura Medium which is probably the basis for the Weezer band logo.
That was fun, but specifying the hex code for each album colour is a bit cumbersome.
How can we use these colours in a custom palette?
We can make a custom palette of named colours so that we can easily access the colours to make plots.
# now make a named character vector of weezer colours weezer_colours <- c( `blue` = "#189BCC", `green` = "#BECC41", `red` = "#EA213A", `white` = "#F3F3F3", `teal` = "#2BBCBB", `black` = "#0D0D0D") # a function to get hex codes of weezer colours weezer_cols <- function(...) { cols <- c(...) if (is.null(cols)) return (weezer_colours) weezer_colours[cols] } # all colours can be listed with weezer_cols() # or colours can be returned by character name(s) weezer_cols("red", "blue") # weezer colours can be used in a plot like this p2 <- ggplot(mtcars, aes(x = hp, y = mpg)) + geom_point(color = weezer_cols("teal"),size = 3) + theme(text = element_text(family = "Futura-Medium")) ggsave("teal_example.png", p2, dpi = 300)
The example above shows how to use the album cover colours directly in a plot. How about generating a LUT/colour palette? We can specify a gradient of colours and get R to interpolate colours along the gradient to use for colourscales and other methods.
# now let's make some palettes weezer_palettes <- list( `main` = weezer_cols("blue", "red", "green"), `cool` = weezer_cols("white", "teal"), `hot` = weezer_cols("black", "red"), `mixed` = weezer_cols("blue", "green", "red", "white", "teal", "black"), `mono` = weezer_cols("white", "black") ) # function to interpolate palette. Default is main. Option to reverse weezer_pal <- function(palette = "main", reverse = FALSE, ...) { pal <- weezer_palettes[[palette]] if (reverse) pal <- rev(pal) colorRampPalette(pal, ...) } # function to colour graph bjects scale_colour_weezer <- function(palette = "main", discrete = TRUE, reverse = FALSE, ...) { pal <- weezer_pal(palette = palette, reverse = reverse) if (discrete) { discrete_scale("colour", paste0("weezer_", palette), palette = pal, ...) } else { scale_color_gradientn(colours = pal(256), ...) } } # function for filling graph objects scale_fill_weezer <- function(palette = "main", discrete = TRUE, reverse = FALSE, ...) { pal <- weezer_pal(palette = palette, reverse = reverse) if (discrete) { discrete_scale("fill", paste0("weezer_", palette), palette = pal, ...) } else { scale_fill_gradientn(colours = pal(256), ...) } }
These functions allow us to specify gradients between the different album cover colours. Here are some examples:
# examples of a plot using scale_colour_weezer p2 <- ggplot(iris, aes(Sepal.Width, Sepal.Length, color = Species)) + geom_point(size = 3) + scale_colour_weezer() + theme(text = element_text(family = "Futura-Medium")) ggsave("colour_scale_example.png",p2,dpi = 300)
p3 <- ggplot(iris, aes(Sepal.Width, Sepal.Length, color = Sepal.Length)) + geom_point(size = 3) + scale_colour_weezer(discrete = FALSE, palette = "hot") + theme(text = element_text(family = "Futura-Medium")) ggsave("colour_scale_example2.png", p3, dpi = 300)
p4 <- ggplot(mpg, aes(manufacturer, fill = manufacturer)) + geom_bar() + theme(axis.text.x = element_text(angle = 45, hjust = 1)) + scale_fill_weezer(palette = "mixed", guide = "none") + theme(text = element_text(family = "Futura-Medium")) ggsave("colour_scale_example3.png", p4, dpi = 300)
Most of the code above was adapted from this really useful post.
—
The post title is taken from “Say It Ain’t So” by Weezer taken from their debut LP “Weezer” also known as The Blue Album.
R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.