R09228001 NTU GOEG M.S. 楊宇翔 YANG

Update R to 4.1.1

rm(list=ls(all=TRUE))
#install.packages('devtools') #assuming it is not already installed
library(devtools)
#install_github('andreacirilloac/updateR')
library(updateR)
#updateR()

load in package

#install.packages("sf")
#install.packages("cartography")
#install.packages("tmap")
#install.packages("leaflet")
#install.packages("mapview")
#install.packages("RColorBrewer")
#install.packages("leafpop")
library(sf)
library(dplyr)
library(ggplot2)
library(cartography)
library(tmap)
library(leaflet)
library(mapview)
library(RColorBrewer)
library(leafpop) 
library(reshape2)
library(rgdal)
library(xts)
library(TSstudio)

load in data

setwd("~/Desktop/110-1/110-1 data visualization/w6_tmap/GISdata")
library(readxl)
s1_2020=read_excel("s1_2020.xlsx")

In-class Practice #1

1. Using real estate transaction data (S1_2020.csv), creating an interactive plot to show the locations of objects in 大安區 where transaction date was on the weekend.
2. Using colors of symbols to differentiate 公寓 、華夏、住宅大樓
3. Using size of symbols to represent the unit price (單價)
#篩選出大安區
#grepl(): 模糊文字對位

daan <- subset(s1_2020,grepl("大安區",Address))
#篩選出周末
daan$交易年月日 <- daan$交易年月日 %>% as.numeric()
daan$交易年月日 <- as.Date(as.character(daan$交易年月日+19110000),format='%Y%m%d')

# 抓出是週末的資料的位置
index <- weekdays(daan$交易年月日)=="週六"|weekdays(daan$交易年月日)=="週日"

#用資料的位置,抓出週末的資料
daan <- daan[index,]
#抓出華廈、公寓、住宅大樓
daan <- subset(daan,grepl("華廈",建物型態)|grepl("公寓",建物型態)|grepl("住宅大樓",建物型態))

把data.frame 資料,利用座標系統EPSG:3826(TWD97),和X、Y座標,轉換成幾何圖型資料。

pts_sf = st_as_sf(daan, coords = c("Response_X", "Response_Y"),
                  crs = 3826)

篩選欄位

library(dplyr)
pts_sf2 <- select(pts_sf, c("id","都市土地使用分區","主要用途","移轉層次" ,"主建物面積","單價元平方公尺","建物型態","建物型態"))
pts_sf2$單價元平方公尺=as.numeric(pts_sf2$單價元平方公尺)
pts_sf2=na.omit(pts_sf2)

plotting 1

tmap_mode("view")
map2 = tm_shape(pts_sf2) +
tm_dots(size="單價元平方公尺", col="建物型態", alpha = 0.6, id = "id")
map2

In-class practice 2.

Based on the previous interactive plot, add an interactive
都市土地使用分區、主要用途、移轉層次、主建物面積、單價。
pts_sf = st_as_sf(daan, coords = c("Response_X", "Response_Y"),
                  crs = 3826)
library(dplyr)
pts_sf2 <- select(pts_sf, c("id","都市土地使用分區","主要用途","移轉層次" ,"主建物面積","單價元平方公尺","建物型態","建物型態"))
pts_sf2$單價元平方公尺=as.numeric(pts_sf2$單價元平方公尺)
pts_sf2=na.omit(pts_sf2)

plotting 2

tmap_mode("view")
map2 = tm_shape(pts_sf2) +
tm_dots(size="單價元平方公尺", col="建物型態", alpha = 0.6, id = "id",popup.vars=c("都市土地使用分區: "="都市土地使用分區","主要用途: " = "主要用途","移轉層次: " = "移轉層次","主建物面積: " = "主建物面積","單價: " = "單價元平方公尺"))
map2