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