Color Packages
R has lots of built in colors that you can call by name, but there are also color packages that help you find nice palettes. Each package tends to have multiple “color ramps” (transitions from one color to another) to choose from. We’ll look at how to incorporate a couple.
{viridis}
{viridis} is one of the most popular color packages, especially for adding its colorblind-friendly palettes. You can see all of its color ramps here, but we’ll use the one that the package is named for, so “viridis”.
We’ll start with a basic graph with R’s autoselected colors. Here the color value is just dependent on the x value.
The way the {viridis} commands work is just to add a variant of scale_color_viridis() or scale_fill_viridis(), depending on if you are coloring lines and points or filling in bars.
For discrete data you would use scale_color_viridis_d(). For continuous data you would use scale_color_viridis_c(), which is what we have, so we’ll add that to the code.
This color ramp of dark purple to yellow are the basic “viridis” color palette, but you can see from their site, they have many color palettes to choose from. We can change to another by adding an option argument.
We can do the same thing with discrete data. Here’s code for a bar plot with R’s default color choices.
Use the correct viridis code to change this to the “mako” palette.
{RColorBrewer}
Another popular color package is {RColorBrewer}. The commands for it work in essentially the same way as {viridis} except with scale_color_brewer() or scale_fill_brewer(). You can see all of the options for RColorBrewer palette’s here. The palettes are discrete colors, not color ramps, so they only work with discrete data.
Here’s the same plot with a palette from {RColorBrewer}.
{wesanderson}
Lastly, here’s a fun one: a package someone made of palettes from Wes Anderson movies. Here’s the site to see the color palettes from each movie (scroll down).
The command wes_palette() works inside scale_fill_manual() or scale_color_manual(). Note that each palette only has five colors, so you can’t use this one on datasets where you have many unique variables.