- Colored 1 2 2 – Create Color Palettes Palette Palettes
- Color Palettes That Work Well Together
- Color Wheel Palette Generator
Plotting with color in R is kind of like painting a room in your house: you have to pick some colors. R has some default colors ready to go, but it’s only natural to want to play around and try some different combinations. In this post we’ll look at some ways you can define new color palettes for plotting in R.
Browse thousands of color combinations on Canva and create your own designs without hiring a designer. The easiest place to get colors from your photos. Want a color scheme that perfectly matches your favorite images? With Canva’s color palette generator, you can create color combinations in seconds. Simply upload a photo, and we’ll use the hues in the photo to create your palette.
Palette Number 4 Of Color Palettes, Check It Out. Thousands of trendy color palettes in a curated collection that is updated daily. Get a fresh color palette for your next design project and save all the palettes.
To begin, let’s use the
palette
function to see what colors are currently available:We have 8 colors currently in the palette. That doesn’t mean we can’t use other colors. It just means these are the colors we can refer to by position. “black” is the first color, so the argument
col=1
will return black. Likewise, col=2
produces “red” and so on. Let’s demonstrate by plotting 8 dots with the 8 different colors. Setting cex=3
makes the dots 3 times their normal size and pch=19
makes solid dots instead of the default open circles:The
palette
function can also be used to change the color palette. For example we could add 'purple'
and 'brown'
. Below we first save the current color palette to an object called cc, and then use the c()
function to concatenate cc with purple and brown:Colored 1 2 2 – Create Color Palettes Palette Palettes
If we want to revert back to the default palette, we can call
palette
with the keyword “default”:How do we know what colors are available for our palette? We can use the colors function to see. Try it! It will list all 657 colors. Below we show the first 20:
We can use these colors by name if we like. For example, here’s a scatterplot of the cars data that come with R using the color “aquamarine3”:
The Stowers Institute for Medical Research provides a handy chart that shows all available R colors: http://research.stowers.org/mcm/efg/R/Color/Chart/ColorChart.pdf
Trying to choose good colors out of 657 choices can be overwhelming and lead to a lot of trial and error. Fortunately a great deal of research has been done on plotting and color combinations and there are several tried-and-tested color palettes to choose from. One R package that provides some of these palettes is RColorBrewer. Named for the creator of these color schemes, Cynthia Brewer, the RColorBrewer package makes it easy to quickly load sensible color palettes.
The RColorBrewer package does not come with R and needs to be installed if you don’t already have it. Once loaded, it provides functions for viewing and creating color palettes.
RColorBrewer provides three types of palettes: sequential, diverging and qualitative.
- Sequential palettes are suited to ordered data that progress from low to high.
- Diverging palettes are suited to centered data with extremes in either direction.
- Qualitative palettes are suited to nominal or categorical data.
The available palettes are listed in the documentation. However the
display.brewer.all
function will plot all of them along with their name. In the graph below we see the sequential palettes, then the qualitative palettes, and finally the diverging palettes.To create a RColorBrewer palette, use the
brewer.pal
function. It takes two arguments: n
, the number of colors in the palette; and name
, the name of the palette. Let’s make a palette of 8 colors from the qualitative palette, “Set2”.Notice the
brewer.pal
function by itself just displays the palette. Also notice the colors are expressed in “hexadecimal triplets” instead of color names. To load the palette we needed to use the palette
function. These are now the colors R will use when referencing color by number. For example:What about ggplot2? Changing color palettes works differently for ggplot2. Let’s make a quick plot in ggplot using the iris data that come with R and see what the default colors look like.
Clearly these are not the colors in our current color palette. It turns out ggplot generates its own color palettes depending on the scale of the variable that color is mapped to. In the above example, color is mapped to a discrete variable, Species, that takes 3 values. We would call this a qualitative palette and it works well for these data. Let’s map color to a continuous variable, Sepal.Width:
Notice the palette changed to a blue palette that gets progressively lighter as values increase. This is actually a smooth gradient between two shades of blue.
To change these palettes we use one of the
scale_color
functions that come with ggplot2. For example to use the RColorBrewer palette “Set2”, we use the scale_color_brewer
function, like so:To change the smooth gradient color palette, we use the
scale_color_gradient
with low and high color values. For example, we can set the low value to white and the high value to red:Color Palettes That Work Well Together
Now what if there’s a color palette in ggplot that we would like to use in base R graphics? How can we figure out what those colors are? For example, let’s say we like ggplot’s red, green, and blue colors it used in the first plot above. They’re not simply “red”, “green” and “blue”. They’re a bit lighter and softer.
It turns out ggplot automatically generates discrete colors by automatically picking evenly spaced hues around something called the hcl color wheel. If a color is mapped to a variable with two groups, the colors for those groups will come from opposite sides of the color wheel, or 180 degrees apart (360/2 = 180). If a color is mapped to a variable with three groups, the colors will come from three evenly spaced points around the wheel, or 120 degrees apart (360/3 = 120). And so on.
Looking at the documentation for the
scale_color_discrete
function tells us where on the hcl color wheel ggplot starts picking the color: 15. This known as the h value, which stands for hue. The c and l values, which stand for chroma and luminance, are set to 100 and 65. For three groups, this means the h value are 15, 135 (15 + 120), and 255 (15 + 120 + 120). Now we can use the hcl function that comes with R to get the associated hexadecimal triplets:And we can use the
palette
function to add these colors to the color palette:Now we can make a base R plot with ggplot2 colors. For example, here’s the
scatterplot
function from the car package plotting the iris data with ggplot2 colors.Finally, it’s relatively straight forward to write a function to generate ggplot2 colors based on the number of groups. Below we first determine the distance between points by dividing 360 by g, the number of groups. Next we determine the actual points on the circle by starting with 15 and cumulatively adding the distance. Finally we call the
hcl
function to get our colors. Of course the function could be made more robust by allowing the c and l values and the starting point on the color wheel to be varied. But this function works fine if you’re happy with the default ggplot2 colors for discrete variables.Color Wheel Palette Generator
For questions or clarifications regarding this article, contact the UVA Library StatLab: [email protected]
View the entire collection of UVA Library StatLab articles.
Clay FordStatistical Research Consultant
University of Virginia Library
June 10, 2016