I’ve been joking about R’s “200 shades of grey” on training courses for a long time. The popularity of the book “50 Shades of Grey” has changed the meaning of this statement somewhat. As the film is due to be released on Valentine’s Day I thought this might be worth a quick blog post.
Firstly, where did I get “200 shades of grey” from? This statement was originally derived from the 200 available named colours that contain either “grey” or “gray” in the vector generated by the
colours function. As you will see there are in fact 224 shades of grey in R.
greys <- grep("gr[ea]y", colours(), value = TRUE) length(greys)  224
This is because there are also colours such as slategrey, darkgrey and even dimgrey! So lets now remove anything that is more than just “grey” or “gray”.
greys <- grep("^gr[ea]y", colours(), value = TRUE) length(greys)  204
So in fact there are 204 that are classified as “grey” or “gray”. If we take a closer look though its clear that there are not 204 unique shades of grey in R as we are doubling up so that we can use both the British, “grey”, and US, “gray”. This is really useful for R users not having to remember to change the way they usually spell grey/gray (you might also notice that I have used the function
colours rather than
colors) but when it comes to unique greys it means we have to be a little more specific in our search pattern. So stripping back to just shades of “grey”:
greys <- grep("^grey", colours(), value = TRUE) length(greys)  102
we find we are actually down to just 102. Interestingly we don’t double up on all grey/gray colours, slategrey4 doesn’t exist but slategray4 does!
So really we have 102 shades of grey in R. Of course this is only using the named colours, if we were to define the colour using
rgb we can make use of all 256 colour values!
So how can we get 50 shades of grey? Well the
colorRampPalette function can help us out by allowing us to generate new colour palettes based on colours we give it. So a palette that goes from
grey0 (black) to
grey100 (white) can easily be generated.
shadesOfGrey <- colorRampPalette(c("grey0", "grey100")) shadesOfGrey(2)  "#000000" "#FFFFFF"
And 50 shades of grey?
fiftyGreys <- shadesOfGrey(50) mat <- matrix(rep(1:50, each = 50)) image(mat, axes = FALSE, col = fiftyGreys) box()
I hear the film is not as “graphic” as the book, I hope this fits bill!