2

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

This is the output I got

Everything works correctly except display the icon. I want to display the icons with the color respective to the count.

KalsaraL
  • 39
  • 7

1 Answers1

1

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 .

  • RStudio 2021.09.0 Build 351 © 2009-2021 RStudio, PBC
  • newest version of packages sf and sp
# ----------------------------------------------------------------------
# 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))

enter image description here

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))
help-info.de
  • 6,695
  • 16
  • 39
  • 41
  • I tried both codes. But location icon isn't still appear. – KalsaraL Nov 26 '21 at 14:09
  • @KalsaraL - see my edit (version info and `library(dplyr) # add for use of mutate)`. `library(dplyr)` is required for the mutate function. Seems something is wrong with the software you installed. Also, answers to other of your questions on SO (https://stackoverflow.com/a/70119845/1981088) apparently didn't fully work for you. – help-info.de Nov 26 '21 at 16:18
  • @ help-info.de- I used Kaggle note book for execute this code. I'm also think there is a version problem not a problem of code. How can I overcome this issue. Thank You! – KalsaraL Nov 26 '21 at 17:49
  • @help-info.de-Are there any additional library needs for icons in Leaflet Markers – KalsaraL Nov 27 '21 at 02:51
  • @KalsaraL - AFAIK noo additional library. See my edit. – help-info.de Nov 27 '21 at 14:37
  • @help-info.de- When I remove "icon=icons" from addAwesomeMarkers() function, Marker icons display with the count and name but in same color. This method isn't facilitate to depict color segment. – KalsaraL Nov 27 '21 at 15:51