0% found this document useful (0 votes)
104 views7 pages

6 Using Colors in Ggplot PDF

Greg Martin discusses using colors effectively in ggplot. He explains that color can convey meaning in data visualizations and that ggplot will automatically assign colors that may not suit the objectives. The document then provides examples of using color palettes from RColorBrewer, manually specifying colors by name or hex code, and mapping colors to variables to customize the colors in a ggplot.

Uploaded by

Yerel Paulino
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
104 views7 pages

6 Using Colors in Ggplot PDF

Greg Martin discusses using colors effectively in ggplot. He explains that color can convey meaning in data visualizations and that ggplot will automatically assign colors that may not suit the objectives. The document then provides examples of using color palettes from RColorBrewer, manually specifying colors by name or hex code, and mapping colors to variables to customize the colors in a ggplot.

Uploaded by

Yerel Paulino
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

Using colors in ggplot

Greg Martin

Using colors

Color has the power to convey emotions, highlight patterns, and bring attention to critical
insights in your dataset. Using color creatively, you can give your data visualization the “look
and feel” that fits with either your message or branding.

An easy start

Whenever you create a plot that requires colors, R will automatically assign a set of colors
that may or may not suit your data visualization objectives. If it does, then so be it. But
more often than not, you’ll want to bring a little of your own personality and style onto the
canvas.
Of course you can specify exactly the color of each element of your plot (and we’ll discus that
below). A first step might be to simply select a range of colors from a palette that are known
to work well together. ColorBrewer is a collection of color palettes that you can chose from
and have applied to any data visualization. You’ll need to install and call the RColorBrewer
package with the following code:

install.packages("RColorBrewer")
library(RColorBrewer)

You can easily view the colorblind friendly palettes with the code:

display.brewer.all(colorblindFriendly=TRUE)

1
Each palette has a name and we’ll be using those names in our ggplot code to set the colors
used in any given plot using the scale_color_brewer function and then setting the palette
argument to the name of the palette that you want to use. Take a look:

2
mpg %>%
ggplot(aes(displ, hwy, color = drv))+
geom_jitter(size = 5)+
scale_color_brewer(palette = "Set2")+
theme_minimal()

40

drv
30 4
hwy

f
r

20

2 3 4 5 6 7
displ

Not that in the code above we’ve used the function scale_color_brewer because the color
aesthetic is being mapped to a variable. For many geometries (like geom_bar or geom_density),
it is the fill aesthetic that is mapped against the variable (as a shape is being filled up). In
these cases we use scale_fill_brewer instead.

Take control

You can of course specify the color of an element directly by using a color name; here is an
example:

3
mpg %>%
ggplot(aes(displ, hwy))+
geom_jitter(color = "steelblue",
size = 5)+
theme_minimal()

40

30
hwy

20

2 3 4 5 6 7
displ

Be specific

As you’ve seen, if the color of the elements of a plot are to be allocated by the values of a
variables (through the process of mapping that we’ve discuss), then ggplot will assign a color
automatically. Can the colors that get assigned by specified? The answer is of course, yes.
Take a look:

4
mpg %>%
ggplot(aes(displ, hwy, color = drv))+
geom_jitter(size = 5)+
scale_color_manual(values =
c("4" ="darkblue",
"f" = "darkred",
"r" = "darkgreen"))+
theme_minimal()

40

drv
30 4
hwy

f
r

20

2 3 4 5 6 7
displ

Be even more specific

Can you use hex code to specify exactly what colors you’d like to use? Again, the answer is
yes. To find the right code, simply do an internet search for “hex code” that will give you any
number of webpages that will let you select from a palette and provide you with hex codes for
the exact colors that you want to use. Take a look:

5
mpg %>%
ggplot(aes(displ, hwy, color = drv))+
geom_jitter(size = 5)+
scale_color_manual(values =
c("4" = "#719AC9",
"f" = "#75B99C",
"r" = "#C98D71"))+
theme_minimal()

40

drv
30 4
hwy

f
r

20

10
2 3 4 5 6 7
displ

6
More help

For more help and support, click on the title (hyperlink) or scan any of the QR codes below.

Using color Wrangle your data video Visualize your data video

Course on how to Course on statistical


Course on data analysis visualize data analysis

You might also like