ggplot2 Colors: Customize Colour and Fill Scales
Colour can distinguish groups, emphasize a pattern or encode a numeric value. The key is to separate fixed colour settings from data-driven colour scales.
This lesson is part of the ggplot2 tutorial. It uses the built-in mpg dataset included with ggplot2, so you can run the examples after loading the package.
Set one fixed colour
Place a fixed colour outside aes(). This changes every point without creating a legend.
ggplot(mpg, aes(displ, hwy)) +
geom_point(colour = "steelblue",
size = 2)
Map colour to a category
Put the variable inside aes(). ggplot2 treats a categorical variable as a discrete scale and creates a legend.
ggplot(mpg, aes(displ, hwy,
colour = class)) +
geom_point()
Use a manual discrete palette
scale_colour_manual() assigns named or ordered colours to categories. scale_fill_manual() does the same for filled areas.
ggplot(mpg, aes(displ, hwy,
colour = drv)) +
geom_point(size = 2) +
scale_colour_manual(
values = c("4" = "#3366AA",
"f" = "#2A9D8F",
"r" = "#E76F51")
)
Use a continuous gradient
When the mapped variable is numeric, use a continuous scale. A gradient can communicate low-to-high values.
ggplot(mpg, aes(displ, hwy,
colour = cty)) +
geom_point(size = 3) +
scale_colour_gradient(
low = "navy",
high = "orange"
)
Choose accessible colours
Use palettes with adequate contrast and do not rely on colour alone to communicate a result. Consider colour-vision deficiencies, print use and the background. Packages such as viridis provide perceptually uniform palettes.
ggplot(mpg, aes(displ, hwy,
colour = class)) +
geom_point() +
scale_colour_viridis_d()
Common mistakes and troubleshooting
- Could not find function: run
library(ggplot2)in the current R session. Install it first withinstall.packages("ggplot2")if needed. - Object not found: check spelling and confirm the column exists in the data frame. Use
names(mpg)to inspect column names. - Unexpected legend: a variable mapped inside
aes()creates a scale and usually a legend. A fixed setting belongs outsideaes(). - Plot does not appear: make sure the complete expression is evaluated. In a script or function, explicitly call
print(p)when displaying a stored ggplot object. - Warning about removed rows: inspect missing or out-of-range values. Do not suppress the warning until you understand why observations were removed.
Next step: Continue with the next lesson using the navigation at the bottom of this page, or return to the ggplot2 tutorial overview.
ggplot2 FAQ
How do I change the colour of points in ggplot2?
Use geom_point(colour = "steelblue") for one fixed colour, or map a variable inside aes(colour = group) to assign colours by category.
What is the difference between scale_colour_manual and scale_fill_manual?
scale_colour_manual() changes discrete colour mappings, commonly outlines and points. scale_fill_manual() changes fill mappings for bars, boxes and other filled geoms.
How do I use a colour gradient?
Map a numeric variable to colour or fill and add scale_colour_gradient() or scale_fill_gradient().