A diverging scale example
Let's take a look at another dataset where the default color scale isn't appropriate. This raster, migration
, has an estimate of the net number of people who have moved into each cell of the raster between the years of 1990 and 2000. A positive number indicates a net immigration, and a negative number an emigration. Take a look:
tm_shape(migration) +
tm_raster() +
tm_legend(outside = TRUE,
outside.position = c("bottom"))
The default color scale doesn't look very helpful, but tmap
is actually doing something quite clever: it has automatically chosen a diverging color scale. A diverging scale is appropriate since large movements of people are large positive numbers or large (in magnitude) negative numbers. Zero (i.e. no net migration) is a natural midpoint.
tmap
chooses a diverging scale when there are both positive and negative values in the mapped variable and chooses zero as the midpoint. This isn't always the right approach. Imagine you are mapping a relative change as percentages; 100% might be the most intuitive midpoint. If you need something different, the best way to proceed is to generate a diverging palette (with an odd number of steps, so there is a middle color) and specify the breaks yourself.
Let's see if you can get a more informative map by adding a diverging scale yourself.
(Data source: de Sherbinin, A., M. Levy, S. Adamo, K. MacManus, G. Yetman, V. Mara, L. Razafindrazay, B. Goodrich, T. Srebotnjak, C. Aichele, and L. Pistolesi. 2015. Global Estimated Net Migration Grids by Decade: 1970-2000. Palisades, NY: NASA Socioeconomic Data and Applications Center (SEDAC). http://dx.doi.org/10.7927/H4319SVC Accessed 27 Sep 2016)
This exercise is part of the course
Visualizing Geospatial Data in R
Exercise instructions
- Print
migration
to verify this is aRasterLayer
object and take a look at the range in migration values. - Generate a diverging palette, called
red_gray
, of 7 colors from the"RdGy"
palette inRColorBrewer
. - Use the diverging set of colors,
red_gray
, as the palette for your plot. This uses your colors, but the breaks aren't useful. - Add fixed breaks for the color scale of:
c(-5e6, -5e3, -5e2, -5e1, 5e1, 5e2, 5e3, 5e6)
Hands-on interactive exercise
Have a go at this exercise by completing this sample code.
# Print migration
# Diverging "RdGy" palette
red_gray <- brewer.pal()
# Use red_gray as the palette
tm_shape(migration) +
tm_raster() +
tm_legend(outside = TRUE, outside.position = c("bottom"))
# Add fixed breaks
tm_shape(migration) +
tm_raster() +
tm_legend(outside = TRUE, outside.position = c("bottom"))