Hi. I’m Sharon Machlis at IDG Communications, here with Episode 59 of Do More With R:
Must-know tips and tricks for R colors and palettes.
Let’s start with single colors.
There are more than 650 colors built right into R. They let you use color names instead of hex codes.
Want to see them? The color()function lists all
the color names, but that doesn’t help you see them.
There are websites and PDFs where you can view all the colors and what they look like.
But why not use your own searchable table in R?
I built a package to do just that, which you’re welcome to download from GitHub using
install_github() from the remotes or devtools packages. You can see the installation code here –
make sure to include build_vignettes = TRUE if you want the package vignette, too.
Load the package, then just run the create_color_table() function.
You can do some rudimentary searching, although not all blue-ish colors have blue in the name.
I included columns for rgb red, green, and blue values, so you can sort by those, too.
That might help put colors in a better order than alphabetically by their name.
To sort on more than one column at a time, hold down the shift key when clicking a column name.
This table search allows for regular expressions,
so it’s easy to search for gray a-y and grey e-y, with a dot for “any letter”..
You can see that some colors are repeated for a-y and e-y. So while there are 657 color entries in
R’s built-in colors, there aren’t actually 657 unique colors. Who knew?
But let’s get back to blues. There’s also a way to search for “colors somewhat like this
specific color”. I discovered this when running the base R color demo.
I didn’t find these displays of built-in colors to be especially useful (that’s why
I made my own searchabe table). But look at this!
“Consider choosing a color by looking in the neighborhood of one you know.” That’s useful!
If you look at the code in this bottom left RStudio pane,
there are two functions generating this plot: nearRcolor() and plotCol().
Here are some blues – you can argue just how blue they are, but they also have names like
“cyan” and “turquoise”, so you can’t search for them in the table by looking just for “blue”.
Hopefully you get the idea. I didn’t see either of those functions in main base R
without running the colors demo. So I pulled code for both of them into my new rcolorsutils package.
If I restart my R session
and try to run the nearRcolor() function you see the function isn’t available anymore,
it’s not built into R. I don’t want to keep having to run the colors demo to access it,
which is why I added it to my colors package. I’ll load rcolorutils and then run the function.
You see I get a list of “nearby” colors. I can plot those with plotCol(. I
can set the number of rows for plotCol() so they’re not all in a single row.
If I look for colors near “blue” I don’t get much.
I can tweak that by setting an rgb distance – I typically fiddle around
with the distance number until I get roughly the number of colors I want – you can see some blues.
The scales package has a nice function for plotting colors also, show_col()
The last topic I’ll cover is R color palettes.
There are a few built-in color palettes in R, but probably the most popular
come from the RColorBrewer and viridis packages. You can install both from CRAN.
If you also install the tmaptools package, you’ll get a great built-in app for exploring
both RColorBrewer and viridis palettes, palette_explorer().This is a very nice app,
letting you to choose number of colors and then you see all available palettes.
Plus it gives you sample code for generating the palettes, as you can see below each
palette color group. And it even has a color blindness simulator at the bottom right.
These may be all the palettes you’ll ever need. But if you’re looking for more variety,
there are other R packages with pre-made palettes. There’s harrypotter.
Gameofthrones. IslamicArt. National parks. It can be hard to keep track of them . . . . which
is what the paletteer package has done for us. paletteer includes more than 2,000 palettes
from 59 packages and classifies them into three groups: discreet, continuous, and dynamic.
I find it a bit difficult to keep track of more than 2,000
– or even to look at them in a long list. So, I made a Shiny app to see them by category.
You can download the code for this app at the InfoWorld article that accompanies this video!
The app lets you search for palettes by category: continuous, discreet, or dynamic. Then pick your
type: if you want colors that diverge, that are in sequence, or are qualitative without any sort
of order. These palette classifications come from the paletteer package and a few of them might not
be exact, so I tend to look at all three types to make sure I’m not missing anything I might like.
Under each color image is code for how to use the palette.
The first line of code shows how to access the vector of hex codes in the palette
If you want to use these palettes with ggplot, paletteer comes with scale_fill
and scale_color functions, along with a few others.
Here’s a quick example with some toy data. First let’s look at ggplot’s default
colors. Now let’s see it with a scale_fill_viridis() palette.
And now with a scale_fill_paletteer() palette.
The Shiny app includes scale_fill and scale_color
functions for a palette.
Last up: Making your own palette. Let’s say these are my company’s primary website colors.
I can view them with scales show_col() function.
I can use these colors in ggplot with ggplot’s scale_fill_manual() function, but wouldn’t it be
cool to have my own scale_fill() function? The paletti package makes it very easy to do that!
Load the package. By the way, it has its own color visualization function,
viz_palette(), which you can see.
Now here’s how it works. First run the get_pal()
function on your vector of colors to create a palette from them. Then run
either get_scale_fill() or get_scale_color() on the results to turn that into a ggplot function.
Now I can use my new scale_fill_my_palette() function in ggplot.
And there you have it! I hope found at least one useful tip or tool for working with colors in R.
That’s it for this episode, thanks for watching! For more R tips, head to the
Do More With R page at bit-dot-l-y slash do more with R, all lowercase except for the R.
You can also find the Do More With R playlist on YouTube’s IDG
Tech Talk channel -- where you can subscribe so you never miss an episode.
Hope to see you next time. Stay healthy and safe, everyone!