Making Maps!

Visualising spatial data in R

Rhian Davies

Welcome

05:00

Introduction to Spatial Data in R

Spatial in R

Cartoon robot with a map of the world

Raster data

  • A grid made up of cells
  • Usually equally spaced squares
  • Can have a pixelated look and feel
  • Often very large

Vector data

  • Made of individual points
  • Join points to make lines and polygons
  • Data isn’t dependent on grid size like raster data

Open Geography portal

QR code

Geometry data

  • Folder with at least 3 files
  • .shp - The geometries i.e. the actual “shapes”
  • .shx - Index file
  • .dbf - Feature attributes i.e. population of a country.
  • Read in the .shp file
  • One file with the extension .geojson
  • Contains JSON code description each feature, it’s geometry and it’s attributes.
  • Folder with at least 4 files
  • .TAB - Central file
  • .DAT - Attribute data
  • .MAP - Geographic information
  • .ID - Links geographic data to the database
  • Read in the .TAB file

Reading in data

library("sf")
world = st_read("data/countries.geojson")
plot(world)

Just a dataframe

ADMIN ISO_A3 geometry
Aruba ABW MULTIPOLYGON (((-69.99694 1…
Afghanistan AFG MULTIPOLYGON (((71.0498 38….
Angola AGO MULTIPOLYGON (((11.73752 -1…
Anguilla AIA MULTIPOLYGON (((-63.03767 1…
Albania ALB MULTIPOLYGON (((19.74777 42…
Aland ALA MULTIPOLYGON (((20.92018 59…

Your turn

  • Open a new R script
  • Load {sf} package
  • Us st_read to load the ESRI data in data/ukgeom
  • View with plot()
07:00

Let’s make maps!

Mapping packages

  • {tmap}
  • {leaflet}
  • {cartogram}
  • {rasterViz}
  • {ggplot2}

My first {tmap}

library("tmap")
tm_shape(nz) + 
  tm_polygons()

Adding polygons

tm_shape(nz) +
  tm_fill(col = "cornflowerblue") +
  tm_borders(col = "maroon")

Adding lines

tm_shape(dataset) +
  tm_lines()

Adding dots

data(ukgeom, package = "jrSpatial")
data(ukhills, package = "jrSpatial")
tm_shape(ukgeom) + 
  tm_polygons() +
tm_shape(ukhills) + 
  tm_dots()

Colour by the data

tm_shape(nz) +
  tm_fill(col = "Median_income") +
  tm_borders()

Changing the breaks

tm_shape(nz) +
  tm_fill("Median_income",
          breaks = c(22000,
                     28000,
                     34000)) +
  tm_borders()

Changing the colour palettes

tm_shape(nz) +
  tm_fill("Median_income",
          breaks = c(22000,
                     28000,
                     34000),
          palette = "PuRd"
  ) +
  tm_borders()

The tm_layout() function

tm_shape(nz) +
  tm_fill(col = "Median_income") +
  tm_layout(
    title = "New Zealand",
    legend.position = c("right",
                        "bottom")
  )

The tm_layout() function

tm_shape(nz) +
  tm_fill(col = "Median_income") +
  tm_layout(
    title = "New Zealand",
    legend.outside = TRUE
  )

Task 2

  • Use tm_shape() & tm_polygons() to plot the UK data
  • Choose a column of the data to fill the polygon tm_polygons(col = "COL_NAME")
  • Use colorbrewer2.org to set the palette argument in tm_polygons()
  • Try setting the breaks argument
  • Play with the settings in tm_layout()
15:00

Extra tips & tricks

Joining data

data(world, package = "jrSpatial")
data(worldstats, package = "jrSpatial")

merged = dplyr::left_join(world, worldstats,
                 by = c("name_long" = "name"))

Add scale and compass

tm_shape(nz) +
  tm_borders() +
  tm_compass() +
  tm_scale_bar()

Make interactive

tmap_mode("view")
tm_shape(nz) +
  tm_fill(col = "Median_income")
  • To return to plot mode, call tmap_mode("plot")

Add text

tm_shape(nz) +
  tm_borders(col = "red") +
  tm_text(text = "Name",
          size = 0.7)

Task 3

  • Load the ukgeom, ukdata.
  • Combine ukgeom with ukdata using left_join().
  • Use {tmap} to map life expectancy
  • Share your pretty map! #RSS2023Conf
15:00

Questions?


@statsRhian

jumpingrivers.com

shiny-in-production.jumpingrivers.com