Dealing with colors in ggplot2





ggplot2 is a R package dedicated to data visualization. It can greatly improve the quality and aesthetics of your graphics, and will make you much more efficient in creating them.

It can sometimes be tricky
to control the colors of a ggplot2 graph. This section describes the most common use cases, making sure you find the solution for your next creation.

It also showcases a little app I've made to find the
perfect color palette among a list of 2500+ option.

If you’re looking for more general ggplot2 tips, check the dedicated page of the gallery!
Setting a color with fill and color

ggplot2 allows to customize the shape colors thanks to its fill and color arguments. It is important to understand the diffence between both.

The fill property changes the color inside the shape, while the color property modifies its border color.

Note that color and colour always have the same effect.

library(ggplot2)

ggplot(mtcars, aes(x=drat)) +
  geom_density(
    color="purple",
    fill="#69b3a2",
    size=2
  )

Picking one color with R


There are 5 main methods to call a color in R. Click the buttons below to see a description of them

Name rgb() Number Hex code

Name → The most common method is to call a color by its name. R offers about 657 color names. You can read all of them using colors().

Read more
ggplot2 theme
Assign a color to a variable with ggplot2

ggplot2 allows to automatically assign a marker color to a variable. Basically, you just have to specify the variable in the aes() part of the call.

Moreover, a legend will come for free! 🎉

library(ggplot2)

ggplot(iris, aes(x=Sepal.Length, y=Sepal.Width, color=Species)) +
  geom_point(size=6)

Changing the color scale with ggplot2


ggplot2 provides a color scale by default. Several methods are available to change it:

→ Included in ggplot2

hue manual grey

→ R Color Brewer

BuPu RdYlBu Paired PuOr Spectral Pastel1

→ Viridis

Magma Inferno Plasma Viridis Cividis

→ All other: Paletteer

Nord Awtools Dutchmasters ggsci ggthemes
ggplot2 theme

ggplot2 natively supports several methods to customize the color palette:

ggplot2 natively supports several methods to customize the color palette:

ggplot2 natively supports several methods to customize the color palette:

ggplot2 natively supports several methods to customize the color palette:

ggplot2 natively supports several methods to customize the color palette:

ggplot2 natively supports several methods to customize the color palette:

ggplot2 natively supports several methods to customize the color palette:

ggplot2 natively supports several methods to customize the color palette:

ggplot2 natively supports several methods to customize the color palette:

ggplot2 natively supports several methods to customize the color palette:

ggplot2 natively supports several methods to customize the color palette:

ggplot2 natively supports several methods to customize the color palette:

ggplot2 natively supports several methods to customize the color palette:

ggplot2 natively supports several methods to customize the color palette:

All the existing color palettes are available in Paletteer. Just specify the package and palette names to use!

All the existing color palettes are available in Paletteer. Just specify the package and palette names to use!

All the existing color palettes are available in Paletteer. Just specify the package and palette names to use!

All the existing color palettes are available in Paletteer. Just specify the package and palette names to use!

All the existing color palettes are available in Paletteer. Just specify the package and palette names to use!

p <- ggplot(iris,
  aes(x=Sepal.Length, y=Sepal.Width, color=Species)) +
  geom_point(size=6)
        
p + scale_color_hue(h = c(180, 300))
        
p + scale_color_manual(values=c("#69b3a2", "purple", "black"))
        
p + scale_color_grey()
        
p + scale_color_brewer(palette = "BuPu")
        
p + scale_color_brewer(palette = "RdYlBu")
        
p + scale_color_brewer(palette = "Paired")
        
p + scale_color_brewer(palette = "PuOr")
        
p + scale_color_brewer(palette = "Spectral")
        
p + scale_color_brewer(palette = "Pastel1")
        
p + scale_color_viridis(discrete=TRUE, option="magma")
        
p + scale_color_viridis(discrete=TRUE, option="inferno")
        
p + scale_color_viridis(discrete=TRUE, option="plasma")
        
p + scale_color_viridis(discrete=TRUE, option="viridis")
        
p + scale_color_viridis(discrete=TRUE, option="cividis")
        
p + scale_color_paletteer_d(nord, aurora)
        
p + scale_color_paletteer_d(awtools, a_palette)
        
p + scale_color_paletteer_d(dutchmasters, milkmaid)
        
scale_color_paletteer_d(ggsci, nrc_npg)
        
p + scale_color_paletteer_d(ggthemes, calc)
        
p + scale_color_hue(h = c(180, 300))
See all See all See all See all See all See all See all See all See all See all See all
Paletteer: The Definitive Catalog of R Color Palettes

Paletteer is an R package that provides over 2500+ ready-to-use color palettes for your charts.

Using paletteer is simple. It comes with a few functions like scale_fill_paletteer_d which allows to utilize any of the 2500+ palettes in your plot. I've written a comprehensive introduction to Paletteer here.

paletter introduction

Additionally, I've developed a lightweight application that allows you to explore these 2500+ palettes quickly. It includes a variety of features that I'm sure you'll enjoy.

Color Palette Finder

Quick notes

I few stuff I often need to remember:

Related chart types


Ggplot2
Animation
3D
Caveats
Data art