Reputation: 39
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.
Upvotes: 2
Views: 1397
Reputation: 7260
EDIT:
The information of using R in Kaggle was shared later by asker. When using Kaggle there are problems with the use of addAwesomeMarkers
. Other markers like e.g. addCircleMarkers
are working well.
As shown in the map below following code is working with following environment on Windows 10 .
# ----------------------------------------------------------------------
# sample for awesomeIcons - color by value
# ----------------------------------------------------------------------
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)
)
library(leaflet)
library(sp)
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)
# --- character to integer -----------------------------------------------------
dfs$count <- as.integer(start_stations$n)
getColor <- function(dfs) {
sapply(dfs$count, function(count) {
if(count <= 25000) {
"green"
} else if(count <= 35000) {
"orange"
} else {
"red"
} })
}
icons <- awesomeIcons(
icon = 'ios-close',
iconColor = 'black',
library = 'ion',
markerColor = getColor(dfs)
)
leaflet() %>% addTiles() %>%
addAwesomeMarkers(data=dfs, ~lon, ~lat, icon=icons, popup = ~as.character(name), label=~as.character(count))
Please note following edited lines and some edited values in your getColor <- function(dfs)
# --- character to integer -------------------------------------------------
dfs$count <- as.integer(start_stations$n)
...
leaflet() %>% addTiles() %>%
addAwesomeMarkers(data=dfs, ~lon, ~lat, icon=icons, popup = ~as.character(name), label=~as.character(count))
or use another solution for your needs like this:
library(dplyr) # add for use of mutate
# --- character to integer -------------------------------------------------
dfs$count <- as.integer(start_stations$n)
# --- add color group column -----------------------------------------------
dfs <- mutate(dfs, group = cut(count, breaks = c(0, 25000, 35000, 99000, Inf),
labels = c("green", "darkred", "red", "purple"), include.lowest = TRUE))
dfs
icons <- awesomeIcons(icon = "ios-close",
iconColor = "yellow",
library = "ion",
markerColor = dfs$group)
leaflet() %>% addTiles() %>%
addAwesomeMarkers(data=dfs,~lon, ~lat, icon=icons, popup = ~as.character(name), label=~as.character(count))
Upvotes: 1