【R】leaflegend
2021年5月24日
1. はじめに
leaflegendは、cssファイルを準備することなくleafletにlegendを付加できるパッケージです。
2. インストール
CRANからインストールできます。
install.packages("leaflegend")
3. つかってみる
パッケージを読み込み、addLegend*()するだけです。こちらのleafpopと併せて使ってみます。
library(leaflegend)
library(leafpop)
library(leaflet)
data(quakes)
quakes[['group']] <- sample(c('A', 'B', 'C'), nrow(quakes), replace = TRUE)
factorPal <- colorFactor('Dark2', quakes$group)
leaflet() %>%
addTiles() %>%
addCircleMarkers(
data = quakes,
lat = ~ lat,
lng = ~ long,
radius = ~ mag * 1.5,
popup = popupTable(quakes),
color = ~ factorPal(group),
opacity = ~ depth / 1000,
fillOpacity = ~ depth / 1000
) %>%
addLegendFactor(
pal = factorPal,
title = htmltools::tags$div('Factor', style = 'font-size: 18px; color: blue;'),
labelStyle = 'font-size: 18px; font-weight: bold;',
orientation = 'horizontal',
values = quakes$group,
position = 'bottomleft',
shape = 'circle',
width = 20,
height = 20
)

4. さいごに
最近、leafletのextensionが多く知ることができて、leafletの使い勝手がさらに向上してます。