Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Distinguish Categorical Variables by Color using leaflet in R

Tags:

r

leaflet

I'm making an interactive map of a population in the D.C. area using "leaflet" in R, and I want to use different colors to distinguish the 7 different communities. My variables are: address, community, gender, and name(for the pop-ups). I used some of the code from TrendCt and Ripples.

How would I tweak my code to show the different colors for different communities, and maybe even how could I have 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 have been attempting to divide by 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 per community, like attempted in the above text, but I'm missing something. Please share if you have ideas.

like image 864
Bradley Thomas Anderson Avatar asked Dec 24 '16 04:12

Bradley Thomas Anderson


People also ask

How do you visualize 2 categorical variables in R?

The categorical variables can be easily visualized with the help of mosaic plot. In a mosaic plot, we can have one or more categorical variables and the plot is created based on the frequency of each category in the variables. To create a mosaic plot in base R, we can use mosaicplot function.

How do you identify categorical variables?

Step 1: Read the problem and identify the variables described. Note key properties of the variables, such as what types of values the variables can take. Step 2: Identify any variables from step 1 that take on values from a limited number of possible values with no particular ordering. These variables are categorical.

How do you know if a variable is categorical in R?

try: 1) str(mydata) to see the actual data types of your data. 2) table(mydata$variable) to see the frecuency of each variable. 3) another option is to use: summary(mydata) to get a general idea of your data.

How do you visualize two categorical variables?

Data concerning two categorical (i.e., nominal- or ordinal-level) variables can be displayed in a two-way contingency table, clustered bar chart, or stacked bar chart.


1 Answers

It looks like you haven't connected the colour palette you created to the data and the legend. Try something along the lines of:

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

You also want to encode gender in the colour shading. Have you tried mapping fillOpacity to a variable describing gender distribution (e.g., fillOpacity = ~ percent_female). Alternatively, you could add a separate layer for each community, setting the palette for each layer based on gender prevalence. You can tie all the layers together using the 'group' parameter within each addCirlayer call.

like image 92
Peter K Avatar answered Nov 14 '22 23:11

Peter K