本文学习目标:利用shapefile格式的文件进行整合绘图
tmap
伦敦边界图:
爱彼迎_London
World_Cities
英国边界图
OSM、Airbnb可以获取酒店信息
加载数据:
library(pacman)
p_load(sf,tmap,tmaptools,tidyverse,rgdal,raster)
qtm()
# 酒店地理信息
OSM<-st_read("G:/Rdata/greater_london_shp/gis_osm_pois_a_free_1.shp") %>%
st_transform(.,27700) %>%
#select hotel only
filter(fclass =='hotel')
#世界地理信息
Worldcities <-st_read(
"G:/Rdata/World_Cities/World_Cities.shp"
)%>%
st_transform(.,27700)
#英国轮廓图
UK_outline <- readRDS(
"G:/Rdata/gadm36_UK_0_sf.rds"
) %>%
st_transform(.,27700)
#伦敦酒店信息
Airbnb <- read_csv("G:/Rdata/airbnb_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')
#伦敦区划边界
#London Borough data is already in 27700
Londonborough <-st_read("G:/Rdata/boundaries_london/ESRI/London_Borough_Excluding_MHW.shp") %>%
st_transform(.,27700)
Londonborough
qtm(UK_outline)+
qtm(Londonborough)+
qtm(OSM)+
qtm(Airbnb,symbols.size = 0.001,symbols.col = 'red')
Hotels
Hotels1<- Hotels %>%
#at the moment each hotel is a row for the borough
#we just one one row that has number of airbnbs
group_by(., GSS_CODE, NAME)%>%
summarise(`Accomodation count`=unique(hotels_in_borough))
Hotels1
Airbnb1 <- Airbnb%>%
group_by(., GSS_CODE,NAME)%>%
summarise(`Accomodation count`=unique(hotels_in_borough))
Aribnb1
st_join类似于ArcGIS中的spatial join功能,能够统计出每个特定区域内满足条件的点
最后的绘图代码如下:
tmap_mode("plot")
breaks = c(0, 5, 12, 26, 57,500)
#plot each map
mypalette<-brewer.pal(7,"Greens")
tm1 <- tm_shape(Hotels1)+
tm_polygons("Accomodation count",
breaks=breaks,
palette=mypalette)+
tm_legend(show=FALSE)+
tm_layout(frame = FALSE)+
tm_credits("(a)", position = c(0,0.85), size=1.5)
tm1
tm2 <- tm_shape(Airbnb1)+
tm_polygons("Accomodation count",
breaks=breaks,
palette=mypalette)+
tm_legend(show=FALSE)+ #不显示图例
tm_layout(frame=FALSE)+ #不显示外框
tm_credits("(b)", position = c(0,0.85), size=1.5) #标记
tm2
tm3<-tm_shape(UK_outlinecrop) + #图形
tm_polygons(col="darkslategray1")+ #颜色
tm_layout(frame = FALSE)+ #外边框
tm_shape(UK_Cities)+ #图形
tm_symbols(col = "red", scale = .5)+ #点状符号
tm_text("CITY_NAME", xmod=-1, ymod = -0.5) #文本
tm3
legend <- tm_shape(Hotels1)+ #对应的图层
tm_polygons("Accomodation count", #图例
palette=mypalette)+ #图层配色
tm_scale_bar(position = c(0.2, 0.04), text.size = 0.8)+ #比例尺
tm_compass(north = 0, position = c(0.65,0.6),size = 3)+ #指南针
tm_layout(legend.only = TRUE,legend.position=c(0.2,0.25),asp=0.1)+
tm_credits("(c)OpenStreetMap contrbutors and Airb n b",position =c(0,0,0,0))
legend
t = tmap_arrange(tm1,tm2,tm3,legend,ncol = 2)
t
使用tm_save可以保存图像
此外我们还可以通过以下代码获得交互式的图
写在后面:
如何修改tmap的图例,指北针等地图要素