注意:
1.實際操作時,讀取檔案的路徑可能要重新調整:
2.目前使用 R 3.6.0 版,套件皆無須 install
atm <- read.csv("D:\\Dropbox\\R-WS\\post_atm.csv",header=T)
看看資料集的摘要統計
summary(atm)
## 縣市 鄉鎮市區 儲匯局號 實體郵局
## 新北市 :252 東區 : 75 : 718 :1972
## 臺北市 :241 中正區 : 60 000100-6: 1 36 : 2
## 高雄市 :237 中壢區 : 43 000101-0: 1 10 : 1
## 桃園市 :185 新店區 : 35 000102-3: 1 105 : 1
## 臺南市 :172 板橋區 : 32 000103-7: 1 118 : 1
## 臺中市 :170 中山區 : 27 000104-1: 1 156 : 1
## (Other):759 (Other):1744 (Other) :1293 (Other): 38
## 附設郵局 局名
## Min. : 9.00 七股郵局 : 1
## 1st Qu.:14.25 七美郵局 : 1
## Median :19.00 九如郵局 : 1
## Mean :32.64 九族文化區園區--大門公車候車亭處: 1
## 3rd Qu.:45.00 二水郵局 : 1
## Max. :93.00 二林工商--守衛室右側 : 1
## NA's :1994 (Other) :2010
## 聯絡電話 郵局地址 經度
## (02)2381-3135 : 57 大學路1號 : 5 Min. : 0.00001
## (03)422-0552轉222 : 37 指南路2段64號 : 4 1st Qu.:120.43335
## (06)226-5046 : 31 三和里1鄰華映路1號: 3 Median :120.90089
## (04)2221-5121轉245: 27 中華路1段705號 : 3 Mean :120.61047
## (05)286-0153 : 25 北寧路2號 : 3 3rd Qu.:121.49498
## (02)2992-1622轉210: 24 南台街1號 : 3 Max. :121.92419
## (Other) :1815 (Other) :1995
## 緯度 自動櫃員機 自動櫃員存提款機
## Min. : 0.000002 : 729 :1405
## 1st Qu.:23.225186 自動櫃員機:1287 自動櫃員存提款機: 611
## Median :24.235425
## Mean :24.059874
## 3rd Qu.:25.000906
## Max. :26.366849
##
## 自動補摺機 自動櫃員提款機 是否為局外ATM
## : 624 :1298 局內:1298
## 自動補摺機 : 317 自動櫃員提款機: 718 局外: 718
## 自動補摺機(營業廳):1075
##
##
##
##
## 縣市排序代號
## Min. : 0.00
## 1st Qu.: 20.00
## Median : 70.00
## Mean : 90.88
## 3rd Qu.:160.00
## Max. :240.00
##
看看資料集的前六筆資料
head(atm)
## 縣市 鄉鎮市區 儲匯局號 實體郵局 附設郵局 局名
## 1 基隆市 七堵區 基隆市 NA 基隆商工--側門旁
## 2 基隆市 七堵區 28 19 基隆監理站--服務台旁
## 3 基隆市 七堵區 臺北市 NA 陽明海運--會議室旁
## 4 基隆市 七堵區 156 85 臺灣鐵路局七堵機務段--室內
## 5 基隆市 七堵區 新北市 NA 麗景天下社區--社區警衛室旁
## 6 基隆市 七堵區 001103-3 165 87 基隆七堵郵局
## 聯絡電話 郵局地址 經度 緯度 自動櫃員機
## 1 (02)2421-0148 東新街22號 121.7175 25.09500
## 2 (02)2421-0148 實踐路296號 121.6964 25.08833
## 3 (02)2421-0148 明德一路271號 121.7111 25.09444
## 4 (02)2421-0148 崇禮街1-4號 121.7156 25.09611
## 5 (02)2421-0148 麗景一街2號 121.6725 25.08611
## 6 (02)2456-1286 基隆市七堵區明德一路171號 121.7127 25.09706 自動櫃員機
## 自動櫃員存提款機 自動補摺機 自動櫃員提款機 是否為局外ATM
## 1 自動補摺機 自動櫃員提款機 局外
## 2 自動補摺機 自動櫃員提款機 局外
## 3 自動補摺機 自動櫃員提款機 局外
## 4 自動補摺機 自動櫃員提款機 局外
## 5 自動櫃員提款機 局外
## 6 自動櫃員存提款機 自動補摺機(營業廳) 局內
## 縣市排序代號
## 1 0
## 2 0
## 3 0
## 4 0
## 5 0
## 6 0
看看資料集的維度
dim(atm)
## [1] 2016 16
安裝需要的套件
#install.packages("ggmap", repos = "http://cran.us.r-project.org")
library(ggmap)
## Loading required package: ggplot2
## Registered S3 methods overwritten by 'ggplot2':
## method from
## [.quosures rlang
## c.quosures rlang
## print.quosures rlang
## Google's Terms of Service: https://cloud.google.com/maps-platform/terms/.
## Please cite ggmap if you use it! See citation("ggmap") for details.
# install.packages("mapproj", repos = "http://cran.us.r-project.org")
library(mapproj)
## Loading required package: maps
儘管利用申請的 Google API key 繪圖, 還是可能會出現使用額度的限制問題 (Google要開始收費了!地圖可能不會出現,只剩框架)
register_google(key="AIzaSyCQeopr5djsHTYbuJG9mZm_PUrOJB3m-Hk")
map <- get_map(location = c(lon = 121, lat = 24),zoom = 7, language = "zh-TW")
## Source : https://maps.googleapis.com/maps/api/staticmap?center=24,121&zoom=7&size=640x640&scale=2&maptype=terrain&language=zh-TW&key=xxx-Hk
## Warning in get_googlemap(center = location, zoom = zoom, maptype =
## maptype, : HTTP 400 Bad Request
ggmap(map) + geom_point(aes(x = 經度, y = 緯度), data = atm)
## Warning: Removed 5 rows containing missing values (geom_point).
如何解決此問題?
暫時將地圖來源做調整,將 ‘get_map’ source (來源) 改用 Stamen Maps
map <- get_map(location = c(lon = 121, lat = 24),zoom = 7, language = "zh-TW", source = "stamen")
## Source : https://maps.googleapis.com/maps/api/staticmap?center=24,121&zoom=7&size=640x640&scale=2&maptype=terrain&key=xxx-Hk
## Warning in get_googlemap(center = location, zoom = zoom, filename =
## filename): HTTP 400 Bad Request
## Source : http://tile.stamen.com/terrain/7/105/53.png
## Source : http://tile.stamen.com/terrain/7/106/53.png
## Source : http://tile.stamen.com/terrain/7/107/53.png
## Source : http://tile.stamen.com/terrain/7/108/53.png
## Source : http://tile.stamen.com/terrain/7/105/54.png
## Source : http://tile.stamen.com/terrain/7/106/54.png
## Source : http://tile.stamen.com/terrain/7/107/54.png
## Source : http://tile.stamen.com/terrain/7/108/54.png
## Source : http://tile.stamen.com/terrain/7/105/55.png
## Source : http://tile.stamen.com/terrain/7/106/55.png
## Source : http://tile.stamen.com/terrain/7/107/55.png
## Source : http://tile.stamen.com/terrain/7/108/55.png
## Source : http://tile.stamen.com/terrain/7/105/56.png
## Source : http://tile.stamen.com/terrain/7/106/56.png
## Source : http://tile.stamen.com/terrain/7/107/56.png
## Source : http://tile.stamen.com/terrain/7/108/56.png
ggmap(map) + geom_point(aes(x = 經度, y = 緯度), data = atm)
## Warning: Removed 5 rows containing missing values (geom_point).
其他考量:安裝和載入 leaflet 套件 可參考以下以下文章:R Leaflet 地圖套件:繪製網頁互動式地圖,呈現經緯度座標資料
# install.packages("leaflet" , repos = "http://cran.us.r-project.org")
library(leaflet)
繪製捷運新埔站附近地圖
map <- leaflet()
map <- addTiles(map)
map <- addMarkers(map, lng=121.468221, lat=25.023040, popup="我在捷運新埔站")
map
整理取出位置介於 121.36 < 經度 < 121.56,24.9 < 緯度 < 25.1 的 ATM 位置資訊
經度x <- atm[atm[,9]>121.36 & atm[,9]<121.56 & atm[,10]>24.9 & atm[,10]<25.1,9]
緯度x <- atm[atm[,9]>121.36 & atm[,9]<121.56 & atm[,10]>24.9 & atm[,10]<25.1,10]
xx <- as.data.frame(cbind(經度x,緯度x))
將此範圍內的 ATM 所在處以圓圈標示
m <- leaflet(xx) %>%
addTiles() %>%
setView(lng = 121.468221, lat = 25.023040, zoom = 14)
m %>% addCircles(lng = ~經度x, lat = ~緯度x)
將此範圍內的 ATM 所在處以隨機大小與色彩的圓圈標示
set.seed(123)
point.df <- data.frame(
lat = 緯度x,
long = 經度x,
size = runif(length(經度x), 5, 10),
color = sample(colors(), length(經度x))
)
m <- leaflet(point.df) %>%
addTiles() %>%
setView(lng = 121.468221, lat = 25.023040, zoom = 14)
m %>% addCircleMarkers(radius = ~size, color = ~color, fill = FALSE)
## Assuming "long" and "lat" are longitude and latitude, respectively