Distinguish categorical variables by color using leaflets in R

I am making an interactive population map in the DC area using a “booklet” in R, and I want to use different colors to distinguish 7 different communities. My variables: address, community, gender and name (for pop-ups). I used some code from TrendCt and Ripples .

How would I customize my code to show different colors for different communities, and maybe even, how could I change the shades to distinguish by gender in the communities?

Here is my code:

###########################################################################
#################### D.C. Community Map in R ##############################
###########################################################################

## Retrieve Data and Download Package
# Use import data set dropdown menu to import data correctly
# Community Map.csv 

# Load packages
library(dplyr)
library(ggmap)
library(leaflet)

# Define new data set
ysa <- Community.Map

## Generate dataset with coordinate variables 
## Averages 10 minutes to render on my i5 processor
ysa %>% 
  mutate(address=paste(gender, sep=", ", ward)) %>%
  select(address) %>% 
  lapply(function(x){geocode(x, output="latlon")})  %>% 
  as.data.frame %>% 
  cbind(ysa) -> ysa1

ysa %>%  
  mutate(popup_info=paste(sep = "<br/>", paste0("<b>","<i>", ward,"<i>", "      
  </b>"), name)) %>% 
  mutate(lon=ifelse(is.na(longitude), address.lon, longitude),
         lat=ifelse(is.na(latitude),  address.lat, latitude)) %>%
  filter(!is.na(lon) & !grepl("CLOSED", ward)) -> ysa2

# Plot the map
leaflet(ysa2) %>%
  addProviderTiles("CartoDB.Positron") %>%
  addCircleMarkers(lng = ~longitude, 
                   lat = ~latitude, 
                   radius = 1.5, 
                   color = "red",
                   stroke=FALSE,
                   fillOpacity = 0.8,
                   popup = ~popup_info) %>%
addLegend("bottomright", colors= "red", labels="Community ", title="YSA     
Bounderies by Community")

I am trying to separate colors using the following code:

# color <- colorFactor(c("blue", "red", "green", "yellow", "brown", "gold", "purple"),    
domain = c("Braddock", "Shenandoah", "Langley", "DC 2nd", "Colonial 1st", 
"Colonial 2nd", "Glenn Dale"))

Essentially, I want to assign a different color to each community, for example, an attempt in the text above, but I missed something. Please share if you have any ideas.

+4
1

, , . - :

library(viridis) # My favorite palette for maps
wardpal <- colorFactor(viridis(7), ysa2$ward) # I'm assuming the variable ward contains the names of the communities.

leaflet(ysa2) %>%
    addProviderTiles("CartoDB.Positron") %>%
    addCircleMarkers(lng = ~longitude, 
                       lat = ~latitude, 
                       radius = 1.5, 
                       fillColor = ~wardpal(ward),
                       stroke=FALSE,
                       fillOpacity = 0.8,
                       popup = ~popup_info) %>%
    addLegend("bottomright", pal = wardpal, values = ~ward, labels = "Community ", title = "YSA Bounderies by Community")

. fillOpacity , (, fillOpacity = ~ percent_female). , . , "" addCirlayer.

+5

Source: https://habr.com/ru/post/1664740/


All Articles