How to make your own color palettes in ggplot
One of the great things about creating data viz with ggplot is that you can create color palettes that match your or your clients’ branding. We’ve written about how we do this in our consulting work. But the way we work is fairly complicated and I was asked recently for a simpler solution to making custom ggplot color palettes.
I recorded a video to show how to make three different types of palettes
- Qualitative (i.e. categorical)
- Sequential (going from a low to a high value)
- Diverging (going from low to high with a midpoint)
As you’ll see in the video, there are a few steps to this process. I begin by defining colors that I want to use:
Qualitative Color Scale
To make a qualitative color scale, I used the colors I defined in combination with the scale_color_manual()
function.
Sequential Color Scale
Next, I made a sequential color scale. I adapted the scale_fill_gradient()
function to make this function.
Diverging Color Scale
Finally, I made a diverging color scale. To do this, I adapted the scale_fill_gradient2()
function, which gives me the ability to set a medium color, in addition to the high and low colors.
Full Code
I’ve included the full code used in the video below. You can see where I get the data for the map of North Carolina that I use and run the code yourself to see how it works.
A Couple Caveats
This post has shown how to make custom color scales. What I haven’t done is talked about choosing good colors for you color scales. If you want to read more on that, check out:
- This blog post written by R for the Rest of Us consultant Cara Thompson
- The Glamour of Graphics course, which has an entire section on color
- This incredibly comprehensive article by Lisa Charlotte Muth on choosing colors for data viz
The other important thing to note is that it’s important to consider colorblindness when creating color palettes. Again, to keep things focused, I haven’t done that in this blog post. However, there is a great R package called colorblindr
to help you ensure your custom ggplot color palettes are accessible to all users.
Good luck making your own custom ggplot color palettes!
Have any questions? Put them below and we will help you out!