From the course: R Essential Training: Wrangling and Visualizing Data
From the course: R Essential Training: Wrangling and Visualizing Data
Using color palettes
“
- [Instructor] You can save yourself some time and get a more cohesive presentation by using color palettes or collections of colors that you can use for your graphics in R. I want to show you several different ways that you can do this. I'm going to start by loading a few packages. Pacman and then I'm going to load the datasets and tidyverse and so on. And I'm going to use the same tiny dataset, only six numbers that I use in the colors demonstration and let's get the default bar plot. There it is, the gray, which is not bad for your own personal exploration. But you can also use palettes. Now, I want to start by getting a little bit information about palettes by doing question mark palette. And then you see here, we've got some help information and let's see what the current palette is. I just say palette, then open and close parentheses. And it tells me it's black, red, green3 and so on. Now, we can use that set of colors to make our bar chart. So I'm actually going to come here and do barplot x and then I'm going to specify col for color and then the one colon six means use the first six colors. And so when I run that command, now you see I have them. There's the black, red, that's green3, blue, cyan and magenta. But R has several other built-in color palettes. They're rainbow, heat.colors, terrain.colors, topo.colors and cm.colors and let me show you how each of these work. All you need to do is call the palette by name and then in parentheses, say how many colors you need out of it. Now, I only have six bars so I'm going to use six in each case. So let's do rainbow. I'm going to run that one. That's the rainbow palette. Heat.colors, I also want six. There's those, you see it's sequential. And then terrain.colors and then topo.colors, like you might see on a topographical map. And cm. That goes from pink to blue and so those are the built-in palettes but you've got more choices than that. For instance, you can go to a very popular one called RColorBrewer. Now, this comes from a website called ColorBrewer. Let's take a quick look at that. So this is ColorBrewer. It's called color advice for cartography. And here you can choose several different color schemes. You can do the number of classes. Here's five, you can do a single hue. You can choose diverging. Or you can do qualitative, you can try to look for things that are good for colorblind. We don't have any there in qualitative but we do for divergent. Or photocopy safe and let's pick one that will show up at different values on a black and white photocopy. So there's a lot of choices and you see, it gives you the hex codes here or if you click right here, it'll give you the RGB triplets on a zero to 255 scale. So you can use this as a great way of seeing what sorts of combinations might work for you but it's even easier because there's a package in R that's developed specifically to use these color palettes. So let's go back to R. And let's load ColorBrewer, excuse me, and let's load RColorBrewer. And then let's get a little bit of information on it. I'm going to do question mark, RColorBrewer and we have some information here. But let me show you the palettes themselves. I'm going to use display.brewer.all and when I do that, we get something that's really hard to see what it is on a small screen but let's zoom in. Well, these are the palettes and what we have are sequential and diverging and qualitative palettes. But let's zoom back out from that one. The sequential palettes, say for instance, we can get an example by looking at this one, BuPu, and ask for seven different colors. When I run that command, here we go from sort of blue to purple. So that's sequential, you can tell you're getting more and more of something. Diverging mean you started a central value and you're indicating sort of negative values and positive values. So here's a five-color diverging one. And then qualitative is when you're simply trying to indicate different groups or categories. So here's one with four that you can easily tell apart. To use the RColorBrewer palettes in your graphs, all you need to do is say col for color and then specify brewer.pal, which is short for palette, and then how many colors you want and then the name of the palette. It's a short name. So let's use this first one. A sequential palette. And then here's a diverging palette. And then here is the qualitative palette. And so any of these, and you've got a lot of choices. It can be a great way of adding a little more interest to graphs as well as guiding the eye, making it easier to see what's happening. Well, RColorBrewer's probably the most popular color palette package for R. There are others. One that I like, 'cause I think it's amusing, is wesanderson. And if we click on this link, you'll see that Wes Anderson is named after the film maker Wes Anderson who's made a number of movies like "Bottle Rocket" and "The Royal Tenenbaums" and "Rushmore." In fact, now, if we come back here, we can load the package. You get some information, it just tells you that it exists but let's see the names of the palettes. There are all the palettes from BottleRocket through IsleofDogs. And if you want to use those, you just use wes_palette and then the name of the palette and the color command. So here we have the BottleRocket and Zissou1 and GrandBudapest1 and IsleofDogs1. You'll see also that it repeats the colors because not many of these palettes have six colors in them but that's what R does any time you have a smaller number of colors compared to the number of things you're representing. Now, there are some other color palette options. For instance, there's this one right here, which is really handy for getting palettes that work with colorblindness and for photocopying and gray scale. You've got a number of choices right here. And then there's also a collection of scientific journal and sci-fi themed color palettes at this option. And when you open that, you see we have for the Lancet and for JAMA and the University of Chicago. We also have Star Trek, Tron, Futurama and Simpsons palettes and so you have a number of options here. I want to finish by pointing out one other thing. Not only do you have these existing palettes but maybe you work some place that has specific color requirements. You have an organization, the company you work for and they have their template. Well, it's very easy to specify a custom template. All you need to do is save the colors into an object in memory. For instance, I'm going to make one called palette1 and I'm just saving a list of the color names. I'm putting them together with c, which is for concatenate and I can save those into memory and you see my palette now shows up right here. Or you can use any other method that R gives you for indexing numbers, like the hex codes. So here are the same colors in hex code, just in reversed order and then simply call that palette under the color command. So here's the first palette and then here it is flipped around in order. With that, you've got an enormous amount of control, both to get a cohesive presentation to draw attention, guide the eye and hopefully help both you and the people you're working with see what's happening in your data, get some insight and do something useful with it.
Contents
-
- Make your data make sense 1m 27s
- Using the exercise files 48s
-
- R in context 6m 46s
- Data science with R: A case study 11m 46s
-
- Installing R 1m 25s
- Environments for R 3m 31s
- Installing RStudio 1m 17s
- Navigating the RStudio environment 6m 4s
- Entering data 7m 5s
- Data types and structures 12m 24s
- Comments and headers 4m 59s
- Packages for R 4m 46s
- The tidyverse 3m 4s
- Piping commands with %>% 4m 33s
-
- R's built-in datasets 4m 58s
- Exploring sample datasets with pacman 6m 41s
- Importing data from a spreadsheet 5m 39s
- Importing XML data 5m 32s
- Importing JSON data 5m 40s
- Saving data in native R formats 6m 50s
-
- Introduction to ggplot2 4m 39s
- Using colors in R 5m 3s
- Using color palettes 8m 5s
- Creating bar charts 9m 22s
- Creating histograms 5m 30s
- Creating box plots 5m 24s
- Creating scatterplots 5m 58s
- Creating multiple graphs 4m 6s
- Creating cluster charts 8m 34s
-
- Creating tidy data 9m 46s
- Using tibbles 4m 51s
- Using data.table 4m 57s
- Converting data from wide to tall and from tall to wide 4m 13s
- Converting data from tables to rows 5m 2s
- Working with dates and times 6m 20s
- Working with list data 5m 13s
- Working with XML data 5m 22s
- Working with categorical variables 6m 29s
- Filtering cases and subgroups 7m 32s
-
- Recoding categorical data 9m 46s
- Recoding quantitative data 7m 10s
- Transforming outliers 8m 49s
- Creating scale scores by counting 5m 35s
- Creating scale scores by averaging 3m 26s
-
- Next steps 2m 9s