In R we can specify a color in several ways:
"blue"
). Type colors()
to obtain a list of available color names. https://www.nceas.ucsb.edu/sites/default/files/2020-04/colorPaletteCheatsheet.pdf"#E36D11"
). #
followed by
6 digits ranging from 0 to F and where first two digits represent
redness, second two greenness, and last two blueness. https://htmlcolorcodes.com/rgb()
specifying three numbers between 0 and 1 to get
hex code (e.g., rgb(.75, 0, 1)
returns “#BF00FF”)When we map a variable to color
or fill
,
ggplot2
uses the type of variable (i.e., numeric, factor)
to choose a color scale.
We can change default by colors using colors from packages such as
RColorBrewer
or viridis
or by manually
changing the colors.
RColorBrewer
package uses ColorBrewer sequential (e.g.,
Blues, YlOrRd), diverging (e.g., RdGy, Spectral), or qualitative scale
(e.g., Accent, Dark2) colors http://colorbrewer2.org
Default for continuous variables is a color gradient that runs from a
blueish-black to light-blue (scale_color_gradient()
).
Other possible color scales include
scale_color_distiller()
,
scale_color_fermenter()
,
scale_color_viridis()
, scale_color_gradient()
,
scale_color_gradient2()
and
scale_color_gradientn()
.
library(ggplot2)
ggplot(mtcars, aes(x = wt, y = mpg, color = qsec)) + geom_point()
Default for categorical variables is a set of distinct hues evenly
spaced around the color wheel (scale_color_hue()
).
Other possible color scales include scale_color_brewer()
scale_color_viridis()
scale_color_grey()
and
scale_color_manual()
.
$cyl <- factor(mtcars$cyl)
mtcarsggplot(mtcars, aes(x = wt, y = mpg, color = cyl)) + geom_point()
Here we present some examples of color
scale functions.
There are analogous scale functions for the fill
aesthetic.
# Single color
ggplot(mtcars, aes(x = wt, y = mpg)) + geom_point(color = "blue")
# Color by group (default scale_color_hue())
<- ggplot(mtcars, aes(x = wt, y = mpg, color = factor(cyl))) + geom_point()) (g
# RColorBrewer palette
library(RColorBrewer)
+ scale_color_brewer(palette = "Dark2") g
# Viridis color
library(viridis)
## Loading required package: viridisLite
+ scale_color_viridis(discrete = TRUE) g
# Grey colors
+ scale_color_grey() g
# Manually change colors (`drop` to decide whether to keep unusued factor levels in the scale)
+ scale_color_manual(values = c("#999999", "#E69F00", "#56B4E9")) g
# Argument breaks can be used to control the appearance of the legend
+ scale_color_manual(breaks = c("8", "6", "4"), values = c("red", "blue", "green")) g
# Color by qsec values (default scale_color_gradient())
<- ggplot(mtcars, aes(x = wt, y = mpg, color = qsec)) + geom_point()) (g
# RColorBrewer color
# The brewer scales were designed for discrete data but often results to continuous data look good
# The distiller scales extend brewer to continuous scales by smoothly interpolating 7 colours from any palette to a continuous scale
# The fermenter scales provide binned versions of the brewer scales
+ scale_color_distiller(palette = "Spectral") g
+ scale_color_fermenter(palette = "Spectral") g
# Viridis color
+ scale_color_viridis() g
# Sequential gradient low and high colors
+ scale_color_gradient(low = "blue", high = "red") g
# Diverging gradient between low, mid and high colors
<- mean(mtcars$qsec)
mid + scale_color_gradient2(midpoint = mid, low = "blue", mid = "white", high = "red") g
# Gradient between n colors
+ scale_color_gradientn(colours = rainbow(5)) g