For this lab, I once again used data from the Chicago Open Data Portal. This time, I examined landmarks throughout the city. With this data, I was able to create a map depicting locations of landmarks throughout the city color coded by the decade in which they were officially desginated as a landmark. Additional info for each can be found by clicking on the markers.
The first step was to load packages. The packages were the same as have been used previously, to include ‘leaflet’ and ‘sf’.
library(leaflet)
library(sf)
library(tidyverse)
library(here)
library(leafpop)
library(janitor)
Next, I read in the data needed for this project. This included the point data for landmarks as well as a GeoJSON for the neighborhoods around Chicagoland.
chi_landmarks <- read_csv(here("data", "Individual_Landmarks.csv")) # Landmark Point Data
chi_comms <- readLines(here("data", "comm_areas.geojson")) %>% # Neighborhoods in GeoJSON
paste(collapse = "\n")
For the landmark data, I separated out the landmark designation date and then assigned the year to a decade, and associated color, using ‘case_when.’ I had hoped to use the year built, but the format of the year data was an absolute mess with no consistency. The designation date provided consistency for ease of data prepping.
The determination to add a color variable was made after multiple attempts to color markers in a different manner, first by creating a function and then by using “colorFactor,” which I will cover at the end of this report.
# Ranges of years
seventies = c(1970:1979)
eighties = c(1980:1989)
nineties = c(1990:1999)
aughts = c(2000:2009)
tens = c(2010:2019)
other = 0
# Cleans data and adds columns for decade and associated color
chi_landmarks <- chi_landmarks %>%
clean_names() %>% # Tidying data
separate(landmark_designation_date, c("month", "day", "year"), remove = FALSE) %>%
mutate(year = ifelse(is.na(year), 0, year)) %>% # Setting NA values to 0
mutate(year_range = case_when(year %in% seventies ~ "1970s", # Value for various year ranges
year %in% eighties ~ "1980s",
year %in% nineties ~ "1990s",
year %in% aughts ~ "2000s",
year %in% tens ~ "2010s",
year %in% other ~ "No Date")) %>%
mutate(color = case_when(year %in% seventies ~ "green", # Associated color for years
year %in% eighties ~ "purple",
year %in% nineties ~ "red",
year %in% aughts ~ "blue",
year %in% tens ~ "orange",
year %in% other ~ "white")) %>%
st_as_sf(coords = c("longitude", "latitude"), crs = 4326) # Assigning coords
Next, to prepare for plotting data, I created two variables - one for legend color and the other for the icons I would use. For the icons, I used a “bank” symbol from the Font Awesome (or ‘fa’) library. The color for these is based on the decade, which was a variable created earlier.
# Function for color palette. Used for legend but does not work for marker color
pal <- colorFactor(c("green", "purple", "red", "blue", "orange", "white"),
domain = c("1970s", "1980s", "1990s", "2000s", "2010s", "No Date"))
# Creating markers, using color variable in dataframe
pointColor <- awesomeIcons(icon = 'bank', iconColor = "black",
markerColor = chi_landmarks$color,
library = 'fa', spin = FALSE)
With the markers and colors set, I next plotted the data using “leaflet.” To make the landmarks standout, I used a basemap from Carto. For my markers, the label is the landmark name and clicking on the marker provides details about each landmark. Because there are so many in a limited space, I elected to use clustering to decrease the clutter. Additionally, I added the neighborhood boundaries from the GeoJSON file. I was unable to figure out how to add a label for the GeoJSON. Finally, the legend added allows the viewer to quickly ascertain the decade each landmark was designated.
# Plotting Chicago Landmarks
leaflet(data = chi_landmarks) %>%
addProviderTiles(providers$CartoDB.DarkMatter) %>%
addAwesomeMarkers(popup = popupTable(st_drop_geometry(chi_landmarks[,1:6]),
feature.id = FALSE, row.numbers = FALSE),
label = ~as.character(landmark_name),
icon = pointColor,
clusterOptions = markerClusterOptions()) %>%
addGeoJSON(chi_comms, weight = 3, color = "white", fill = FALSE,) %>%
addLegend(title = "Year Range", pal = pal, values = ~year_range)
As mentioned earlier, I attempted two different methods of assigning color to the markers before defaulting to adding a color column in my dataframe. The first was was to create a function which assigned color based on year each landmark was inducted. I confirmed the function worked but it would not add color to the markers using the code immediately following, which is the same as what is used in my code with the exception of calling a function instead of a column in the dataframe.
# Function to designate colors based on Year designated landmark
# works but cannot use for marker color
getcolor <- function(chi_landmarks) {
sapply(chi_landmarks$year, function(year) {
if(year >= 1970 & year <= 1979){"green"}
else if(year >= 1980 & year <= 1989) {"purple"}
else if(year >= 1990 & year >= 1999) {"red"}
else if(year >= 2000 & year >= 2009) {"blue"}
else if(year >= 2010) {"orange"}
else {"white"}
})
}
pointColor <- awesomeIcons(icon = 'bank', iconColor = "black",
markerColor = getcolor(chi_landmarks),
library = 'fa', spin = FALSE)
I also attempted to use ‘colorFactor’ to assign marker color. I again verified it was assigning color but could not get the markers to plot.