空間分析 第三週

助教 杜承軒 2021.03.08

library(sf);library(tmap);library(pals);library(cartography);library(grid);library(ggplot2)
setwd("D:/1092SA/Data")
EPA=st_read("EPA_STN1.shp", options="ENCODING=BIG5")
TW=st_read("Popn_TWN2.shp", options="ENCODING=BIG5")
TW$POP=TW$A0A14_CNT+TW$A15A64_CNT+TW$A65UP_CNT
plot(TW["POP"])

plot(TW["POP"],breaks = "jenks", nbreaks = 6, pal=brewer.blues(6))

qtm(TW, fill="POP")

qtm(TW, fill="POP", fill.title="人口", title="地圖", fill.palette="-Blues")

brk=getBreaks(v=TW$POP, nclass = 6, method = "jenks") #如何分類
tm_shape(TW) +tm_polygons("POP", title = "人數", palette = "-GnBu", breaks = brk)

tm_shape(TW) + tm_polygons("POP")

tm_shape(EPA) + tm_dots(size=.5)

tm_shape(TW) + tm_polygons("POP")+
  tm_shape(EPA) + tm_dots(col='red',size=.3)+tm_layout(frame = F)

qtm(TW)+qtm(EPA,symbols.size = 1,symbols.col = "red")

qtm(TW, fill="POP",fill.palette="-Blues")+tm_shape(EPA)+tm_dots(col='red',size=.3)+tm_layout(frame = F)

tmap1=tm_shape(TW) + tm_polygons("POP")
tmap2=tm_shape(EPA) + tm_dots(size=.5)

tmap_arrange(tmap1,tmap2,ncol=2,nrow=1)

grid.newpage()
pushViewport(viewport(layout=grid.layout(2,2)))
print(tmap1, vp=viewport(layout.pos.col = 1, layout.pos.row=1))
## Some legend labels were too wide. These labels have been resized to 0.64, 0.64, 0.64, 0.64, 0.64. Increase legend.width (argument of tm_layout) to make the legend wider and therefore the labels larger.
print(tmap2, vp=viewport(layout.pos.col = 2, layout.pos.row=2))