Map coloring: the color scale styles available in the tmap package

Junk Charts 2019-10-17

Summary:

This vignette builds on the making maps chapter of the Geocomputation with R book.Its goal is to demonstrate all possible map styles available in the tmap package.PrerequisitesThe examples below assume the following packages are attached:library(spData) # example datasetslibrary(tmap) # map creationlibrary(sf) # spatial data reprojectionThe world object containing a world map data from Natural Earth and information about countries’ names, regions, and subregions they belong to, areas, life expectancies, and populations.This object is in geographical coordinates using the WGS84 datum, however, for mapping purposes, the Mollweide projection is a better alternative (learn more in the modifying map projections section).The st_tranform function from the sf package allows for quick reprojection to the selected coordinate reference system (e.g., "+proj=moll" represents the Mollweide projection).world_moll = st_transform(world, crs = "+proj=moll")One colorLet’s start with the basics.To create a simple world map, we need to specify the data object (world_moll) inside the tm_shape() function, and the way we want to visualize it.The tmap package offers several visualisation possibilities for polygons, including tm_borders(), tm_fill(), and tm_polygons().The last one draws the filled polygons with borders, where the fill color can be specified with the col argument:tm_shape(world_moll) + tm_polygons(col = "lightblue")The output is a map of world countries, where each country is filled with a light blue color.Coloring of adjacent polygonsThe col argument is very flexible, and its action depends on the value provided.In the previous example, we provided a single color value resulting in a map with one color.To create a map, where adjacent polygons do not get the same color, we need to provide a keyword "MAP_COLORS".tm_shape(world_moll) + tm_polygons(col = "MAP_COLORS")The default color can be changed using the palette argument - run the tmaptools::palette_explorer() function to see possible palettes’ names.tm_shape(world_moll) + tm_polygons(col = "MAP_COLORS", palette = "Pastel1")Additionally, in this case, it is possible to use the minimize argument, which triggers the internal algorithm to search for a minimal number of colors for visualization.tm_shape(world_moll) + tm_polygons(col = "MAP_COLORS", minimize = TRUE)The new map uses five colors.On a side note, in theory, no more than four colors are required to color the polygons of the map so that no two adjacent polygons have the same color (learn more about the four color map theorem on Wikipedia).Categorical mapsThe third use of the col argument is by providing the variable (column) name.In this case, the map will represent the given variable.By default, tmap behaves differently depending on the input variable type.For example, it will create a categorical map when the provided variable contains characters or factors.The tm_polygons(col = "subregion", style = "cat") code will be run automatically in this case.tm_shape(world_moll) + tm_polygons(col = "subregion")+ tm_layout(legend.outside = TRUE) Discrete mapsDiscrete maps represents continuous numerical variables using discrete class intervals.There are several ways to convert continuous variables to discrete ones implemented in tmap.PrettyWhen the variable provided as the col argument is numeric, tmap will use the "pretty" style as a default.In other words, it runs tm_polygons(col = "lifeExp", style = "pretty") invisibly to the user.This style rounds breaks into whole numbers where possible and spaces them evenly.tm_shape(world_moll) + tm_polygons(col = "lifeExp", legend.hist = TRUE) + tm_layout(legend.outside = TRUE) A histogram is added using legend.hist = TRUE in this and several next examples to show how the selected map style relates to the distribution of values.It is possible to indicate a preferred number of classes using the n argument.Importantly, not every n is possible depending on the range of the values in the data.tm_shape(world_moll) + tm_polygons(col = "lifeExp", legend.hist = TRUE, n = 4) + tm_layout(legend.outside = TRUE) FixedThe "jenks" style allows for a manual selection of the breaks in conjunction with the breaks argument.tm_shape(world_moll) + tm_polygons(col = "lifeExp", style = "fixed", breaks = c(45, 60, 75, 90), legend.hist = TRUE) + tm_layout(legend.outside = TRUE) Additionally, the default labels can be overwritten using the labels argument.tm_shape(world_moll) + tm_polygons(col = "lifeExp", style = "fixed", breaks = c(45, 60, 75, 90), labels = c("low", "medium", "high"), legend.hist = TRUE) + tm_layout(legend.outside = TRUE) Breaks based on the standard deviation valueThe "sd" style calculates a standard deviation of a given variable, and next use this value as the break width.tm_shape(world_moll) + tm_polygons(col = "lif

Link:

http://feedproxy.google.com/~r/RBloggers/~3/iX4PtyE_Lzs/

From feeds:

Statistics and Visualization » R-bloggers

Tags:

bloggers

Authors:

the Geocomputation with R website

Date tagged:

10/17/2019, 17:42

Date published:

10/16/2019, 20:00