아래는 기본 layer를 불러오는 코드
library(leaflet)
## Warning: package 'leaflet' was built under R version 3.5.2
m <- leaflet() %>% addTiles()
m
# 지도의 가운데 및 줌 설정
m %>% setView(lng=127,lat= 37, zoom = 18)
# 매끄러운 팬줌을 활용해 줌 설정
m%>%flyTo(lng=127,lat=37,zoom=18)
#지도의 경계 설정
m %>% fitBounds(lng1=126, lat1=37, lng2=128, lat2=38)
#팬줌을 활용한 지도의 경계 설정
leaflet(options = leafletOptions(minZoom = 11)) %>% addTiles()%>%flyTo(lng=127,lat=37,zoom=18)%>%flyToBounds(lng1=126, lat1=37, lng2=128, lat2=38)
#지도 범위 제한주기
#leafletOptions는 최소 줌을 제한하고 setmaxbounds로 범위를 벗어나지 못하게 할 수 있다.
leaflet(options = leafletOptions(minZoom = 11)) %>% addTiles()%>%setView(lng=127,lat= 37, zoom = 18)%>%setMaxBounds(lng1=127.5, lat1=37.5, lng2=128, lat2=38)
#지도에 위의 조건을 제거하는 코드
m%>%clearBounds()
addControl : 임의의 HTML 컨트롤을지도에 추가 addTiles :지도에 타일 레이어 추가 addWMSTiles :지도에 WMS 타일 레이어 추가 addPopups :지도에 팝업 추가 addMarkers :지도에 마커 추가 addLabelOnlyMarkers : 레이블 만 마커를지도에 추가합니다. addCircleMarkers :지도에 원 마커 추가 highlightOptions : 마우스 오버시 모양을 강조 표시하는 옵션 addCircles :지도에 원을 추가합니다. addPolylines :지도에 폴리 라인 추가 addRectangles :지도에 직사각형을 추가합니다. addPolygons :지도에 폴리곤 추가하기 addGeoJSON :지도에 GeoJSON 레이어 추가 addTopoJSON :지도에 TopoJSON 레이어 추가
#범주를 자바스크립트로 줄 수 있음
m%>% addControl("<img src='https://leafletjs.com/examples/custom-icons/leaf-green.png'>green<br/>
<img src='https://leafletjs.com/examples/custom-icons/leaf-shadow.png'>shadow",position='topleft')
m %>% addPopups(127, 37, "Here is the <b>lat =127 lng= 37</b>")
Rlogo <- file.path(R.home("doc"), "html", "logo.jpg")
m %>% addMarkers( 127, 37, icon = list(
iconUrl = Rlogo, iconSize = c(10, 7.6)
), popup = "R was born here!"
)
#원 추가
m %>% addCircles(127, 37, radius =1,color='#ff0000')
m %>% addCircleMarkers(127, 37, color = "#ff0000")
# 사각형
m %>% addRectangles(lng1=126, lat1=37, lng2=128, lat2=38,
color = "red", fill = FALSE, dashArray = "5,5", weight = 3
)
#하이라이트 옵션
m %>% addRectangles(lng1=126, lat1=37, lng2=128, lat2=38,
color = "red", dashArray = "5,5", weight = 3
,highlightOptions = highlightOptions(color = "white", weight = 2,
bringToFront = TRUE)
)
# 선 긋기
m %>% addPolylines(c(126,128),c(37,38))
#다각형
m %>% addPolygons(c(126,128,127),c(37,38,37), layerId = "foo")
# geoJSON
seattle_geojson <- list(
type = "Feature",
geometry = list(
type = "MultiPolygon",
coordinates = list(list(list(
c(126,37),c(128,38),c(127,37)
)))
),
properties = list(
name = "Ballard",
population = 48000,
# You can inline styles if you want
style = list(
fillColor = "yellow",
weight = 2,
color = "#000000"
)
),
id = "ballard"
)
m %>% setView(127, 37, zoom = 13) %>% addGeoJSON(seattle_geojson)
http : // {s} .basemaps.cartocdn.com / dark_nolabels / {z} / {x} / {y} .png에서 dark_nolabels 에 해당하는 부분을 변경하면 다양한 지도를 가져와 사용 가능. light_all, dark_all, light_nolabels등이 있다.
# use the Dark Matter layer from CartoDB
leaflet() %>% addTiles("http://{s}.basemaps.cartocdn.com/light_all/{z}/{x}/{y}.png",
attribution = paste(
"© <a href=\"http://openstreetmap.org\">OpenStreetMap</a> contributors", "© <a href=\"http://cartodb.com/attributions\">CartoDB</a>"
)) %>% setView(-122.36, 47.67, zoom = 10)
leaflet() %>% addTiles("http://{s}.basemaps.cartocdn.com/dark_all/{z}/{x}/{y}.png",
attribution = paste(
"© <a href=\"http://openstreetmap.org\">OpenStreetMap</a> contributors",
"© <a href=\"http://cartodb.com/attributions\">CartoDB</a>"
)
) %>% setView(-122.36, 47.67, zoom = 10)
library(maps)
mapStates = map("world",regions =c("South Korea",'North Korea'), fill = TRUE, plot = FALSE)
leaflet(data = mapStates)%>% addTiles() %>%
addPolygons(fillColor = topo.colors(10, alpha = NULL), stroke = T)
mapStates = map("state", fill = TRUE, plot = FALSE)
leaflet(data = mapStates) %>% addTiles() %>%
addPolygons(fillColor = topo.colors(10, alpha = NULL), stroke = FALSE)
# provide a data frame to leaflet()
rand_lng <- function(n = 10) rnorm(n, -93.65, .01)
rand_lat <- function(n = 10) rnorm(n, 42.0285, .01)
categories <- LETTERS[1:10]
df <- data.frame(
lat = rand_lat(100), lng = rand_lng(100), size = runif(100, 5, 20),
category = factor(sample(categories, 100, replace = TRUE), levels = categories),
value = rnorm(100)
)
m <- leaflet(df) %>% addTiles()
m %>% addCircleMarkers(~lng, ~lat, radius = ~size)
m %>% addCircleMarkers(~lng, ~lat, radius = runif(100, 4, 10), color = c("red"))
# Discrete colors using the "RdYlBu" colorbrewer palette, mapped to categories
RdYlBu <- colorFactor("RdYlBu", domain = categories)
m %>% addCircleMarkers(~lng, ~lat, radius = ~size,
color = ~RdYlBu(category), fillOpacity = 0.5)
# Continuous colors using the "Greens" colorbrewer palette, mapped to value
greens <- colorNumeric("Greens", domain = NULL)
m %>% addCircleMarkers(~lng, ~lat, radius = ~size,
color = ~greens(value), fillOpacity = 0.5)
library(devtools)
install_github('qkdrk777777/kma2')
## Skipping install of 'kma2' from a github remote, the SHA1 (d7b37a54) has not changed since last install.
## Use `force = TRUE` to force installation
library(kma2)
greenLeafIcon <- makeIcon(
iconUrl = "https://leafletjs.com/examples/custom-icons/leaf-green.png",
iconWidth = 38, iconHeight = 95,
iconAnchorX = 22, iconAnchorY = 94,
shadowUrl = "https://leafletjs.com/examples/custom-icons/leaf-shadow.png",
shadowWidth = 50, shadowHeight = 64,
shadowAnchorX = 4, shadowAnchorY = 62
)
leaflet(area[1:5,7:8]) %>% addTiles() %>%
addMarkers(~lon, ~lat, icon = greenLeafIcon)
예전에 공간통계 수업 때 하이퍼링크 나 위에서 언급한 하이퍼링크2에 레스터 자료 및 지도자료를 시각화 한예제가 있으니 참조(추후 정리예정).