12.5 Using a Manually Defined Palette for a Discrete Variable
12.5.2 Solution
In the example here, we’ll manually define colors by specifying values with scale_colour_manual()
(Figure 12.11). The colors can be named, or they can be specified with RGB values:
library(gcookbook) # Load gcookbook for the heightweight data set
# Create the base plot
ggplot(heightweight, aes(x = ageYear, y = heightIn, colour = sex)) +
hw_plot <- geom_point()
# Using color names
+
hw_plot scale_colour_manual(values = c("red", "blue"))
# Using RGB values
+
hw_plot scale_colour_manual(values = c("#CC6666", "#7777DD"))
# Using RGB values based on the viridis color scale
+
hw_plot scale_colour_manual(values = c("#440154FF", "#FDE725FF")) +
theme_bw()
For fill scales, use scale_fill_manual()
instead.
12.5.3 Discussion
The order of the items in the values vector matches the order of the factor levels for the discrete scale. In the preceding example, the order of sex is f, then m, so the first item in values goes with f and the second goes with m. Here’s how to see the order of factor levels:
levels(heightweight$sex)
#> [1] "f" "m"
If the variable is a character vector, not a factor, it will automatically be converted to a factor, and by default the levels will appear in alphabetical order.
It’s possible to specify the colors in a different order by using a named vector:
+
hw_plot scale_colour_manual(values = c(m = "blue", f = "red"))
There is a large set of named colors in R, which you can see by running color()
. Some basic color names are useful: “white”, “black”, “grey80”, “red”, “blue”, “darkred”, and so on. There are many other named colors, but their names are generally not very informative (I certainly have no idea what “thistle3” and “seashell” look like), so it’s often easier to use numeric RGB values for specifying colors.
RGB colors are specified as six-digit hexadecimal (base-16) numbers of the form #RRGGBB
. In hexadecimal, the digits go from 0 to 9, and then continue with A (10 in base 10) to F (15 in base 10). Each color is represented by two digits and can range from 00 to FF (255 in base 10). So, for example, the color #FF0099
has a value of 255 for red, 0 for green, and 153 for blue, resulting in a shade of magenta. The hexadecimal numbers for each color channel often repeat the same digit because it makes them a little easier to read, and because the precise value of the second digit has a relatively insignificant effect on appearance.
Here are some rules of thumb for specifying and adjusting RGB colors:
- In general, higher numbers are brighter and lower numbers are darker.
- To get a shade of grey, set all the channels to the same value.
- The opposites of RGB are CMY: Cyan, Magenta, and Yellow. Higher values for the red channel make it more red, and lower values make it more cyan. The same is true for the pairs green and magenta, and blue and yellow.
You may want to manually select colors based on the color scales in the viridis package, as described in Recipe 12.4. You can do so by calling viridis()
and passing it the number of discrete categories you have. This will generate the RGB hexadecimal values. You can similarly generate the RGB values for the other color scales in the viridis package: “magma”, “plasma”, “inferno”, and “cividis”.
library(viridis)
viridis(2) ## Specifying 2 discrete categories with the viridis color scale
#> [1] "#440154FF" "#FDE725FF"
inferno(5) ## Specifying 5 discrete categories with the inferno color scale
#> [1] "#000004FF" "#56106EFF" "#BB3754FF" "#F98C0AFF" "#FCFFA4FF"
12.5.4 See Also
A chart of RGB color codes: http://html-color-codes.com.