R 中自定义标记的传单图例
Leaflet Legend for Custom Markers in R
我有一个使用 Leaflet 创建交互式地图的 R Shiny 应用程序。在此地图上,分类变量用于指定不同类型的点,并使用自定义标记(不同的图标,取决于因子级别)进行可视化。
我想做的是在情节中添加一个图例,但让图例显示各种标记图标而不是纯色。 legends tutorial 不包括这一点。
我遇到了另一个 - 但它是在 JavaScript 中完成的,我不确定如何翻译 it/if 它可以翻译为在 R 中工作。任何人知道如何做到这一点吗?
一个基本的可复制示例:
library(leaflet)
# Sample Data
data(quakes)
quakes <- quakes[1:10,]
# Choose Icon:
leafIcons <- icons(
iconUrl = ifelse(quakes$mag < 4.6,
"http://leafletjs.com/docs/images/leaf-green.png",
"http://leafletjs.com/docs/images/leaf-red.png"
),
iconWidth = 38, iconHeight = 95,
iconAnchorX = 22, iconAnchorY = 94)
# Produce Map:
leaflet(data = quakes) %>% addTiles() %>%
addMarkers(~long, ~lat, icon = leafIcons)
虽然图标的使用是在 addLegend() 中 not currently implemented,但易辉建议使用 addControl(),使用原始 html - 效果很好!
library(leaflet)
# Sample Data
data(quakes)
quakes <- quakes[1:10,]
# Choose Icon:
leafIcons <- icons(
iconUrl = ifelse(quakes$mag < 4.6,
"http://leafletjs.com/examples/custom-icons/leaf-green.png",
"http://leafletjs.com/examples/custom-icons/leaf-red.png"
),
iconWidth = 38, iconHeight = 95,
iconAnchorX = 22, iconAnchorY = 94)
html_legend <- "<img src='http://leafletjs.com/examples/custom-icons/leaf-green.png'>green<br/>
<img src='http://leafletjs.com/examples/custom-icons/leaf-red.png'>red"
# Produce Map:
leaflet(data = quakes) %>% addTiles() %>%
addMarkers(~long, ~lat, icon = leafIcons) %>%
addControl(html = html_legend, position = "bottomleft")
链接
- 绿色:http://leafletjs.com/examples/custom-icons/leaf-green.png
- 红色:http://leafletjs.com/examples/custom-icons/leaf-red.png
- 橙色:http://leafletjs.com/examples/custom-icons/leaf-orange.png
产生:
回应上面的评论:您可以更改图例中图标的大小,而不管您定义的初始大小如何。您所要做的就是添加
style='width:(desired_width)px;height:(desired_height)px';
到 HTML 部分。
具体来说,您的代码需要:
library(leaflet)
# Sample Data
data(quakes)
quakes <- quakes[1:10,]
# Choose Icon:
leafIcons <- icons(
iconUrl = ifelse(quakes$mag < 4.6,
"http://leafletjs.com/docs/images/leaf-green.png",
"http://leafletjs.com/docs/images/leaf-red.png"
),
iconWidth = 38, iconHeight = 95,
iconAnchorX = 22, iconAnchorY = 94)
html_legend <- "<img src='http://leafletjs.com/docs/images/leaf-green.png'
style='width:10px;height:10px;'>green<br/>
<img src='http://leafletjs.com/docs/images/leaf-red.png'
style='width:10px;height:10px;'>red"
# Produce Map:
leaflet(data = quakes) %>% addTiles() %>%
addMarkers(~long, ~lat, icon = leafIcons) %>%
addControl(html = html_legend, position = "bottomleft")
我有一个使用 Leaflet 创建交互式地图的 R Shiny 应用程序。在此地图上,分类变量用于指定不同类型的点,并使用自定义标记(不同的图标,取决于因子级别)进行可视化。
我想做的是在情节中添加一个图例,但让图例显示各种标记图标而不是纯色。 legends tutorial 不包括这一点。
我遇到了另一个
一个基本的可复制示例:
library(leaflet)
# Sample Data
data(quakes)
quakes <- quakes[1:10,]
# Choose Icon:
leafIcons <- icons(
iconUrl = ifelse(quakes$mag < 4.6,
"http://leafletjs.com/docs/images/leaf-green.png",
"http://leafletjs.com/docs/images/leaf-red.png"
),
iconWidth = 38, iconHeight = 95,
iconAnchorX = 22, iconAnchorY = 94)
# Produce Map:
leaflet(data = quakes) %>% addTiles() %>%
addMarkers(~long, ~lat, icon = leafIcons)
虽然图标的使用是在 addLegend() 中 not currently implemented,但易辉建议使用 addControl(),使用原始 html - 效果很好!
library(leaflet)
# Sample Data
data(quakes)
quakes <- quakes[1:10,]
# Choose Icon:
leafIcons <- icons(
iconUrl = ifelse(quakes$mag < 4.6,
"http://leafletjs.com/examples/custom-icons/leaf-green.png",
"http://leafletjs.com/examples/custom-icons/leaf-red.png"
),
iconWidth = 38, iconHeight = 95,
iconAnchorX = 22, iconAnchorY = 94)
html_legend <- "<img src='http://leafletjs.com/examples/custom-icons/leaf-green.png'>green<br/>
<img src='http://leafletjs.com/examples/custom-icons/leaf-red.png'>red"
# Produce Map:
leaflet(data = quakes) %>% addTiles() %>%
addMarkers(~long, ~lat, icon = leafIcons) %>%
addControl(html = html_legend, position = "bottomleft")
链接
- 绿色:http://leafletjs.com/examples/custom-icons/leaf-green.png
- 红色:http://leafletjs.com/examples/custom-icons/leaf-red.png
- 橙色:http://leafletjs.com/examples/custom-icons/leaf-orange.png
产生:
回应上面的评论:您可以更改图例中图标的大小,而不管您定义的初始大小如何。您所要做的就是添加
style='width:(desired_width)px;height:(desired_height)px';
到 HTML 部分。
具体来说,您的代码需要:
library(leaflet)
# Sample Data
data(quakes)
quakes <- quakes[1:10,]
# Choose Icon:
leafIcons <- icons(
iconUrl = ifelse(quakes$mag < 4.6,
"http://leafletjs.com/docs/images/leaf-green.png",
"http://leafletjs.com/docs/images/leaf-red.png"
),
iconWidth = 38, iconHeight = 95,
iconAnchorX = 22, iconAnchorY = 94)
html_legend <- "<img src='http://leafletjs.com/docs/images/leaf-green.png'
style='width:10px;height:10px;'>green<br/>
<img src='http://leafletjs.com/docs/images/leaf-red.png'
style='width:10px;height:10px;'>red"
# Produce Map:
leaflet(data = quakes) %>% addTiles() %>%
addMarkers(~long, ~lat, icon = leafIcons) %>%
addControl(html = html_legend, position = "bottomleft")