Reputation: 2520
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
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"))
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. 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'
Upvotes: 2
Views: 3770
Reputation: 314
The answer of Oleksiy, help me a lot.
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.
So
mutate(data2,type=factor(data2$project_id),c("project1","project2"))
instead of
mutate(type=factor(data$project_id),c("project1","project2")`.
Upvotes: 1
Reputation: 2520
Resolved personally.
library(leaflet)
library(dplyr)
Each icon was customized with numbers personally and was put into the working directory.
Then use 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.
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
Upvotes: 2