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:
library(dplyr)
library(ggmap)
library(leaflet)
ysa <- Community.Map
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
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.