Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/66.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R中自定义标记的传单图例_R_Leaflet_Legend - Fatal编程技术网

R中自定义标记的传单图例

R中自定义标记的传单图例,r,leaflet,legend,R,Leaflet,Legend,我有一个R闪亮的应用程序,它使用传单创建一个交互式地图。在此地图上,分类变量用于指定不同类型的点,并使用自定义标记(不同图标,取决于因子级别)进行可视化 我想做的是在情节中添加一个图例,但是让图例显示各种标记图标,而不是纯色。报告没有涵盖这一点 我遇到过另一个例子——但它是用JavaScript完成的,我不知道如何翻译它/是否可以翻译成R语言。有人知道如何实现这一点吗 一个基本的可复制示例: library(leaflet) # Sample Data data(quakes) quakes

我有一个R闪亮的应用程序,它使用传单创建一个交互式地图。在此地图上,分类变量用于指定不同类型的点,并使用自定义标记(不同图标,取决于因子级别)进行可视化

我想做的是在情节中添加一个图例,但是让图例显示各种标记图标,而不是纯色。报告没有涵盖这一点

我遇到过另一个例子——但它是用JavaScript完成的,我不知道如何翻译它/是否可以翻译成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)
图书馆(传单)
#样本数据
数据(地震)
地震%
addMarkers(~long,~lat,icon=leaf图标)
虽然图标是在addLegend()中使用的,但Yihui建议使用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")
图书馆(传单)
#样本数据
数据(地震)
地震%
addMarkers(~long,~lat,icon=leafcicons)%>%
addControl(html=html\u图例,position=“bottomleft”)
链接
  • 绿色:
  • 红色:
  • 橙色:
产生:


回应上述评论:您可以更改图例中图标的大小,而不管您定义的初始大小如何。你所要做的就是添加

style='width:(所需宽度)px;高度:(所需高度)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")
图书馆(传单)
#样本数据
数据(地震)
地震%
addMarkers(~long,~lat,icon=leafcicons)%>%
addControl(html=html\u图例,position=“bottomleft”)

如果可行,最简单的方法可能就是获取JavaScript的源代码。尝试过这个方法后,效果很好,但图例中的图标确实很大。有没有办法在html_legend code或addControl()中调整它们的大小?是的,图标大小在icons()调用中定义,在上面的调用中,它们被设置为38px宽和95px高。