Visit the google doc for the practical http://tiny.cc/pirateroom and find the page for your breakout room.
Students are tasked with building a map with point locations for everyone in their group.
Each student needs to install the libraries on their own computer
library("ggplot2")
library("sf")
## Linking to GEOS 3.8.1, GDAL 3.1.1, PROJ 6.3.1
library("sp")
library("rnaturalearth")
library("rnaturalearthdata")
#I happen to like adding in a visual theme, so you can just paste the line:
theme_set(theme_bw())
Go to https://www.google.com/maps click twice on the map until the grey marker and box appears that lists your latitude and longitude. Copy that Y, X location (e.g. 54.768244, -1.577359) and paste it into the table.
As a group write all of your names and paste in coordinates in the table.
groupdf<-data.frame(
"name" = c("monica stephens", "andrew baldwin", "greta ferloni", "other person"),
"lat" = c(54.768244,54.977768, 52.9258854,54.8),
"long" = c(-1.577359, -1.615672,-1.5232818, -1.59))
If students were feeling fancy, they could convert this into a spatial data frame However if they did this, it would need to match up with the coordinate system of the other layers
point_geo<-st_as_sf(groupdf, coords = c(x = "long", y="lat"), crs = 4326)
Create an object that calls the function “ne_countries” with arguments for the scale and returnclass = “sf”. You will have other arguments as you manipulate the spatial data.
There were several different options for how students could do this, and it somewhat depended on what countries the members of the group were in
## This creates a global scale map that could be modified later with another command
world <- ne_countries(scale = "medium", returnclass = "sf")
plot(world)
## Warning: plotting the first 9 out of 63 attributes; use max.plot = 63 to plot
## all
## OR (this one is much slower) ##
hworld <- ne_countries(scale = "large", returnclass = "sf")
plot(hworld)
## this was a great way to do it!!
countries<-ne_countries(country = c('united kingdom','ireland'), scale='large', returnclass = "sf")
plot(countries)
## Warning: plotting the first 10 out of 94 attributes; use max.plot = 94 to plot
## all
## This would also work well ##
sp::plot(ne_countries(country = c('united kingdom','ireland'), scale='large', returnclass = "sf"),
col = 'antiquewhite',
border = 'bisque4',
xlim=c(-11,3), ylim=c(49,60.9))
## Warning: plotting the first 10 out of 94 attributes; use max.plot = 94 to plot
## all
= Now plot a map with the object you just created as the base and in the points with each member of your groups’ location from your dataframe above.
If your map is showing too much of the world and there isn’t much spatial variation in locations of members of your group… say everyone has a location in Durham, then you can zoom in on your map by adding the following line:
coord_sf(xlim=c(-11,3), ylim=c(49,60.9), expand = FALSE)
you likely would have ended the last line with a + You can modify the bounding box coordinates to aesthetically fit the scale of your data
ggplot(data = world) +
geom_sf() +
geom_point(data = groupdf, aes(x = long, y = lat, color = name), size = 3, shape = 1) +
coord_sf(xlim=c(-11,3), ylim=c(49,60.9), expand = FALSE)
I tweaked a few settings, particularly the “countries” layer we created earlier has a more detailed coast line. This will get rid of that bit of France.
old<-ggplot(data = countries) +
geom_sf() +
geom_point(data = groupdf, aes(x = long, y = lat, color = name), size = 3, shape = 1) +
coord_sf(xlim=c(-11,3), ylim=c(49,60.9), expand = FALSE) +
theme(legend.position = 'none')
I wanted to zoom in to the extreme, if I had used a high resolution or large scale map for the basemap this would be prettier.
zoom<-ggplot(data = world) +
geom_sf() +
geom_point(data = groupdf, aes(x = long, y = lat, color = name), shape = 1) +
coord_sf(xlim=c(-4,0), ylim=c(52,56), expand = FALSE) +
theme_void() +
theme(legend.position = "top")
Lastly I wanted to show students the two plots beside each other in the instructions, so this took loading one more package
library(gridExtra)
grid.arrange(old, zoom, nrow = 1)