Using Color to Add Complexity

Here’s our code for the scatterplot again. If we want to color the points for different species different colors, we add to the aes() function. To select what to color by just add the argument color = factor, where factor is whatever variable you want represented by color.

Here we want to separate the species by color, so our code would be:

Adding color here is very important. From the black and white graph, you may have gotten the impression that there was no trend, or even a downward trend, but with color we can see that the trend is actually positive within each species.

color Versus fill

The color argument is useful for coloring points and lines. If you want a box or bar to have color, then you would use the fill option, which does the same thing as color.

See if you can add code to make a boxplot of body_mass_g separated by species and have the species boxplots be different colors.

It’s okay to have both x = species and fill = species.

We can add even more complexity to this plot by changing fill to be a different variable. Change it to be sex and see what happens.

Colors!

Congratulations, you have now reached the most fun part of R: the 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.

There are a lot of different ways to change colors in R, and you can pick any color you 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”.

Manually Changing Colors

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

See if you can figure out how you would change that code to get custom colors for the scatterplot:

Additional Color Packages

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)

Note that any time you want to use functions from a different package you will first need to run library(package_name) in order to load the functions.