Visualizing Spatial Data in R

Objectives

  1. Apply aesthetic mappings to visualize attributes
  2. Visualize a wide range of spatial data types and attributes using tmap
  3. Create map layouts using tmap
  4. Produce interactive maps with tmap
  5. Export layouts and interactive maps

Overview

In this section we will explore visualizing and mapping geospatial data in R. The ggmap package offers a mapping extension for ggplot2. However, this requires an API key for Google Maps, so I will not cover that method here. If you would like to learn ggmap, there are many resources online to do so. Instead, we will explore the tmap package. The name stands for thematic maps and it offers a variety of methods to visualize categorical and continuous variables over map space using vector and raster data.

In this section we will explore many packages including tmap, tmaptools, rgdal, sf, raster, and OpenStreetMap. You will need to install all of these packages before proceeding. In the first code block, I am calling these packages in.

We will use several spatial data layers in the example maps. Here is a brief explanation of each.

  • cities2.shp: major cities in US
  • states.shp: state boundaries
  • tracks.shp: major Amtrak routes in US
  • dem_example.img: example digital elevation model (DEM) from the National Elevation Data set (NED)
  • lc_example.img: example land cover from 2011 National Land Cover Database (NLCD)
  • tucker_dem2.tif: 30 m DEM of Tucker County, WV from NED
  • tucker_hs2.tif: 30 m hillshade of Tucker County, WV

In the following code block I am reading in the vector data layers using st_read() from the sf package and raster grids using raster() from the raster package.

Visualizing Vector Data

The tmap syntax is very similar to ggplot2. A map is initiated using tm_shape() and the data visualization and additional map elements are defined using additional arguments. The components are combined using +. Here is a list of some key functions to visualize different data types.

  • tm_polygons(): polygons with fill and border
  • tm_fill(): polygons with just fill
  • tm_border(): polygons with just border
  • tm_dots(): points as dots
  • tm_markers(): points as markers
  • tm_squares(): points as squares
  • tm_bubbles(): points as bubbles with size, fill, and border
  • tm_lines(): lines with color, type, and width
  • tm_raster(): display categorical and continuous raster grids
  • tm_rgb(): raster as RGB image
  • tm_basemap(): add base map tiles
  • tm_text(): add map text and labels
  • tm_iso(): add isolines or contour lines

There are additional functions for adding map elements, altering the layout, and saving the resulting map to disk.

  • tm_compass(): add map compass or north arrow
  • tm_scale_bar(): add scale bar
  • tm_credits(): add map credits as text
  • tm_grid(): add graticules or coordinate system grids
  • tm_layout(): make changes to layout
  • tm_layout(): change tmap style
  • tm_facets(): create multiple maps as facets (similar to facet_grid() in ggplot2)
  • tm_save(): save map layout to file
  • tm_mode(): switch between “view” and “plot” modes

Let’s start using these functions to create some maps.

Points

In this first example, I am symbolizing cities based on population using tm_bubbles(). Similar to ggplot2, we can map constants or variables to graphic parameters using aesthetic mappings. Here are a few notes about this map.

  1. I am adding an Open Street Maps base map using the osm_map() function from the OpenStreetMap package. I specify the extent of the base map using the bounding box of the states object. The bounding box is extracted using st_bbox() from sf. tm_shape() is used to indicate that we want to include the base map layer, and it is drawn using tm_rgb().
  2. The points are added using tm_shape() and symbolized using tm_bubbles(). The size of the bubble is mapped to a continuous variable of population. I indicate that the title associated with the size should be “Population of City”, which will be used in the legend.
  3. Using tm_layout() I specify that I want the legend to print outside of the map space and at the bottom of the map.

Again, the best way to learn is to experiment, so feel free to make changes and see how this impacts the result.

I generally use tm_bubbles() to show point data. However, it is also possible to use tm_dots() or tm_markers() as demonstrated in the next two maps.Note that no legend is printed since no variables are mapped to graphical parameters.

Lines

Here are some explanations of the following map.

  1. I am using a base map as already described above.
  2. The tracks are symbolized using tm_lines(). Here, I am not mapping data variables to graphical parameters. Instead, I just specify the color, size, and line type desired. However, it is possible to map continuous variables to size, continuous or categorical variables to color, and categorical variables to line type.

Polygons

Polygon features are generally symbolized using tm_polygons(). When using this function, you can map variables or constants to color, and it is possible to represent continuous or categorical variables. Here, I am not mapping any data variables to a graphical parameter, so no legend is provided.

I have also added a north arrow using tm_compass() and a scale bar using tm_scale_bar(). They are both positioned at the bottom-left of the map space.

Lastly, I am using tm_layout() to add a main title, which will be rendered outside of the map space by default.

tm_fill() and tm_borders() can be used to just display the polygons using a fill color with no outline or just using a border or outline with no fill, respectively. The next two maps demonstrate these options.

Here, I am combining tm_fill() and tm_borders() to provide both a fill and a border.

In this example, I have added a base map beneath the polygons displayed using tm_borders(). Similar to ggplot2, layers added later will draw above prior layers. So, we would not want to provide the state boundaries first or they would get covered up by the base map.

It is possible to include labels using tm_text(). In this example, the “STATE_ABBR”" field provides the state abbreviation, which I am rendering to text labels. I have also scaled the text size relative to the area of the state.

We will now explore mapping variables to colors. The tmaptools package provides a palette_explorer() function that can be used to explore color palettes made available by ColorBrewer. If you are a GIS professional, you might be familiar with the ColorBrewer2 website by Cynthia Brewer of Penn State that helps with the selection of colors for choropleth maps. These color ramps can also be accessed in R, and a variety of sequential, diverging, and qualitative palettes are provided.

I have commented out the code example below because this will cause a Shiny window to open that allows you to explore the color palettes interactively. If you want to open this window, feel free to run the code. Shiny allows for the generation of interactive web content from R. I have provided a screen capture of the Shiny page below.

Once you have selected a color palette, you can apply it in tmap.

ColorBrewer2 Palettes

ColorBrewer2 Palettes

In this example, I am displaying a continuous variable, population density, using a sequential color ramp defined by ColorBrewer2. I am using 5 classes and a quantile classification scheme. Also, I set the title for the the tm_polygons() to “People/Sq1. Mi.”, as I want this to print as part of the legend.

The legend is plotted in the map space at the bottom-right position while the north arrow and scale bar are plotted at the bottom-left position. I have provided a main title that plots outside of the map frame.

Similar to the example above, I am providing text labels using tm_text().

It is also possible to use color to map a categorical variable. In this example, I have selected a color ramp using ColorBrewer that is suitable for qualitative data, in this case sub-regions of the country. The style is set to “cat” to indicated that I want to represent categorical data. I have set n to 9 because there are 9 different sub-regions to differentiate. I have set the title argument to a blank string within tm_polygons() so that no legend title is plotted. Other than that, the code is the same as the example above.

In this example, I am using tm_facets() to separate the map into multiple components by sub-region. Again, this is pretty similar to facet_grid() in ggplot2. You will need to use a categorical variable to define the facets.

Final Vector Examples

In this example, I have combined several components to create a map of Amtrak lines across the country. Here are some explanations.

  1. The states are symbolized using tm_polygons() with the color mapped to the sub-region name. I used a color palette from ColorBrewer2 and set the style to “cat.” I set the title in tm_polygons() to a blank string so that a title is not included in the legend.
  2. The Amtrak lines are symbolized using tm_lines() with a black color and a width of 2.
  3. The cities are symbolized using tm_bubbles() with size representing population. I have also provided a title to use in the legend.
  4. A scale bar and north arrow have been provided in the bottom-left position.
  5. I have provided a title, title size, and placed the legend outside of the map frame using tm_layout()

What if you wanted to make a layout in a different coordinate system or map projection? To do this you should convert all layers to the desired projection before making the layout. In the next two code blocks I have converted the layers to a Lambert Conformal Conic projection using st_transform() then recreated the map from above in this projection.

tmap provides a tmap_arrange() function that allows you to place multiple maps in the same layout space. This is similar to the plot_grid() function from cowplot. It is different from facet_grid() and tm_facets() since we are not using categorical variables to define the component maps.

In this example, I first convert the state boundaries to different coordinate systems. I then create four maps using the different projections. The maps are saved to objects so that they can be used in a later function.

In these maps, I have also provided a main title outside of the map frame that provides the coordinate system used and tm_grid() to add graticules, or a latitude/longitude grid. The states are assigned colors using a “MAP_COLORS” field with a defined palette.

Lastly, the maps are combined to as single layout space using tmap_arrange(). This provides a nice side-by-side comparison of the different coordinate systems.

Visualizing Raster Data

Continuous Raster Grids

In tmap, single-band raster data are symbolized using tm_raster() as demonstrated in the next code block. This is a DEM, which is a continuous raster.

Similar to most GIS software packages, data values can be grouped or binned to represent a range of values with one color. Some common methods used include the following:

  • Equal Interval: equal range of data values in each bin
  • Quantiles: equal number of map features or grid cell in each bin.

The next two examples demonstrate these different classification schemes.

If you do not want to group colors into ranges or binds, you can use the continuous method where a color ramp is used to display the data without binning.

The next set of examples demonstrate applying ColorBrewer2 color ramps to this continuous raster surface. Note that these ramps can be applied when using classified or continuous methods.

It is also possible to invert the color ramp by adding a negative sign to the name.

Categorical Rasters

Now we will investigate symbolizing categorical raster data. This can be accomplished by setting the style argument equal to “cat.”

I generally find that I need to provide my own labels and colors for categorical data. In this example, I am providing labels for each code so that the land cover category is displayed as opposed to the stand-in cell value. I also provide my own color palette using named R colors. Similar to ggplot2, the codes, labels, and colors must be in the same order so that they match up correctly, since this is based on the position or index only. Note that you could also provide hex codes or RGB values to define your colors.

Final Raster Examples

In this final raster example, I am visualizing a terrain surface using a hillshade and DEM. The DEM is placed below the hillshade, and I have set alpha equal to 0.6 to apply a transparency. I also provide a compass, scale bar, and data credits. All of the elements are plotted within the map frame. Here, I have specified the credit position using values as opposed to named positions. This works the same a ggplot2.

Again, it would be good to experiment with these example to see if you can figure out how to make some desired changes or add more data layers.

Plots vs. Views

Throughout this example, we have been using “plot” mode as opposed to “view” mode. In “plot” mode, you are working in a layout space. In contrast, “view” mode allows you to generate an interactive map that you can navigate and move within. The default is “plot”. The first example here shows the “plot” mode while the second show “view” mode. It is generally a good idea to restore the mode to “plot” after using “view”. Note also that base maps are defined differently in “plot” and “view” modes.

Write to File

Similar to ggplot2, layouts can be written to disk as vector or raster graphic files. This is accomplished using tmap_save(). In the first example, I am saving the map as SVG and PDF files. Note that the map is saved to an object so that it can be called in the save function. If you don’t want to save these files to your computer, you should not run these examples.

In this example, I am exporting to raster formats at JPEG and TIFF.

When using “view” mode, you can also write the interactive map out as an HTML file for viewing in a web browser.

That’s it! Now, you should be able to symbolize a variety of data types and variable using tmap, generate map layouts, and export your maps as layouts or webpages. If you would like to learn more about tmap, here is a link to the documentation. As with ggplot2, you will likely run into specific issues that I did not cover; however, you should be able to troubleshoot using the documentation and/or a Google search. Throughout the remainder of the course, you will see more tmap examples as we visualize output from analyses.

You got a light introduction to interactive web maps in this module; however, there is much more functionality made available through Leaflet, which is accessible via an R package. We will investigate Leaflet in a later module.

Back to Course Page

Back to WV View

Download Data