library(sf)
## Linking to GEOS 3.8.0, GDAL 3.0.4, PROJ 6.3.1
library(tmap)
library(tmaptools)
library(tidyverse)
## -- Attaching packages --------------------------------------- tidyverse 1.3.0 --
## v ggplot2 3.3.3 v purrr 0.3.4
## v tibble 3.0.6 v dplyr 1.0.4
## v tidyr 1.1.2 v stringr 1.4.0
## v readr 1.4.0 v forcats 0.5.1
## -- Conflicts ------------------------------------------ tidyverse_conflicts() --
## x dplyr::filter() masks stats::filter()
## x dplyr::lag() masks stats::lag()
We need to bring a few pieces of data into our R workspace. We need to reproject everything to the British National grid, EPSG 27700.
First we need to get the hotels data from the points of interest file. and reproject it to the British National grid, EPSG 27700.
OSM <- st_read("gis_osm_pois_a_free_1.shp") %>%
st_transform(., 27700) %>%
#select hotels only
filter(fclass == 'hotel')
## Reading layer `gis_osm_pois_a_free_1' from data source `C:\Users\Braden\Documents\CSC495\WK5 Assignment\gis_osm_pois_a_free_1.shp' using driver `ESRI Shapefile'
## Simple feature collection with 39569 features and 4 fields
## geometry type: MULTIPOLYGON
## dimension: XY
## bbox: xmin: -0.5108706 ymin: 51.28117 xmax: 0.322123 ymax: 51.68948
## geographic CRS: WGS 84
str(OSM)
## Classes 'sf' and 'data.frame': 980 obs. of 5 variables:
## $ osm_id : chr "4256244" "6534148" "12349799" "14397528" ...
## $ code : int 2401 2401 2401 2401 2401 2401 2401 2401 2401 2401 ...
## $ fclass : chr "hotel" "hotel" "hotel" "hotel" ...
## $ name : chr "Holiday Inn, Bloomsbury" "Premier Inn" "Premier Inn" "Dorsett Shepherds Bush London" ...
## $ geometry:sfc_MULTIPOLYGON of length 980; first list element: List of 1
## ..$ :List of 1
## .. ..$ : num [1:9, 1:2] 530147 530192 530209 530194 530183 ...
## ..- attr(*, "class")= chr [1:3] "XY" "MULTIPOLYGON" "sfg"
## - attr(*, "sf_column")= chr "geometry"
## - attr(*, "agr")= Factor w/ 3 levels "constant","aggregate",..: NA NA NA NA
## ..- attr(*, "names")= chr [1:4] "osm_id" "code" "fclass" "name"
Now we need the map of the London Boroughs. This shapefile is already in the proper projection.
Londonborough <- st_read("London_Borough_Excluding_MHW.shp") %>%
st_transform(27700)
## Reading layer `London_Borough_Excluding_MHW' from data source `C:\Users\Braden\Documents\CSC495\WK5 Assignment\London_Borough_Excluding_MHW.shp' using driver `ESRI Shapefile'
## Simple feature collection with 33 features and 7 fields
## geometry type: MULTIPOLYGON
## dimension: XY
## bbox: xmin: 503568.2 ymin: 155850.8 xmax: 561957.5 ymax: 200933.9
## projected CRS: OSGB 1936 / British National Grid
Now we need the Airbnb listings.
Airbnb <- read_csv("listings.csv") %>%
st_as_sf(., coords = c("longitude", "latitude"),
crs = 4326) %>%
st_transform(., 27700)%>%
#select entire places that are available all year
filter(room_type == 'Entire home/apt' & availability_365 =='365')
##
## -- Column specification --------------------------------------------------------
## cols(
## id = col_double(),
## name = col_character(),
## host_id = col_double(),
## host_name = col_character(),
## neighbourhood_group = col_logical(),
## neighbourhood = col_character(),
## latitude = col_double(),
## longitude = col_double(),
## room_type = col_character(),
## price = col_double(),
## minimum_nights = col_double(),
## number_of_reviews = col_double(),
## last_review = col_date(format = ""),
## reviews_per_month = col_double(),
## calculated_host_listings_count = col_double(),
## availability_365 = col_double()
## )
head(Airbnb)
Now we need to do a spatial join to associate every hotel with a borough. Then use group_by and summarize to get a count of hotels in each borough. Finally, take the logarithms of the counts.
Hotels =
st_join(Londonborough,OSM) %>%
group_by(GSS_CODE) %>%
summarize(hotels_in_borough = n()) %>%
ungroup() %>%
mutate(log_hotels = log10(hotels_in_borough))
tm1 <- tm_shape(Hotels) +
tm_polygons("hotels_in_borough",
palette="viridis") +
tm_layout(frame=FALSE) +
tm_credits("Hotels in Borough", position=c(0,0.85), size=1.5)
tm1
Repeat the map but use the log variable instead of the raw values.
# Place your code here.
prob1 <- tm_shape(Hotels) +
tm_polygons("log_hotels",
palette="viridis") +
tm_layout(frame=FALSE) +
tm_credits("Hotels in Borough", position=c(0,0.85), size=1.5)
prob1
## Problem 2
Use tmap_arrange() to put these two maps side-by-side.
# Place your code here.
tmap_arrange(tm1, prob1)
Note that we get far more detail with the log values. This is because we have large outliers.
Repeat the steps above to create a map showing the distribution of Airbnb listings throughou the boroughs. Use the logs of the values.
# Place your code here.
Airbnbloc <-
st_join(Londonborough,Airbnb) %>%
group_by(GSS_CODE) %>%
summarize(airbnb_in_borough = n()) %>%
ungroup() %>%
mutate(airbnb_in_borough_log = log10(airbnb_in_borough))
prob3 <- tm_shape(Airbnbloc) +
tm_polygons("airbnb_in_borough_log",
palette="viridis") +
tm_layout(frame=FALSE) +
tm_credits("Airbnb in Borough", position=c(0,0.85), size=1.5)
prob3
Use tmap_arrange() to show the distributions of hotels and Airbnb listings side-by-side.
# Place your code here.
tmap_arrange(prob1, prob3)