0% found this document useful (0 votes)
4 views8 pages

Mapping Point Data in R

This document is a tutorial on mapping point data in R, focusing on spatial data analysis and visualization using the tmap package. It covers creating point shapefiles from CSV data, mapping house prices, and generating proportional bubble maps. The tutorial includes code snippets for loading data, creating spatial data frames, and customizing visualizations.

Uploaded by

mercy
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views8 pages

Mapping Point Data in R

This document is a tutorial on mapping point data in R, focusing on spatial data analysis and visualization using the tmap package. It covers creating point shapefiles from CSV data, mapping house prices, and generating proportional bubble maps. The tutorial includes code snippets for loading data, creating spatial data frames, and customizing visualizations.

Uploaded by

mercy
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

10/3/24, 10:05 AM [Link]/system/files/practical6_0.

html

Practical 6: Mapping Point Data in R


An Introduction to Spatial Data Analysis and Visualisation in R - Guy Lansley & James Cheshire (2016)

This practical will follow on from the previous exercise ([Link]


f9168462f4ac/70c4bc61-0475-4806-9240-4ef1fa649a06) by introducing the handling and mapping of spatial point
data in R using tmap. Data for the practical can be downloaded from the Introduction to Spatial Data Analysis
and Visualisation in R ([Link]
homepage.

In this tutorial we will:

Create a point shapefile from a CSV using coordinates


Map the points in tmap
Create a proportional bubble map

First, we must set the working directory and load the practical data.

# Set the working directory


setwd("C:/Users/Guy/Documents/Teaching/CDRC/Practicals")

# Load the data. You may need to alter the file directory
[Link] <-[Link]("practical_data.csv")

We will also need the polygon shapefile from our previous exercise and to join our census data to it.

# load the spatial libraries


library("rgdal")
library("rgeos")

# Load the output area shapefiles


[Link] <- readOGR(".", "Camden_oa11")

## OGR data source with driver: ESRI Shapefile


## Source: ".", layer: "Camden_oa11"
## with 749 features
## It has 1 fields

# join our census data to the shapefile


[Link] <- merge([Link], [Link], by.x="OA11CD", by.y="OA")

Loading point data into R


In this tutorial we will be handling house price paid data originally made available for free by the Land Registry. The
data is formatted as CSV where each row is a unique house sale, including the price paid in pounds and the
postcode. Prior to this practical, the data file was joined to the Office for National Statistics (ONS) postcode lookup
table which provides latitude and longitude coordinates for each postcode.

[Link] 1/8
10/3/24, 10:05 AM [Link]/system/files/practical6_0.html

# load the house prices csv file


houses <- [Link]("[Link]")

# we only need a few columns for this practical

houses <- houses[,c(1,2,8,9)]

Whilst it is possible to plot this data using the standard plot() in R (as demonstrated below), it is not being
handled as spatial data.

# 2D scatter plot
plot(houses$oseast1m, houses$osnrth1m)

Therefore, we need to assign spatial attributes to the CSV so it can be mapped properly in R. To do this we will
need to load the sp package, this package provides classes and methods for handling spatial data. Remember to
install the package first if you have not done so before.

Next, we will convert the CSV into a SpatialPointsDataFrame. To do this we will need to set what the data is to be
included, what columns contain the x and y coordinates, and what projection system we are using.

[Link] 2/8
10/3/24, 10:05 AM [Link]/system/files/practical6_0.html

library("sp")

# create a [Link] SpatialPointsDataFrame


[Link] <-SpatialPointsDataFrame(houses[,3:4], houses, proj4string = CRS("+init=EPSG:2770
0"))

Before we map the points, we will create a base map using the output area boundaries.

library("tmap")

# This plots a blank base map, we have set the transparency of the borders to 0.4
tm_shape([Link]) + tm_borders(alpha=.4)

We can now add on the points as an additional tm_shape layer in our map.

To do this, we copy in the same code to make our base map, followed by a plus symbol, then enter the details for
the points data. The additional arguments for the points data can be summarised as:

tm_shape(polygon file) + tm_borders(transparency = 40%) +


tm_shape(our spatial points data frame) + tm_dots(what variable is coloured, the colour palette
and interval style)

Which is entered into R like this:

[Link] 3/8
10/3/24, 10:05 AM [Link]/system/files/practical6_0.html

# creates a coloured dot map


tm_shape([Link]) + tm_borders(alpha=.4) +
tm_shape([Link]) + tm_dots(col = "Price", palette = "Reds", style = "quantile")

We can also add in more arguments within the tm_dots() function for points like we would with tm_fill() for
polygon data. Some arguments are unique to tm_dots() . For example, the scale argument which rescales the
size of the points.

# creates a coloured dot map


tm_shape([Link]) + tm_borders(alpha=.4) +
tm_shape([Link]) + tm_dots(col = "Price", scale = 1.5, palette = "Reds", style = "quantil
e", title = "Price Paid (£)")

[Link] 4/8
10/3/24, 10:05 AM [Link]/system/files/practical6_0.html

We can also add tm_layout() and tm_compass() as we did in the previous practical.

# creates a coloured dot map


tm_shape([Link]) + tm_borders(alpha=.4) +
tm_shape([Link]) + tm_dots(col = "Price", scale = 1.5, palette = "Purples", style = "quant
ile", title = "Price Paid (£)") +
tm_compass() +
tm_layout([Link] = 1.1, [Link] = 1.4, frame = FALSE)

[Link] 5/8
10/3/24, 10:05 AM [Link]/system/files/practical6_0.html

Proportional symbols
We can also create proportional symbols in tmap. To do it in tmap, we replace the tm_dots() function with the
tm_bubbles() function. In the example below, the size and colours are both set as the price column.

# creates a proportional symbol map


tm_shape([Link]) + tm_borders(alpha=.4) +
tm_shape([Link]) + tm_bubbles(size = "Price", col = "Price", palette = "Blues", style = "q
uantile", [Link] = FALSE, [Link] = "Price Paid (£)") +
tm_layout([Link] = 1.1, [Link] = 1.4, frame = FALSE)

[Link] 6/8
10/3/24, 10:05 AM [Link]/system/files/practical6_0.html

We can also make the polygon shapefile display one of our census variables as a choropleth map as shown
below. In this example, we have also added some more parameters within the tm_bubbles() function to create thin
borders around the bubbles.

# creates a proportional symbol map


tm_shape([Link]) + tm_fill("Qualification", palette = "Reds", style = "quantile", title = "%
Qualification") +
tm_borders(alpha=.4) +
tm_shape([Link]) + tm_bubbles(size = "Price", col = "Price", palette = "Blues", style = "q
uantile", [Link] = FALSE, [Link] = "Price Paid (£)", [Link] = "black", border.l
wd = 0.1, [Link] = 0.1) +
tm_layout([Link] = 0.8, [Link] = 1.1, frame = FALSE)

This is an exported copy of the map …

[Link] 7/8
10/3/24, 10:05 AM [Link]/system/files/practical6_0.html

Saving the shapefile


Finally, we can write the newly formed [Link] shapefile to our working directory if you wish to save it for
later use.

# write the shapefile to your computer (remember to chang the dsn to your workspace)
writeOGR([Link], dsn = "C:/Users/Guy/Documents/Teaching/CDRC/Practicals", layer = "Camden
_house_sales", driver="ESRI Shapefile")

The rest of the online tutorials in this series can be found at: [Link]
data-analysis-and-visualisation-r ([Link]
visualisation-r)

[Link] 8/8

You might also like