0

I am creating a very simple map, using this code:

library(dplyr) library(leaflet)

library(dplyr)
library(leaflet)

Rwanda$Quartile<-cut(Rwanda$lpro12x,quantile(Rwanda$lpro12x),include.lowest=TRUE,labels=FALSE)
color_function <- colorFactor(c("gray25", "gray50", "gray75", "gray0"), domain = NULL)

base <- leaflet() %>% 
  addTiles() %>%
  addScaleBar() %>% 
  setView(lat = mean(Rwanda$latitude), lng = mean(Rwanda$longitude), zoom = 8)

Rwandamap <- base %>% 
     addLegend("bottomright", 
                colors = c("gray25", "gray50", "gray25", "gray0"),
                opacity = 0.8,
                labels= c("Lowest Quartile", "Second Quartile", "Third Quartile", "Highest Quartile"),
                title= "Deaths") %>% 
  addCircleMarkers(lng = Rwanda$longitude, lat = Rwanda$latitude,
                   color = "black", fillColor = color_function(Rwanda$Quartile), fillOpacity=1, stroke=T, weight=1, popup= Rwanda$popup, 
                   label = Rwanda$label) %>% 
  setView(lat = mean(Rwanda$latitude), lng = mean(Rwanda$longitude), zoom = 8)

Rwandamap

But, while the colors show up in the markers, they do not show up in the legend:

enter image description here

What completely befuddles me is that when I replace "gray75" with "red" in the colors list for the legend (but make no other changes, the red shows up, but the others do not:

library(dplyr)
library(leaflet)

Rwanda$Quartile<-cut(Rwanda$lpro12x,quantile(Rwanda$lpro12x),include.lowest=TRUE,labels=FALSE)
color_function <- colorFactor(c("gray25", "gray50", "gray75", "gray0"), domain = NULL)

base <- leaflet() %>% 
  addTiles() %>%
  addScaleBar() %>% 
  setView(lat = mean(Rwanda$latitude), lng = mean(Rwanda$longitude), zoom = 8)

Rwandamap <- base %>% 
     addLegend("bottomright", 
                colors = c("red", "gray50", "gray25", "gray0"),
                opacity = 0.8,
                labels= c("Lowest Quartile", "Second Quartile", "Third Quartile", "Highest Quartile"),
                title= "Deaths") %>% 
  addCircleMarkers(lng = Rwanda$longitude, lat = Rwanda$latitude,
                   color = "black", fillColor = color_function(Rwanda$Quartile), fillOpacity=1, stroke=T, weight=1, popup= Rwanda$popup,, 
                   label = Rwanda$label) %>% 
  setView(lat = mean(Rwanda$latitude), lng = mean(Rwanda$longitude), zoom = 8)

Rwandamap

enter image description here

For purposes of replication, here is a truncated version of my dataframe:

name<- c("MUTONGO", "MURURU", "MUHARI", "KAMEMBE", "WINTEKO")
latitude<-c(-2.53022, -2.51104, -2.4483, -2.457, -2.517)
longitude<- c(28.87806, 28.88937, 28.9076, 28.9046, 28.9065)
lpro12x<- c(5.8406415, 5.4161005, 5.5174527, 5.9506426, 5.4930615)


Rwanda<-data.frame(name, latitude, longitude, lpro12x)

I did try replacing colors = c("gray75", "gray50", "gray25", "gray0") with colors = color_function, but I get an error ("colors' and 'labels' must be of the same length")

0

1 Answer 1

0

Replace the colors' names with colors' code in hex.

name<- c("MUTONGO", "MURURU", "MUHARI", "KAMEMBE", "WINTEKO")
latitude<-c(-2.53022, -2.51104, -2.4483, -2.457, -2.517)
longitude<- c(28.87806, 28.88937, 28.9076, 28.9046, 28.9065)
lpro12x<- c(5.8406415, 5.4161005, 5.5174527, 5.9506426, 5.4930615)

Rwanda<-data.frame(name, latitude, longitude, lpro12x)


library(dplyr)
library(leaflet)

Rwanda$Quartile<-cut(Rwanda$lpro12x,quantile(Rwanda$lpro12x),include.lowest=TRUE,labels=FALSE)
color_function <- colorFactor(c("gray25", "gray50", "gray75", "gray0"), domain = NULL)

base <- leaflet() %>% 
  addTiles() %>%
  addScaleBar() %>% 
  setView(lat = mean(Rwanda$latitude), lng = mean(Rwanda$longitude), zoom = 8)

base %>% 
  addCircleMarkers(lng = Rwanda$longitude, lat = Rwanda$latitude,
                   color = "black", fillColor = color_function(Rwanda$Quartile), 
                   fillOpacity=1, 
                   stroke=T, weight=1, popup= Rwanda$popup,
                   label = Rwanda$label) %>% 
  addLegend("bottomright", 
            colors = c("#FF0000", "#7F7F7F", "#404040", "#030303"),
            opacity = 0.8,
            labels= c("Lowest Quartile", "Second Quartile", "Third Quartile", "Highest Quartile"),
            title= "Deaths")
1
  • Great, thanks, that works. Still curious why "grey50" etc worked fine re the marker colors, but hardly a pressing issue.
    – gdanning
    Commented Mar 14 at 23:20

Not the answer you're looking for? Browse other questions tagged or ask your own question.