報告主題
- 主題說明:
- 應用:使用者可以分為社會大眾、學校教師。
- 社會大眾可使用此地圖進行學校資料的查詢、
課堂或工作報告以及個人進修,而且網站資料的內容
淺顯易懂的優勢,操作上也不會太過困難
- 教師則可以利用網站做教育應用,視學生考試成績將
此地圖應用當作一個輔助工具,引導學生瞭解自己成績落
點會相對應的學校位置。
套件
# r
library(readxl)
library(leaflet)
library(maptools)
## 載入需要的套件:sp
## Checking rgeos availability: FALSE
## Please note that 'maptools' will be retired by the end of 2023,
## plan transition at your earliest convenience;
## some functionality will be moved to 'sp'.
## Note: when rgeos is not available, polygon geometry computations in maptools depend on gpclib,
## which has a restricted licence. It is disabled by default;
## to enable gpclib, type gpclibPermit()
library(rgdal)
## Please note that rgdal will be retired by the end of 2023,
## plan transition to sf/stars/terra functions using GDAL and PROJ
## at your earliest convenience.
##
## rgdal: version: 1.5-32, (SVN revision 1176)
## Geospatial Data Abstraction Library extensions to R successfully loaded
## Loaded GDAL runtime: GDAL 3.3.2, released 2021/09/01
## Path to GDAL shared files: C:/Users/USER/AppData/Local/R/win-library/4.2/rgdal/gdal
## GDAL binary built with GEOS: TRUE
## Loaded PROJ runtime: Rel. 7.2.1, January 1st, 2021, [PJ_VERSION: 721]
## Path to PROJ shared files: C:/Users/USER/AppData/Local/R/win-library/4.2/rgdal/proj
## PROJ CDN enabled: FALSE
## Linking to sp version:1.4-7
## To mute warnings of possible GDAL/OSR exportToProj4() degradation,
## use options("rgdal_show_exportToProj4_warnings"="none") before loading sp or rgdal.
- This is my first conclusion
- This is my second conclusion
設定匯入資料
# r, include=FALSE, fig.width=3, fig.height=3
path = "C:/Users/USER/Desktop/yulong/"
file = paste0(path, "data/2.xls")
df= readxl::read_excel(file, col_names=TRUE)
colnames(df) = c("學校級別", "代碼", "學校名稱", "縣市別","鄉鎮市區","地址","電話", "網址","體系別", "地區","經度","緯度")
建立網址函數
# r, echo=FALSE, fig.width=3, fig.height=3
url_txt = function(variable){
return(paste0("<a href='", variable, "'> ", variable, "</a>"))
}
座標顯現資料
df$popup = paste0("學校名稱:", df$學校名稱, "<br/>",
"縣市別:", df$縣市別, "<br/>",
"地址:", df$地址, "<br/>",
"電話:", df$電話,"<br/>",
"網址:", url_txt(df$網址), "<br/>")
繪圖-呈顯圖標在地圖上
# r, eval=FALSE, fig.width=3, fig.height=3
leaflet(data=df) %>%
addTiles() %>%
setView(lng=mean(df$經度), lat=mean(df$緯度), zoom=7.4) %>% # 設定中心點
# 使用 addTiles() 函數添加 tiles (圖磚,亦即方形地圖),
# 並使用默認參數默認即是 OpenStreetMap,即街區 Tiles。
addMarkers(lng=~經度,
lat=~緯度,
popup=~popup)
繪圖群聚地圖(可勾選想要縣市)
alpha = 0.8
towns_color = data.frame("town"=unique(df$縣市別),
"color"=topo.colors(length(unique(df$縣市別)), alpha=alpha))
pal = leaflet::colorFactor(palette=towns_color$color,
domain=towns_color$town)
overlayGroups = df$縣市別 %>% unique()
# overlayGroups = unique(df$縣市別)
map4 = leaflet() %>%
addTiles() %>%
addCircleMarkers(data=df,
lng=~經度,
lat=~緯度,
color=~pal(縣市別),
group=df$縣市別,
radius=3,
fillOpacity=alpha,
popup=~popup) %>%
addLayersControl(overlayGroups=overlayGroups) %>%
# addLegend("bottomright", pal=pal, values=~縣市別) # doesn't work
addLegend(position="topright", pal=pal, values=df$縣市別) # works fine
map4
file4 = paste0(path, "html/school_map4.html")
htmlwidgets::saveWidget(map4, file4)
自我評分
- 說明 :(1) 利用 R 之 plotly (dygraphs) 套件建立 candlestick
chart+布林帶+移動平均線+成交量應用與結果分析。(60分) (2)
加分項:蠟燭圖RMarkdown(+5分) (3) 利用 R 之 leaflet
套件建立統計地理資訊查詢。(完成第二個子主題)(+10分) (4)
加分項:統計地理資訊查詢RMarkdown(+5分)、shinny(+5分) (5) 上課筆記(+5分)
==>期末報告總分90分