The final task is to draw a map of the world with our data on it. If you’ve followed the previous tutorials, you should be able to complete the task below. It might be a bit challenging, but try to tackle one part of the task at a time, and make sure your results are the expected ones.
You need to know two more things to do this task:
The maps
package helps plot maps. First, make sure you’ve installed this package:
install.packages("maps")
Then you can plot a map of the world like this:
library(maps)
map()
This produces a map of the world and displays it in the plot window. We can add stuff on top with functions like points
and abline
:
# draw a map of the world
map()
# add a point at longitude 0, latitude 52
# (and two other arguments to make it a filled red circle)
points(0, 52, col = 2)
# add a horizontal line at the equator
abline(h = 0, col = 2)
The function points
can take vectors of longitudes and latitudes and plot several points at once. In some imaginary data:
points(d$longitude, d$latitude, col = d$pointColour)
In the code above, note the extra argument in points
and abline
called “col”. This sets which colour the point or line should be. You can give points different colours by supplying a vector of values (one for each point).
Colours in R can be specified using:
rainbow
, heat.colors
or the RColorBrewer
package.The file “societies.csv” includes extra data about each society, including the geographic location (taken from D-PLACE, mainly based on Glottolog location data). The column id
in societies.csv is an ID that can be matched to the column society_id
in the main roof shape data.
Download societies.csv here: societies.csv. (https://github.com/seannyD/IntroToRForAnthropology/raw/main/data/societies.csv).
Task: Make a map of the world with a point added for each society, and where the colour of the point indicates whether it has a flat or non-flat roof. The map should not contain societies for which we don’t have rainfall data.
Write a script for this - don’t try and do it directly in the console.
Below is a set of steps you’ll need to take. Before looking at them, see if you can work these out yourself - braking the problem down is an important skill to develop.
You’ll need to:
Now we also need to assign a colour to each row, based on whether the rooves are flat or not flat.
pointColour
in the main data that is e.g. “green” if the roof type is flat and “red” if the roof type is not flat.Now we can draw the map:
points
also has a col
argument, where you can give it a vector which contains the colour for each point. You can put the pointColour
data here.Here’s my plot, with a legend for reference. Click on the code
button to the right to reveal one possible answer.
# Set the working directory
setwd("~/Documents/Teaching/CARDIFF/RCourse/IntroToRForAnthropologists/analysis/")
# Load the maps library
library(maps)
#Load the roof data
d = read.csv("../data/RoofShapeData.csv", stringsAsFactors = F)
# Creae a variable called 'roofShape', just for convenience
d$roofShape <- d$code_label
# Work out flat/ non-flat
d$flatRoof = d$roofShape == "Flat"
# Load rainfall data
rainfallData = read.csv("../data/RainfallData_all.csv",stringsAsFactors = F)
# Load society data
societyData = read.csv("../data/societies.csv",stringsAsFactors = F)
# Add the rainfall data into the main data source
d$rainfall <- rainfallData[match(d$society_id, rainfallData$society_id) , ]$code
# Filter out any data with NA values
d = d[!is.na(d$rainfall),]
# Add the geographic coordinates
d$longitude <- societyData[match(d$society_id, societyData$id) , ]$Long
d$latitude <- societyData[match(d$society_id, societyData$id) , ]$Lat
# Assign a colour based on the roof shape
# First, we just create a new variable that is "red" for all rows:
d$pointColour = "red"
# Now we set any rows where the roof shape is flat to "green":
# (remember that 'flatRoof' is a boolean vector)
d[d$flatRoof,]$pointColour = "green"
# Create a new pdf file
pdf("myMap.pdf", width=15, height=10)
# draw a map
map()
# add points
points(d$longitude, d$latitude, col= d$pointColour)
# add a legend
legend(-45,-40, legend=c("Flat","Not Flat"),
col=c("green","red"), pch=16, ncol=2)
# We're done with plotting, so close the file connection
dev.off()
FALSE quartz_off_screen
FALSE 2
Back to the index