# install.packages("leaflet") library(leaflet) # Zakladni pouziti v krocich # 1) vytvoreni widgetu (vizualni interaktivni prvek, slouzici k otevirani a ovladani ruznych programu a programovych skupin # 2) priani vrstev pomoci prislusnych funkci pr.: addTiles, addMarkers, addPolygons,... # 3) zobrazeni widgetu (vcetne vrstev) m <- leaflet() %>% addTiles() %>% addMarkers(lng = 174.768, lat = -36.852, popup = "The birthplace of R") # pridame bod/marker m # lze pouzit i klasictejsi strukturovany zapis m <- leaflet() m <- addTiles(m) m <- addMarkers(m, lng = 174.768, lat = -36.852, popup = "The birthplace of R") m # note: moznost napojeni leaflet s dazabazemi napr. maps # install.packages("maps") library(maps) mapStates = map("state", fill = TRUE, plot = FALSE) leaflet(data = mapStates) %>% addTiles() %>% addPolygons(fillColor = topo.colors(10, alpha = NULL), stroke = FALSE) mapStates = map("italy", fill = TRUE, plot = FALSE) leaflet(data = mapStates) %>% addTiles() %>% addPolygons(fillColor = topo.colors(10, alpha = NULL), stroke = FALSE) # pro prehled dat v databazi maps: help(package='maps') ###### moznosti zobrazeni v leaflet m <- leaflet() %>% setView(lng = 16.617, lat = 49.200, zoom = 12) m %>% addTiles() # setView nastavi souradnice na ktere se checme zamerit # funkce addProviderTiles(providers$_________) nastavi v podstate typ mapy # prehled: http://leaflet-extras.github.io/leaflet-providers/preview/index.html # priklad pouziti m %>% addProviderTiles(providers$CartoDB.Positron) m %>% addProviderTiles(providers$Esri.WorldImagery) m %>% addProviderTiles(providers$OpenTopoMap) m %>% addProviderTiles(providers$CartoDB.DarkMatter) ###### zobrazeni bodu (markeru) lat <- c(48.883, 49.200, 50.083) long <- c(16.633, 16.617, 14.417) df <- as.data.frame(cbind(long,lat)) # pomoci popup leaflet(data = df) %>% addTiles() %>% addMarkers(~long, ~lat, popup = c("Dolni Vestonice", "Brno", "Praha")) # pomoci label leaflet(data = df) %>% addTiles() %>% addMarkers(~long, ~lat, label = c("Dolni Vestonice", "Brno", "Praha")) # oboje leaflet(data = df) %>% addTiles() %>% addMarkers(~long, ~lat, popup = c("Dolni Vestonice", "Brno", "Praha"), label = c("Dolni Vestonice", "Brno", "Praha")) # zobrazeni stacionarnich bodu leaflet() %>% setView(lng = 16.617, lat = 49.200, zoom = 12) %>% addTiles() %>% addPopups(lng = 16.617, lat = 49.200, "Brno", options = popupOptions(closeButton = FALSE)) # moznost clustrovani bodu pokud jich je na male plose hodne - fce: clusterOptions = markerClusterOptions() # ukazka na implementovanem data.frame zemetreseni data(quakes) leaflet(quakes) %>% addTiles() %>% addMarkers(clusterOptions = markerClusterOptions()) # spojeni bodu useckou leaflet() %>% addTiles() %>% addPolylines(data = df, lng = ~long, lat = ~lat) # spojeni jenim casti dat leaflet() %>% addTiles() %>% addPolylines(data = df[1:2,], lng = ~long, lat = ~lat) leaflet() %>% addTiles() %>% addPolylines(data = df[1:2,], lng = ~long, lat = ~lat, color = "salmon", weight = 15, popup = "vzdušná trasa") # weight - tloustka cary v pixeleh # color - barva cary # popup - obdobne jako u bodu, jen pro caru # Heatmap v ramci leaflet - pozor, NEzobrazuje denzitu stejnym zpusobem jako ggmap # princip neni zalozen na statisticke analyze nybrz na zpusobu zobrazeni bodu data_vzorky <- read.table("vzorky_maps.txt",header = T, sep = "\t") str(data_vzorky) attach(data_vzorky) levels(typ) # install.packages("leaflet.extras") library(leaflet.extras) leaflet(data_vzorky) %>% addTiles() %>% addHeatmap( lng=~lon, lat=~lat, max = 0.6, blur = 60) leaflet(data_vzorky) %>% addTiles() %>% addHeatmap( lng=~lon, lat=~lat, max = 0.6, blur = 30) # dokumentace leaflet: https://rstudio.github.io/leaflet/