ggmap
Pecu PPT / Code :
https://docs.google.com/…/1pO9H2MwVIUi6YJtrSDSPTFvR2P…/edit…
http://datascienceandr.org/…/03-RVisualization-04-Javascrip…
# ggmap 上課教材
https://blog.gtwang.org/r/r-ggmap-package-spatial-data-visualization/
# 如果遇到版本問題:Update R 到 3.3.3
Mac :https://cran.r-project.org/src/base/R-3/
Windows : https://cran.rstudio.com/bin/windows/base/old/3.3.3/
# Windows 出現 f error 在 console 將ggplot2 版本改成2.2.0
library(devtools)
install_github("hadley/[email protected]")
# 輸入地址轉換經緯度
http://gps.uhooamber.com/address-to-lat-lng.html
# Code -- 繪製基本地圖
library(ggplot2)
library(ggmap)
library(mapproj)
map <- get_map(location = 'Taiwan',zoom = 7,language = "zh-TW")
map <- get_map(location =c(lan = 120.233937,lat = 22.993013),zoom = 10, language = "zh-TW")
ggmap(map)
# Code -- 將資料畫在地圖上
首先從政府資料開放平臺上下載紫外線即時監測資料的 csv 檔,接著將資料讀進 R 中
uv <-read.csv("Whatyouwant.csv",encoding="big-5")
# 繪製經緯度
lon.deg <- sapply((strsplit(as.character(uv$WGS84Lon), ",")), as.numeric)
uv$lon <- lon.deg[1, ] + lon.deg[2, ]/60 + lon.deg[3, ]/3600
lat.deg <- sapply((strsplit(as.character(uv$WGS84Lat), ",")), as.numeric)
uv$lat <- lat.deg[1, ] + lat.deg[2, ]/60 + lat.deg[3, ]/3600
# 把資料加到地圖中
library(ggmap)
map <- get_map(location = 'Taiwan', zoom = 7)
ggmap(map) + geom_point(aes(x = lon, y = lat, size = UVI), data = uv)
# Google 地圖的標記(marker)與路徑(path)
d <- function(x=-95.36, y=29.76, n,r,a){
round(data.frame(
lon = jitter(rep(x,n), amount = a),
lat = jitter(rep(y,n), amount = a)
), digits = r)
}
df <- d(n = 50,r = 3,a = .3) # random 製造經緯度
map <- get_googlemap(markers = df, path = df,, scale = 2)
ggmap(map)
# 小提醒 -- lon 跟 lat 的表示型式需修改成:121.123455 (原為121,123,455的話)
# 老師提供完整code
#install.packages("devtools")
#library(devtools)
#install.packages("ggplot2")
library(ggplot2)
library(ggmap)
library(mapproj)
map <- get_map(location ='taiwan', zoom =9, language ="zh-TW")
map <- get_map(location = c(lon =-95.36, lat =29.76),
zoom =10, language ="zh-TW")
ggmap(map)
uv <- read.csv("UV_20151116152215.csv", encoding="big-5")
uv$lon <- uv$WGS84Lon
uv$lat <- uv$WGS84Lat
map <- get_map(location ='Taiwan', zoom =7)
ggmap(map) +
geom_point(aes(x = lon, y = lat, size = uv), data = uv) +
scale_size(range = c(0,3))
#d <- function(x=-95.36, y=29.76, n,r,a){
# round(data.frame(
# lon = jitter(rep(x,n), amount = a),
# lat = jitter(rep(y,n), amount = a)
# ), digits = r)
#}
#df1 <- d(n = 50,r = 3,a = .3)
#map <- get_googlemap(markers = df1, path = df1, scale = 2)
#ggmap(map)
df2 <- uv[,5:6]
names(df2) <- c('lon','lat')
map2 <- get_googlemap(center ='Taiwan', zoom =7,
markers = df2, path = df2, scale =2)
ggmap(map2)
mu <- c(-95.3632715,29.7632836)
nDataSets <- sample(4:10,1)
chkpts <- NULL
for(k in1:nDataSets){
a <- rnorm(2); b <- rnorm(2);
si <-1/3000* (outer(a,a) + outer(b,b))
chkpts <- rbind(chkpts,
cbind(MASS::mvrnorm(rpois(1,50), jitter(mu,.01), si), k))
}
chkpts <- data.frame(chkpts)
names(chkpts) <- c("lon","lat","class")
chkpts$class <- factor(chkpts$class)
qplot(lon, lat, data = chkpts, colour = class)
df2$uv <- as.factor(as.integer(uv$uv))
qplot(lon, lat, data = df2, colour = uv)
df2$air <- sample(1:10, length(df2$uv), replace=T)
ggmap(get_map(location ='taiwan', zoom =7), extent ="panel") +
geom_point(aes(x = lon, y = lat, colour = factor(uv), size = air), data = df2) +
scale_size(range = c(0,6))
# 進階設定顏色 http://ggplot2.tidyverse.org/reference/scale_gradient.html