knitr::opts_chunk$set( collapse = TRUE, comment = "#>", fig.width = 6, fig.height = 3 )
library(leaflethex) library(tibble) library(dplyr)
It also contains a function
addHexbin() for applying a hexbin layer to a leaflet map
If you are interested in using the hexbin plugin that comes along with
leaflethex, the function
addHexbin() can be used which has a similar API to
the rest of the Leaflet R functions that add layers to leaflet maps such as
The default use bins geopoints into hexagonal regions and used size and/or color to represent the number of points in each bin.
# Randomly generated data points data_points <- tibble( lat = 42.9634 + rnorm(1000), lng = -85.6681 + rnorm(1000) ) # Create map with data points leaflet::leaflet(data_points) %>% addTiles() %>% addHexbin()
Radius, color, and opacity of hexagon are easily modified
using arguments to
addHexbin(). As with other leaflet functions,
data is inherited from the map unless specified with the
For a more complete description of
addHexbin(), see the function
help and package vignettes.
data_points <- tibble( lat = 42.9634 + rnorm(1000), lng = -85.6681 + rnorm(1000) ) data_points2 <- tibble( lat = 42.9634 + rnorm(1000), lng = -95.6681 + rnorm(1000) ) leaflet::leaflet(rbind(data_points, data_points2)) %>% addTiles() %>% addHexbin(data = data_points) %>% addHexbin(data = data_points2, lowEndColor = "yellow", highEndColor = "red")
Leaflethex also provides a mechanism for creating hooks to arbitrary
code that modfies a leaflet map so that it can be used from within R
using the leaflet R package
Place this code in a new file and place the file in a new folder. (In the example below, this file will be called `example.js`.)
mapwith the keyword
thisso that the loaded plugin can attach to the given map instead of creating a new map.
Note: if the JS Code makes extensive use of DOM manipulation more work may be needed to make it compatible.
(In the example below, this file will be called `deps.js`.)
```r new_plugin <- pluginFactory( "Name of Plugin", "path/of/parent/folder/", "leaflet-script.js", "deps.js", "stylesheet.css") # Create a basic leaflet map leaflet::leaflet() %>% addTiles() %>% new_plugin() ```
When simply creating the plugin and running it locally the `pluginFactory()` will work as expected in this example; however, when creating a package there are a few things that need tweaking: a. Any time `system.file()` is used, the `package` parameter must be set to the new package like so: ```r addPluginName <- pluginFactory( "Name of Plugin", system.file("folder_containing_js_file", "", package = "your_package_name"), # Add the package parameter "name_of_file.js", "dependencies_required.js", "stylesheet_for_js.css") ``` a. Secondly, the js folder must be inside the `inst` folder in order to be installed when the package is installed, otherwise Rstudio will complain that the js folder doesn'
Any scripts or data that you put into this service are public.
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.