28 Themes and Palettes
This chapter originated as a community contribution created by ar3879
This page is a work in progress. We appreciate any input you may have. If you would like to help improve this page, consider contributing to our repo.
Our graphs have to be informative and attractive to the audience to get their attention. Themes and colors play an important role in making the graphs attractive.
This section covers how we can set different palettes and themes to suit the context and to make them look cool.
28.2 ggplot2 themes
ggplot2, we do have a set of themes, which we can set. A brief description of them is as follows:
theme_bw(): dark on light
theme_linedraw(): uses black lines on white backgrounds only
theme_light(): similar to
linedraw()but with grey lines as well
theme_dark(): lines on a dark background instead of light
theme_minimal(): no background annotations, minimal feel
theme_classic(): theme with no grid lines
theme_void(): empty theme with no elements
28.2.1 ggplot2 theme example
<- ggplot(subset, aes(x = clarity, y = carat, color = cut)) + q geom_point(size = 2.5,alpha = 0.75) + theme_minimal()q
There are several other packages available that set the themes and colors in many ways. We will discuss 4 of them.
Often, we find ourselves looking for the colors which make our graph look clear and cool.
RColorBrewer offers a number of palettes, which we can use based on the context of our graph. There are three categories of these palettes: Sequential, Diverging, and Qualitative
<- ggplot(subset, aes(x = clarity, y = carat, color = cut)) + q geom_point(size = 2.5, alpha = 0.75)
- Sequential Palette: It represents the shade of the color from light to dark. It is usually used to represent interval data where low values can be shown with a light color and high values can be shown with a dark color. For instance –Blues, BuPu, YlGn, Reds, OrRd
+ scale_colour_brewer(palette = "Blues")q
- Diverging Palette: It has darker colors of contrasting hues on both the ends, and lighter color in the middle. For instance –Spectral, RdGy, PuOr
+ scale_colour_brewer(palette = "PuOr")q
- Qualitative Palette: It is usually used when we want to highlight the differences in the classes (categorial variables). For instance –set1, set2, set3, pastel1, pastel2 , dark2
+ scale_colour_brewer(palette = "Pastel1")q
ggthemes provides additional geoms, scales, and themes to
ggplot2. Some of them are really cool! We can change the theme and color of a graph based on the context.
<- ggplot(subset, aes(x = clarity, y = carat, color = cut)) + g1 geom_point(size = 2.5,alpha = 0.75)
28.4.1 ggthemes examples
+ theme_economist() + scale_colour_economist()g1
+ theme_igray() + scale_colour_tableau()g1
+ theme_wsj() + scale_color_wsj()g1
+ theme_igray() + scale_colour_colorblind()g1
If we would like to use these colors in the graphs, which may not support using
ggthemes, we can use the
scales package to know what colors were used for a given palette. For example:
ggthemr is used to set the theme of ggplot graphs. It has 17 different themes to change the way ggplot graphs look. Use of ggthemr is different from other other packages. We set the theme before using it.
28.5.1 ggthemr examples
ggthemr("sky") ggplot(subset, aes(x = clarity, y = carat, color = cut)) + geom_point(size = 2.5, alpha = 0.75)
ggthemr("flat") ggplot(subset, aes(x = clarity, y = carat, color = cut)) + geom_point(size = 2.5, alpha = 0.75)
Interestingly, we can set more parameters to change the themes:
ggthemr("lilac", type = "outer", layout = "scientific", spacing = 2) ggplot(subset, aes(x = clarity, y = carat, color = cut)) + geom_point(size = 2.5, alpha = 0.75)
ggsci offers a number of palettes inspired by colors used in scientific journals, science fiction movies, and TV shows. For continous data,
scale_fill_material(colname) is used, and for discrete data,
scale_fill_palname() are used.
28.6.1 ggsci for discrete data
# we need to remove the theme set previously if we don't want to use it anymore ggthemr_reset() <- ggplot(subset, aes(x = clarity, y = carat, color = cut)) + g1 geom_point(size = 2.5, alpha = 0.75) + scale_color_startrek()g1
28.6.2 ggsci for continuous data
ggplot(diamonds, aes(carat, price)) + geom_hex(bins = 20, color = "red") + scale_fill_material("orange")
We can also find out the color used, so that we can use them in some other graphs created in base R:
= pal_lancet("lanonc", alpha = 0.7)(9) palette show_col(palette)