This is my data set:
start_stations <-
data.frame(
station = c("StreeterDr", "MichiganAve", "WellsSt"),
lat = c(41.89228, 41.90096, 41.91213),
lng = c(-87.61204,-87.62378,-87.63466),
n = c(23000, 56780, 34520)
)
This is the code I tried to plot the map using these lat and lng coordinates and add color variations to stations (locations) according to its count and label each location using name and count.
install.packages(c("leaflet", "sp"))
library(leaflet)
library(sp)
install.packages("sf")
library(sf)
lon <- start_stations$lng
lat <- start_stations$lat
name <- start_stations$station
count <- start_stations$n
dfs <- as.data.frame(cbind(lon,lat,name,count))
dfs <- sf::st_as_sf(dfs, coords = c("lon","lat"), crs = 4326)
getColor <- function(dfs) {
sapply(dfs$count, function(count) {
if(count <= 20000) {
"green"
} else if(count <= 30000) {
"orange"
} else {
"red"
} })
}
icons <- awesomeIcons(
icon = 'ios-close',
iconColor = 'black',
library = 'ion',
markerColor = getColor(dfs)
)
leaflet(dfs) %>% addTiles() %>%
addAwesomeMarkers(~lon, ~lat, icon=icons, popup = ~as.character(name), label=~as.character(count))
Everything works correctly except display the icon. I want to display the icons with the color respective to the count.