How to expand color palette with ggplot and RColorBrewer

September 12, 2013

(This article was first published on novyden, and kindly contributed to R-bloggers)

Histograms are almost always a part of data analysis presentation. If it is made with R ggplot package then it may look like this:

ggplot(mtcars) +
geom_histogram(aes(factor(cyl), fill=factor(cyl)))

The elegance of ggplot functions is in simple yet compact expression of visualization formula while hiding many options assumed by default. Hiding doesn’t mean lacking as most options are just a step away. For example, color selection can change with one of scale functions such as scale_fill_brewer:

ggplot(mtcars) +
geom_histogram(aes(factor(cyl), fill=factor(cyl))) +

In turn, scale_fill_brewer palette can be changed too:

ggplot(mtcars) +
geom_histogram(aes(factor(cyl), fill=factor(cyl))) +

Palettes used live in the package RColorBrewer – to see all available choices simply attach the package with library(RColorBrewer) and run display.brewer.all() to show this:

There are 3 types of palettes, sequential, diverging, and qualitative; each containing from 8 to 12 colors (see data frame and help ?RColorBrewer for details).

Curious reader may notice that if a histogram contains 13 or more bars (bins in case of continuous data) we may get in trouble with colors:

ggplot(mtcars) + 
geom_histogram(aes(factor(hp), fill=factor(hp))) +

Indeed length(unique(mtcars$hp)) finds 22 unique values for horse power in mtcars, while specified palette Set2 has 8 colors to choose from. Lack of colors in the palette triggers ggplot warnings like this (and invalidates plot as seen above):

1: In brewer.pal(n, pal) :
  n too large, allowed maximum for palette Set2 is 8
Returning the palette you asked for with that many colors

RColorBrewer gives us a way to produce larger palettes by interpolating existing ones with constructor function colorRampPalette. It generates functions that do actual job: they build palettes with arbitrary number of colors by interpolating existing palette. To interpolate palette Set1 to 22 colors (number of colors is stored in colourCount variable for examples to follow):

colourCount = length(unique(mtcars$hp))
getPalette = colorRampPalette(brewer.pal(9, "Set1"))
ggplot(mtcars) +
geom_histogram(aes(factor(hp)), fill=getPalette(colourCount)) +

While we addressed color deficit other interesting things happened: even though all bars are back and are distinctly colored we lost color legend. I intentionally added theme(legend.position=…) to showcase this fact: despite explicit position request the legend is no more part of the plot.

The difference: fill parameter was moved outside of histogram aes function – this effectively removed fill information from aesthetics data set for ggplot. Hence, there is nothing to apply legend to.

To fix it place fill back into aes and use scale_fill_manual to define custom palette:

ggplot(mtcars) + 
geom_histogram(aes(factor(hp), fill=factor(hp))) +
scale_fill_manual(values = getPalette(colourCount))

Another likely problem with large number of bars in histogram plots is placing of the legend. Adjust legend position and layout using theme and guides functions as follows :

ggplot(mtcars) + 
geom_histogram(aes(factor(hp), fill=factor(hp))) +
scale_fill_manual(values = getPalette(colourCount)) +
theme(legend.position="bottom") +

Finally, the same example using  in place palette constructor with different choice of library palette:

ggplot(mtcars) + 
geom_histogram(aes(factor(hp), fill=factor(hp))) +
scale_fill_manual(values = colorRampPalette(brewer.pal(12, "Accent"))(colourCount)) +
theme(legend.position="bottom") +

There are quite a few more scale functions to choose from depending on aesthetics type (colour, fill), color types (gradient, hue, etc.), data values (discrete or continuous).

To leave a comment for the author, please follow the link and comment on their blog: novyden. offers daily e-mail updates about R news and tutorials on topics such as: Data science, Big Data, R jobs, visualization (ggplot2, Boxplots, maps, animation), programming (RStudio, Sweave, LaTeX, SQL, Eclipse, git, hadoop, Web Scraping) statistics (regression, PCA, time series, trading) and more...

If you got this far, why not subscribe for updates from the site? Choose your flavor: e-mail, twitter, RSS, or facebook...

Comments are closed.


Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)