knitr::opts_chunk$set( collapse = TRUE, comment = "#>" )
library(htmlwidgets) library(htmltools) library(dplyr) library(leaflethex) library(leaflet)
df <- tibble( lat = rnorm(100), lng = rnorm(100) ) df2 <- tibble( lat = 42.9634 + rnorm(1000), lng = -85.6681 + rnorm(1000) ) df3 <- tibble( lat = 51.8126 + rnorm(1000), lng = 5.8372 + rnorm(1000) ) df4 <- tibble( lat = 30.2460 + rnorm(1000), lng = -87.7008 + rnorm(1000) )
leaflet::leaflet(df, width = 600, height = 300) %>% addTiles() %>% addHexbin( radius = 19, lowEndColor = "yellow", highEndColor="red") %>% addHexbin( data = df2, radius = 16, lowEndColor = "lightblue", highEndColor="purple") %>% addHexbin( data = df3, uniformSize = TRUE, lowEndColor = 'yellow', highEndColor = 'red') %>% addHexbin( data = df4, uniformColor = 'green', radius = 18)
By default, addHexbin()
uses hexagans to represent counts -- the number of
latitude/longitude pairs in each hexagonal bin. It is also possible to represent
the value of a summary function applied to some variable in the data. Summary
functions currently supported are min, max, mean, and median. To use a summary
function, simply supply the summary function and the name of the variable containing
the data to summarize.
kids_df <- tibble( lat = 33.2460 + rnorm(1000, sd = 0.5), lng = -89.7008 + rnorm(1000, sd = 0.5), sample_size = sample(x = 1:5, size = 1000, replace = TRUE) ) temp_df <- tibble( lat = 42.2460 + rnorm(1000, sd = 0.5), lng = -87.7008 + rnorm(1000, sd = 0.5), temp = sample(x = 20:100, size = 1000, replace = TRUE) ) both_df <- tibble( lat = 40.0150 + rnorm(1000, sd = 0.5), lng = -105.2705 + rnorm(1000, sd = 0.5), sample_size = sample(x = 1:5, size = 1000, replace = TRUE), temp = sample(x = 20:100, size = 1000, replace = TRUE) ) leaflet::leaflet(kids_df, width = 600, height = 300) %>% addTiles() %>% addHexbin( radius = 19, lowEndColor = "yellow", highEndColor="red", sizeSummaryFunction = "sum", sizevar = "sample_size" ) %>% addHexbin( data = temp_df, radius = 19, lowEndColor = "blue", highEndColor="red", sizeSummaryFunction = "mean", sizevar = "temp" ) %>% addHexbin( data = both_df, radius = 18, lowEndColor = "white", highEndColor="darkgreen", sizeSummaryFunction = "sum", sizevar = "sample_size", colorSummaryFunction = "median", colorvar = "temp" )
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.