Graph Colors and Themes

Colors!

Congratulations, you have now reached the most fun part of R: colors. R gives you full control over the appearance of your data visualizations, so you are not stuck with the default color choices. In fact, there are many packages that are devoted just to having the best looking color pallete. If you are someone who spends as much time picking the colors for your graph as you did making the graph itself (like me), you will love the level of customization you can do in R.

Using Color to Add Complexity

We’ve already done this a little with our boxplots with the extra fill value. But in addition to fill you can use color to separate out different groups within other types of plots. fill is used when you want to fill in a shape, so it’s good for boxplots and bar graphs. color is for lines and points, so it’s good for scatterplots and line graphs.

Let’s remake the penguin bill scatterplot and color by species.

Customizing Colors

There are a lot of different ways to change colors in R and you can do any color you would like because R recognizes color hex codes. But for a simpler way to get started changing colors, here’s a PDF that shows all of the built in colors that R knows by name, including such goofball options as “mediumorchid”, “seashell” and “peachpuff”.

There are numerous packages made just to help you get your colors right. Here are a few:

  • {viridis}: a popular choice for many different color scales

  • {RColorBrewer}: another good one with a lot of preset palettes to choose from

  • {wesanderson}: a package for theming your colors by Wes Anderson movie palletes (scroll down on site to see all the options)

Manual Coloring

To add on to ggplot code all we need to do is add another + and then our new line.

The first example we’ll do is to add the command scale_color_manual to change the default colors. Note that I need to pick as many colors as there are values in the legend, so here I need to pick three. And I have to use the c() function to put the colors into a list. So my code is as follows:

We can do a similar command to pick the fill colors that we want. Here’s our same code from before that made a boxplot. You can add scale_fill_manual in a similar fashion that we did scale_color_manual to change the colors.

ggplot(data = penguins, aes(x = species, y = body_mass_g, fill = species)) +
  geom_boxplot() +
  scale_fill_manual(values = c("firebrick", "seagreen", "dodgerblue"))

You can also see what would happen if you chose color for the boxplot instead of fill:

You can even do both with different color palettes:

Themes

We can also add a line of code to change the hideous gray background. We do this by changing the theme. I like either theme_bw() or theme_classic(), but you can see all the background options here. Try out different ones in place of theme_classic().

You can also change whether or not grid lines show with either of these lines of code:

Try the same thing with the following code to remove major grid lines or have both lines to remove all the gridlines.

# remove major grid lines
theme(panel.grid.major = element_blank())