An important part of spatial visualization is mapping variables to colors. While R has no shortage of built-in functionality to map values to colors, we found that there was enough friction in the process to warrant introducing some wrapper functions that do a lot of the work for you.

To that end, we’ve created a family of color* convenience functions that can be used to easily generate palette functions. Essentially, you call the appropriate color function with 1) the colors you want to use and 2) optionally, the range of inputs (i.e. domain) that are expected. The color function returns a function that can be passed a vector of input values, and it’ll return a vector of colors in #RRGGBB(AA) format.

There are currently three color functions for dealing with continuous input: colorNumeric, colorBin, and colorQuantile; and one for categorical input, colorFactor.

Coloring Continuous Data


# From
countries <- readOGR("json/countries.geojson", "OGRGeoJSON")
map <- leaflet(countries)

We’ve loaded some shape data for countries, including a numeric field gdp_md_est which contains GDP estimates.

par(mar = c(5,5,0,0), cex = 0.8)
hist(countries$gdp_md_est, breaks = 20, main = "")

Continuous Input, Continuous Colors (colorNumeric)

Let’s start by mapping GDP values directly to the "Blues" palette from Color Brewer 2. We’ll use the colorNumeric function to create a mapping function. The "Blues" palette only contains nine colors, but colorNumeric interpolates these colors so we get continuous output.

# Create a continuous palette function
pal <- colorNumeric(
  palette = "Blues",
  domain = countries$gdp_md_est

The palette parameter is the ordered list of colors you will map colors to. In this case we used a Color Brewer palette, but we could’ve used c("white", "navy") or c("#FFFFFF", "#000080") for a similar effect. You can also pass more than two colors, for a diverging palette for example. And for maximum flexibility, you can even pass a function that takes a numeric value over the interval [0,1] and returns a color.

The second parameter, domain, indicates the set of input values that we are mapping to these colors. For colorNumeric, you can provide either a min/max as in this example, or a set of numbers that colorNumeric can call range() on.

The result is pal, a function that can accept numeric vectors with values in the range range(countries$gdp_md_est) and return colors in "#RRGGBB" format.

# Apply the function to provide RGB colors to addPolygons
map %>%
  addPolygons(stroke = FALSE, smoothFactor = 0.2, fillOpacity = 1,
    color = ~pal(gdp_md_est)

Continuous Input, Discrete Colors (colorBin and colorQuantile)

colorBin maps numeric input data to a fixed number of output colors using binning (slicing the input domain up by value).

You can specify either the exact breaks to use, or the desired number of bins. Note that in the latter case, if pretty=TRUE (the default) you’ll end up with nice round breaks but not necessarily the number of bins you wanted.

binpal <- colorBin("Blues", countries$gdp_md_est, 6, pretty = FALSE)

map %>%
  addPolygons(stroke = FALSE, smoothFactor = 0.2, fillOpacity = 1,
    color = ~binpal(gdp_md_est)

colorQuantile maps numeric input data to a fixed number of output colors using quantiles (slicing the input domain into subsets with equal numbers of observations).

qpal <- colorQuantile("Blues", countries$gdp_md_est, n = 7)
map %>%
  addPolygons(stroke = FALSE, smoothFactor = 0.2, fillOpacity = 1,
    color = ~qpal(gdp_md_est)