AirBnb investigation
library(sf)
## Linking to GEOS 3.6.1, GDAL 2.2.3, PROJ 4.9.3
library(tmap)
library(tmaptools)
library(plyr)
library(tidyverse)
## -- Attaching packages -------------------------------- tidyverse 1.2.1 --
## v ggplot2 3.2.1 v purrr 0.3.2
## v tibble 2.1.3 v dplyr 0.8.3
## v tidyr 1.0.0 v stringr 1.4.0
## v readr 1.3.1 v forcats 0.4.0
## -- Conflicts ----------------------------------- tidyverse_conflicts() --
## x dplyr::arrange() masks plyr::arrange()
## x purrr::compact() masks plyr::compact()
## x dplyr::count() masks plyr::count()
## x dplyr::failwith() masks plyr::failwith()
## x dplyr::filter() masks stats::filter()
## x dplyr::id() masks plyr::id()
## x dplyr::lag() masks stats::lag()
## x dplyr::mutate() masks plyr::mutate()
## x dplyr::rename() masks plyr::rename()
## x dplyr::summarise() masks plyr::summarise()
## x dplyr::summarize() masks plyr::summarize()
#reading in OSM file
OSM <- st_read("Data/gis_osm_pois_a_free_1.shp")
## Reading layer `gis_osm_pois_a_free_1' from data source `C:\Users\cex\Documents\Smart Cities and Urban Analytics\GIS\Week-8\Data\gis_osm_pois_a_free_1.shp' using driver `ESRI Shapefile'
## Simple feature collection with 35359 features and 4 fields
## geometry type: MULTIPOLYGON
## dimension: XY
## bbox: xmin: -0.5108706 ymin: 51.28117 xmax: 0.322123 ymax: 51.68948
## epsg (SRID): 4326
## proj4string: +proj=longlat +datum=WGS84 +no_defs
#reading in LondonBorough
LondonBorough <- st_read("Data/ESRI/London_Borough_Excluding_MHW.shp")
## Reading layer `London_Borough_Excluding_MHW' from data source `C:\Users\cex\Documents\Smart Cities and Urban Analytics\GIS\Week-8\Data\ESRI\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
## epsg (SRID): NA
## proj4string: +proj=tmerc +lat_0=49 +lon_0=-2 +k=0.999601272 +x_0=400000 +y_0=-100000 +datum=OSGB36 +units=m +no_defs
#reading in Airbnb data
Airbnb <- read_csv("Data/listings.csv")
## Parsed with 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_character(),
## reviews_per_month = col_double(),
## calculated_host_listings_count = col_double(),
## availability_365 = col_double()
## )
#reading in world cities
Worldcities <- st_read("data/World_Cities/World_Cities.shp")
## Reading layer `World_Cities' from data source `C:\Users\cex\Documents\Smart Cities and Urban Analytics\GIS\Week-8\Data\World_Cities\World_Cities.shp' using driver `ESRI Shapefile'
## Simple feature collection with 2540 features and 13 fields
## geometry type: POINT
## dimension: XY
## bbox: xmin: -176.1516 ymin: -54.792 xmax: 179.2219 ymax: 78.2
## epsg (SRID): 4326
## proj4string: +proj=longlat +datum=WGS84 +no_defs
#reading in UK outline
UKoutline <- st_read("Data/gadm36_GBR_shp/gadm36_GBR_0.shp")
## Reading layer `gadm36_GBR_0' from data source `C:\Users\cex\Documents\Smart Cities and Urban Analytics\GIS\Week-8\Data\gadm36_GBR_shp\gadm36_GBR_0.shp' using driver `ESRI Shapefile'
## Simple feature collection with 1 feature and 2 fields
## geometry type: MULTIPOLYGON
## dimension: XY
## bbox: xmin: -13.69139 ymin: 49.86542 xmax: 1.764168 ymax: 61.52708
## epsg (SRID): 4326
## proj4string: +proj=longlat +datum=WGS84 +no_defs
#plotting xy data
#changing airbnb to sf
Airbnb <- st_as_sf(Airbnb, coords = c("longitude", "latitude"), crs = 4326)
#reproject
OSM <- st_transform(OSM, 27700)
Worldcities <- st_transform(Worldcities, 27700)
UKoutline <- st_transform(UKoutline, 27700)
Airbnb <- st_transform(Airbnb, 27700)
#we don't need to reproject LDN but it doesn't have CRS
LondonBorough <- st_transform(LondonBorough, 27700)
#selecting hotels only
OSM <- OSM[OSM$fclass == 'hotel',]
Airbnb <- Airbnb[Airbnb$room_type == 'Entire home/apt' & Airbnb$availability_365 == '365',]
#this selects rows that have a certain value in the given column
#making a function for the join
joinfun <- function(data1, data2) {
#join OSM and London boroughs
joined <- st_join(data1, data2, join = st_within)
#count the number of hotels per borough
countno <- as.data.frame(plyr::count(joined$GSS_CODE))
#join the count back to the borough layer
counted <- left_join(data2, countno, by = c("GSS_CODE"="x"))
return(counted)
}
#use the function for hotels
Hotels <- joinfun(OSM, LondonBorough)
#then for Airbnb
Airbnb1 <- joinfun(Airbnb, LondonBorough)
worldcities2 <- Worldcities[Worldcities$CNTRY_NAME=='United Kingdom'&
Worldcities$CITY_NAME=='Birmingham'|
Worldcities$CITY_NAME=='London'|
Worldcities$CITY_NAME=='Edinburgh',]
newbb <- c(xmin=-296000, ymin=5408, xmax=655696, ymax=1000000)
UKoutlinecrop <- st_crop(UKoutline$geometry, newbb)
#arranging the plots with tmap
breaks = c(0,5,12,26,57,286)
#change the column name from freq for the legend
colnames(Hotels)[colnames(Hotels)=='freq'] <- "Accom count"
#plot each map
tm1 <- tm_shape(Hotels) +
tm_polygons("Accom count", breaks=breaks)+
tm_legend(show=FALSE)+
tm_layout(frame=FALSE)+
tm_credits("(a)", position = c(0,0.85),size=1.5)
tm1
## Warning: Values have found that are higher than the highest break

tm2 <- tm_shape(Airbnb1)+
tm_polygons("freq", breaks = breaks)+
tm_legend(show=FALSE)+
tm_layout(frame=FALSE)+
tm_credits("(b)", position = c(0,0.85), size=1.5)
tm2

tm3 <- tm_shape(UKoutlinecrop)+
tm_polygons(col="darkslategray1")+
tm_layout(frame=FALSE)+
tm_shape(worldcities2)+
tm_symbols(col='red', scale = .5)+
tm_text("CITY_NAME", xmod=-1,ymod=-0.5)
tm3

legend <- tm_shape(Hotels)+
tm_polygons("Accom count")+
tm_scale_bar(position = c(0.2,0.04), text.size=0.6)+
tm_compass(north=0, position=c(0.65,0.6))+
tm_layout(legend.only=TRUE, legend.position=c(0.2,0.25),asp=0.1)+
tm_credits("(c) OpenStreetMap contributors and Airbnb", position = c(0.0,0.0))
t=tmap_arrange(tm1, tm2, tm3, legend, ncol = 2)
t
## Warning: Values have found that are higher than the highest break

#we can also arrange the maps using the grid package
library(grid)
grid.newpage()
pushViewport(viewport(layout = grid.layout(2,2)))
print(tm1, vp=viewport(layout.pos.col=1, layout.pos.row=1, height=10))
## Warning: Values have found that are higher than the highest break
print(tm2, vp =viewport(layout.pos.col=2, layout.pos.row=1, height =5))
print(tm3, vp = viewport(layout.pos.col=1, layout.pos.row=2, height = 5))
print(legend, vp=viewport(layout.pos.col=2, layout.pos.row=2, height =5))

#we can output the map using
tmap_save(t, 'hotelsandairbnbR.png')
## Warning: Values have found that are higher than the highest break
## Map saved to C:\Users\cex\Documents\Smart Cities and Urban Analytics\GIS\Week-8\hotelsandairbnbR.png
## Resolution: 2100 by 2100 pixels
## Size: 7 by 7 inches (300 dpi)
#we can also make an interactive map
tmap_mode("view")
## tmap mode set to interactive viewing
tm_shape(Airbnb1)+
tm_polygons("freq", breaks=breaks)
#we can take this further so we can select the layers on an interactive map
# library for pop up boxes
library(leafpop)
library(leaflet)
#join data
ti<-st_join(Airbnb1, Hotels, join = st_equals)
ti<-st_transform(ti,crs = 4326)
#remove the geometry for our pop up boxes to avoid the geometry field
ti2 <- ti
st_geometry(ti2) <- NULL
popairbnb <- popupTable(ti2, zcol=c("NAME.x", "GSS_CODE.x", "freq"))
pophotels <- popupTable(ti2, zcol=c("NAME.x", "GSS_CODE.x", "Accom count"))
# set the colour palettes using our previously defined breaks
# the colour palettes are the same using the same breaks
# but different data
pal <- colorBin(palette = "YlOrRd", domain=ti2$freq, bins=breaks)
pal2 <- colorBin(palette = "YlOrRd", domain=ti2$`Accom count`, bins=breaks)
map<- leaflet(ti) %>%
# add basemap options
addTiles(group = "OSM (default)") %>%
addProviderTiles(providers$Stamen.Toner, group = "Toner") %>%
addProviderTiles(providers$Stamen.TonerLite, group = "Toner Lite") %>%
addProviderTiles(providers$CartoDB.Positron, group = "CartoDB")%>%
#add our polygons, linking to the tables we just made
addPolygons(color="white",
weight = 2,
opacity = 1,
dashArray = "3",
popup = popairbnb,
fillOpacity = 0.7,
fillColor = ~pal(freq),
group = "Airbnb")%>%
addPolygons(fillColor = ~pal(`Accom count`),
weight = 2,
opacity = 1,
color = "white",
dashArray = "3",
popup = pophotels,
fillOpacity = 0.7,group = "Hotels")%>%
# add a legend
addLegend(pal = pal2, values = ~`Accom count`, group = c("Airbnb","Hotel"),
position ="bottomleft") %>%
# specify layers control
addLayersControl(
baseGroups = c("OSM (default)", "Toner", "Toner Lite", "CartoDB"),
overlayGroups = c("Airbnb", "Hotels"),
options = layersControlOptions(collapsed = FALSE)
)
## Warning in pal(`Accom count`): Some values were outside the color scale and
## will be treated as NA
# plot the map
map