我需要根据它们的类型在Shiny传单地图点上进行映射 - 总共四种类型,使用相同的不同颜色的标记。
我查了一下:
https://rstudio.github.io/leaflet/markers.html
这看起来像我的回复,但我无法解决它:( Change color of leaflet marker
此代码在答案中分享
library(dplyr)
library(leaflet)
mutate(quakes, group = cut(mag, breaks = c(0, 5, 6, Inf), labels = c("blue",
"green", "orange"))) -> mydf
### I edit this png file and created my own marker.
### https://raw.githubusercontent.com/lvoogdt/Leaflet.awesome-
markers/master/dist/images/markers-soft.png
quakeIcons <- iconList(blue = makeIcon("/Users/jazzurro/Documents/Stack
Overflow/blue.png", iconWidth = 24, iconHeight =32),
green = makeIcon("/Users/jazzurro/Documents/Stack
Overflow/green.png", iconWidth = 24, iconHeight =32),
orange = makeIcon("/Users/jazzurro/Documents/Stack
Overflow/orange.png", iconWidth = 24, iconHeight =32))
leaflet(data = mydf[1:100,]) %>%
addTiles() %>%
addMarkers(icon = ~quakeIcons[group])
我基本上有相同的代码
# Create our own custom icons
teamIcons <- iconList(
A = makeIcon("C:/Map/Asset 20.png", iconWidth = 18, iconHeight = 18),
B = makeIcon("C:/Map/Asset 21.png", iconWidth = 18, iconHeight = 18),
C = makeIcon("C:/Map/Asset 22.png", iconWidth = 18, iconHeight = 18),
D = makeIcon("C:/Map/Asset 23.png", iconWidth = 18, iconHeight = 18))
data1&lt; - data%&gt;%mutate(type = factor(data $ type),c(“A”,“B”,“C”,“D”))
然后我就这样做了
m <- leaflet(data=data) %>%
addProviderTiles(providers$Stamen.TonerLite) %>%
addMarkers(~data1$long, ~data1$lat, icon = ~teamIcons[data1$type], popup
state_popup)
addMarkers的数据来自另一个数据集 - data1,而不是数据。当我使用真棒图标时,它不会产生问题。当我在目录中使用自己的图标时,我在地图上有一个普通的蓝色标记。
无效的下标类型'logical'
答案 0 :(得分:1)
但是我在mutate函数中发现了两个错误,它错过了要转换的datafame的名称,并且在创建data2
时,我猜该因子函数必须与data2
数据帧一起使用。 / p>
所以
mutate(data2,type=factor(data2$project_id),c("project1","project2"))
而不是
mutate(type=factor(data$project_id),c("project1","project2")`.
答案 1 :(得分:0)
亲自解决。
library(leaflet)
library(dplyr)
每个图标都是个人自定义的,并被放入工作目录。
然后使用icon_list()
# Create our own custom icons
icon_list <- iconList(
project1 = makeIcon("C:/Map/1.png", iconWidth = 24, iconHeight = 30),
project2 = makeIcon("C:/Map/2.png", iconWidth = 24, iconHeight = 30)
和你一样多
project1,project2等当然在数据集列中匹配相同的名称,在数据集中每个项目应该有long和lat。
然后
data2 <- data %>% mutate(type = factor(data$project_id), c("project1",
"project2")
在服务器中,简单的代码将是这样的
m <- leaflet(data=data) %>%
addProviderTiles(providers$Stamen.TonerLite) %>%
addMarkers(~data2$long, ~data2$lat, icon=~icon_list[data2$project_id],
popup = state_popup)