We'll start by loading the necessary packages.

library("sf")
library("tmap")
library("jrSpatial")

Question 1

Load in the ukgeom data using

data(ukgeom, package = "jrSpatial")
  1. Calculate the population density for 2017 and save these in new columns. Population density is defined as the populatation divided by the area.

Bonus question: Use tmap() to create a plot to show the population density in 2017 across the U.K.

library("dplyr")
ukgeom = 
ukgeom  %>%
  mutate(pop_den_2017 = pop_2017 / area)

  # Bonus
tm_shape(ukgeom) +
  tm_borders() +
  tm_fill("pop_den_2017") + 
  tm_layout(legend.outside = TRUE)
  1. There is another dataset ukData which contains extra statistics about the UK regions such as birth rates and income. Load this data using
data(ukdata, package = "jrSpatial")

Create a new data frame called ukgeomfull which combines ukgeom with ukdata using left_join().

Does the order of the arguments matter? What happens if you put ukdata as the first argument instead of ukgeom? (Hint: Look at the class of the output using class().)

library("tidyr")
ukgeomfull = left_join(ukgeom, ukdata, by = c("id" = "geo"))
class(ukgeomfull)
# The output is still recognised as an sf spatial dataframe.

ukgeomfull_reverse = left_join(ukdata, ukgeom, by = c("geo" = "id"))
class(ukgeomfull_reverse)
# Notice this output is recognised as a data frame, but not as being an sf data frame.
# This means that functions like tmap() will not work. 
  1. Use filter() to find the regions with the following characteristics.
  2. Have a population (in 2017) greater than 1,600,000.
  3. Has a disposable income of less than 15000 or an unemployment rate of greater than 5.
ukgeomfull %>%
  filter(pop_2017 > 2500000) 

ukgeomfull %>%
  filter(income < 15000 | unemployment > 5) 
  1. Use group_by() and summarise() to answer the following questions. You may wish to drop the geometry column with st_drop_geometry() to make the output easier to view.

  2. What was the total population in 2017 for each country?

  3. What about the average life expenctancy?
ukgeomfull %>%
  st_drop_geometry() %>%
  group_by(country) %>%
    summarise(total_pop = sum(pop_2017),
              avg_life = round(mean(lifeExp)))

Question 2

Hills of UK

The ukhills dataset includes the name, height and geometry of all the Marilyns in the U.K. A Marilyns is a hill which relatively high compared to its surroundings. Load it into R using

data(ukhills, package = "jrSpatial")
  1. We'd like to know which region each of the hills are in. In order to do this, we'll need to combine the ukgeom data with the ukhills data set. There are no common columns, so this time we'll be joining by geometry. Use st_join create a new data frame called hills_uk which merges ukhills and ukgeom. Use View() to inspect the output.
hills_uk = st_join(ukhills, ukgeom)
  1. Use tm_shape() and tm_dots() to overlay the hills data on the UK regions borders.
tm_shape(hills_uk) + tm_dots() +
  tm_shape(ukgeom) + tm_borders()

Don't worry about the warning "although coordinates are longitude/latitude, st_intersects assumes that they are planar" - This is only a problem if points are massive distances away or near the poles.

  1. After joining there are NAs in the region column. Why have these occured? What do they represent? Plotting the data may help you.
# There are number of Marilyns in the dataset which are located in Ireland. 
# Ireland is not included in the ukgeom data, and therefore st_join() sets the 
# region as NA for these hills.
  1. Use filter() and arrange() to find the three highest Marilyns in Cumbria.
hills_uk %>%
  st_drop_geometry() %>%
  filter(region == "Cumbria") %>%
  arrange(desc(Metres)) %>%
  slice(1:3)
  1. Use group_by(), and summarise() to work out which region has the most Marilyns
hills_uk %>%
  st_drop_geometry() %>%
  group_by(region) %>%
  summarise(total = n()) %>%
  arrange(desc(total)) %>%
  slice(1)


jr-packages/jrSpatial documentation built on Sept. 23, 2020, 2:31 p.m.