Skip to Content

How to specify colors in ggplot2?


ggplot2 is a popular R package for creating elegant and customizable data visualizations. One of the most common tasks when using ggplot2 is specifying colors for different plot elements like points, lines, bars, etc. Controlling colors allows you to highlight patterns in your data, encode categorical variables, and improve the overall aesthetics of your plots. Thankfully, ggplot2 provides many different ways to map colors to data values and plot features. In this comprehensive guide, we will explore the various methods and options for specifying colors in ggplot2 plots.

Setting Default Color Palettes

ggplot2 has several built-in color palettes that serve as defaults when no explicit color mapping is defined. The core ggplot2 package includes palettes like “tableau20”, “colorbrewer”, “grey”, and more. You can set a global default color palette like this:

“`r
ggplot2::theme_set(theme_set(theme_grey()))
“`

This will apply the grey scale color palette globally to all your plots. You can also set defaults on a plot by plot basis with the scale_color_brewer() and scale_fill_brewer() functions.

Using Colorbrewer Palettes

The colorbrewer R package provides many nice sequential, diverging and qualitative color palettes that work well for data visualization. You can easily access these palettes by passing a palette name to scale_color_brewer() or scale_fill_brewer().

“`r
ggplot(data, aes(x, y, color = z)) +
geom_point() +
scale_color_brewer(palette = “Blues”)
“`

Some commonly used ColorBrewer palette names are “Blues”, “BuGn”, “YlOrRd”, “Spectral”, etc. See the full list of palettes.

Specifying Colors by Hex Code

For full control over colors, you can directly specify hex color codes like “#FF0000” for red. Pass a vector of hex codes to scale_color_manual():

“`r
ggplot(data, aes(x, y, color = z)) +
geom_point() +
scale_color_manual(values = c(“#FF0000”, “#00FF00”, “#0000FF”))
“`

You can find hex codes for millions of colors on sites like Color-Hex.

Mapping Colors to Data Values

One of the most powerful features in ggplot2 is mapping plot colors to actual data values and variables. This allows color encoding of information. For example, you can map color to a categorical variable:

“`r
ggplot(data, aes(x, y, color = category)) +
geom_point()
“`

Or to a continuous numeric variable:

“`r
ggplot(data, aes(x, y, color = value)) +
geom_point() +
scale_color_gradient(low=”yellow”, high=”red”)
“`

The color mapping will be handled automatically based on the data types.

Creating Custom Color Scales

You have full control over the color mapping by defining custom color scales with scale_color_manual() and scale_fill_manual(). With these, you provide a vector of color values mapped to data values.

For example:

“`r
vals Specifying Colors by Name

R has a set of built-in color names you can use like “red”, “blue”, “green”. Pass these names directly to color aesthetics:

“`r
ggplot(data, aes(x, y)) +
geom_point(color = “purple”)
“`

Or in scales:

“`r
scale_color_manual(values = c(“yellow”, “green”, “blue”))
“`

The full list of color names in R can be found by running colors(). These provide an easy way to access common color terms.

Using Alpha Transparency

You can add transparency to colors in ggplot2 by specifying an alpha value between 0 (fully transparent) and 1 (fully opaque). An alpha of 0.5 gives partial transparency.

To set transparency on all plot items, use the alpha argument:

“`r
ggplot(data, aes(x, y)) +
geom_point(alpha = 0.2)
“`

For transparency mapped to a variable, use scale_alpha():

“`r
ggplot(data, aes(x, y, alpha = z)) +
geom_point() +
scale_alpha(range = c(0.2, 1))
“`

Specifying Fill Colors

In addition to colors for points, lines, etc. you can control fills for bars, polygons, boxplots and other goemetric objects that have interior fills:

“`r
ggplot(data, aes(x, y, fill = category)) +
geom_bar(stat = “identity”)
“`

All the above color specifications work for fill as well using the fill aesthetic and functions like scale_fill_brewer(), scale_fill_manual(), etc.

Controlling Legend Colors

By default, the color legend will display each color scale used on the plot. You can override the legend colors manually without affecting the actual plot colors:

“`r
scale_color_manual(
values = c(“red”, “blue”, “green”),
guide = “legend”,
labels = c(“Group 1”, “Group 2”, “Group 3”),
legend = c(“1” = “yellow”, “2” = “purple”, “3” = “orange”)
)
“`

This maps yellow, purple and orange colors to the legend keys and labels.

Changing Color Hue

An easy way to create a sequence of colors is to start with a single color and then vary the hue. The scale_color_hue() function makes this easy:

“`r
ggplot(data, aes(x, y, color = z)) +
geom_point() +
scale_color_hue()
“`

By default this cycles through hues around the color wheel. You can specify start and end positions, and direction:

“`r
scale_color_hue(h = c(0, 360) + c(90, 180))
“`

Colorblind Friendly Palettes

To avoid issues for colorblind readers, choose qualitatively distinct colors and avoid excessive use of red/green combinations. Packages like viridis provide colorblind friendly palettes:

“`r
scale_color_viridis()
“`

There are also tools to simulate color vision deficiency and check plots.

Conclusion

ggplot2 provides enormous flexibility and control for mapping data to colors in your plots. By combining the various color specification methods – like built-in palettes, custom hex codes, data-driven mapping, transparency, and hue manipulation – you can create precisely the color encodings needed for your particular data visualization. The key is linking color choices directly to the underlying data and visual encoding goals. With practice, you’ll develop intuition for color choices that highlight patterns and meaning.

Example Data Tables

Category Value
A 10
B 20
C 30
Name Color Hex Code
Red #FF0000
Green #00FF00
Blue #0000FF

This article provides an overview of the main methods for specifying colors in ggplot2 data visualizations in R. It includes code examples demonstrating different color palettes, use of hex codes, data-driven color mapping, custom color scales, transparency, and more. The data tables give simple examples of categorical and color hex code data that could be used in example plots.