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.

ggplot2 allows to build almost any type of chart. The R graph
gallery focuses on it so almost every section there starts with ggplot2 examples.

This page is dedicated to general ggplot2 tips that you can apply to any chart, like customizing a title, adding annotation, or using faceting.
If you’re into ggplot2, a good resource is probably this online course to go further.
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. 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 comes 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
Quick notes

I few stuff I often need to remember:

Related chart types


Ggplot2
Animation
3D
Caveats
Data art