简体   繁体   English

闪亮的传单地图上的自定义标记

[英]Custom markers on Shiny leaflet map

I need to map on Shiny leaflet map points based on their type - four types in total with the same marker of a different color. 我需要根据它们的类型在“闪亮”传单地图点上进行映射-共有四种类型,并且具有相同标记的颜色也不同。

I checked this: 我检查了这个:

https://rstudio.github.io/leaflet/markers.html https://rstudio.github.io/leaflet/markers.html

This looks like my response, but I cannot fix it :( Change color of leaflet marker 这看起来像我的回应,但我无法解决它:( 更改传单标记的颜色

This code was shared in the answer 该代码在答案中共享

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])

I basically have the same code 我基本上有相同的代码

# 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 <- data %>% mutate(type = factor(data$type), c("A", "B", "C", "D")) data1 <-数据%>%mutate(type = factor(data $ type),c(“ A”,“ B”,“ C”,“ D”))

Then I just do 那我就做

m <- leaflet(data=data) %>%
addProviderTiles(providers$Stamen.TonerLite) %>%
addMarkers(~data1$long, ~data1$lat, icon = ~teamIcons[data1$type], popup 
state_popup) 

Data for addMarkers is taken from another dataset - data1, not data. addMarkers的数据取自另一个数据集-data1,而不是数据。 When I use awesome icons, it does not create problems. 当我使用很棒的图标时,它不会产生问题。 When I use my own icons from the directory, I have an ordinary blue marker on a map. 当我使用目录中的图标时,地图上会显示一个普通的蓝色标记。

Invalid subscript type 'logical' 下标类型“逻辑”无效

The answer of Oleksiy , help me a lot. Oleksiy答案 ,对我有很大帮助。

But I found two errors in the mutate function, it miss the name of the datafame to transform and, as you create data2 , I guess that factor function have to work with data2 dataframe. 但是我在mutate函数中发现了两个错误,它错过了要转换的datafame的名称,并且在创建data2 ,我猜想factor函数必须与data2 dataframe一起使用。

So 所以

mutate(data2,type=factor(data2$project_id),c("project1","project2"))

instead of 代替

mutate(type=factor(data$project_id),c("project1","project2")`.

Resolved personally. 亲自解决。

library(leaflet)
library(dplyr) 

Each icon was customized with numbers personally and was put into the working directory. 每个图标都使用数字进行了个性化定制,并被放入工作目录中。

Then use icon_list() 然后使用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)

etc as many as you have 等你有多少

project1, project2 etc match the same names in the dataset column of course and in the dataset each project should have long and lat. project1,project2等在课程的数据集列中匹配相同的名称,并且在数据集中,每个项目应具有long和lat。

Then 然后

data2 <- data %>% mutate(type = factor(data$project_id), c("project1", 
"project2") 

In server, the simple code will be something like this 在服务器中,简单的代码将如下所示

m <-  leaflet(data=data) %>% 
  addProviderTiles(providers$Stamen.TonerLite) %>%
  addMarkers(~data2$long, ~data2$lat, icon=~icon_list[data2$project_id], 
  popup = state_popup)

Thanks to this question 由于这个问题

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM