From c48c0001d903ec534a9e1ea5113ed0b2f09fc9e4 Mon Sep 17 00:00:00 2001 From: dorisyan1122 Date: Fri, 15 Dec 2023 16:10:24 -0500 Subject: [PATCH] changed some comments --- map.qmd | 193 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 193 insertions(+) create mode 100644 map.qmd diff --git a/map.qmd b/map.qmd new file mode 100644 index 0000000..a942d45 --- /dev/null +++ b/map.qmd @@ -0,0 +1,193 @@ +--- +title: "Final Project" +author: "Ziwen Lu, Xuyan Xiu, Doris Yan" +format: html +editor: visual +execute: + warning: false +embed-resources: true +--- + +```{r load libraries, echo=FALSE} +library(haven) +library(here) +library(tidyverse) +library(icpsrdata) +library(plotly) +dir.create("data") +``` + +# Reproducible data retrieval + +```{r} +options("icpsr_email" = "dy212@georgetown.edu", "icpsr_password" = "Fbp9nbmKreLsubd8nL5H") +``` + +## crime + +```{r} +# takes 1-2 mins +icpsr_download( + file_id = 38649, + download_dir = here("data") +) + +crime<-read_dta(here("data","ICPSR_38649","DS0001","38649-0001-Data.dta")) +``` + +# Clean data + +## crime + +```{r} +# crime data +crime_data<-crime|> + rename_all(tolower)|> + rename(county=stcofips)|> + mutate(viol = murder+rape+robbery+agasslt)|> + mutate(property = burglry+larceny+mvtheft)|> + select(county,year,viol,property) + +attr(crime_data$viol, "label")<-"Total violent crimes reported (MURDER + RAPE + ROBBERY + AGASSLT)" + +attr(crime_data$property, "label")<-"Total property crimes reported (BURGLRY + LARCENY + MVTHEFT)" + +``` + +```{r} + +# extract state-level FIPS codes (first two digits of county FIPS codes) + +crime_data$state_fips <- + substr(crime_data$county, + 1, 2) + +state_fips_to_abb <- + tibble( + state_fips = c("01", "02", "04", "05", "06", + "08", "09", "10", "11", "12", + "13", "15", "16", "17", "18", + "19", "20", "21", "22", "23", + "24", "25", "26", "27", "28", + "29", "30", "31", "32", "33", + "34", "35", "36", "37", "38", + "39", "40", "41", "42", "44", + "45", "46", "47", "48", "49", + "50", "51", "53", "54", "55", "56"), + state_abb = c("AL", "AK", "AZ", "AR", "CA", "CO", + "CT", "DE", "DC", "FL", "GA", "HI", + "ID", "IL", "IN", "IA", "KS", "KY", + "LA", "ME", "MD", "MA", "MI", "MN", + "MS", "MO", "MT", "NE", "NV", "NH", + "NJ", "NM", "NY", "NC", "ND", "OH", + "OK", "OR", "PA", "RI", "SC", "SD", + "TN", "TX", "UT", "VT", "VA", "WA", + "WV", "WI", "WY") +) + +``` + +## Map + +```{r} + +# Convert county-level data to state-level data, aggregate violent crime data + +crime_data_map <- crime_data |> + group_by(state_fips) |> + summarise( + total_violence = sum(viol, + na.rm = TRUE), + total_property = sum(property, + na.rm = TRUE) + ) |> + + left_join(state_fips_to_abb, + by = 'state_fips') |> + + mutate( + hover_violence = paste(state_abb, + '
', + "Total Violent Crimes: ", + total_violence), + hover_property = paste(state_abb, + '
', + "Total Property Crimes: ", + total_property) + ) |> + + pivot_longer( + cols = c(total_violence, + total_property), + names_to = "crime_type", + values_to = "crime_count" + ) + +``` + +```{r} + +# create the map for total violence + +violence_map <- + plot_geo(crime_data_map |> + filter(crime_type == "total_violence"), + locationmode = 'USA-states') |> + + add_trace( + z = ~crime_count, + text = ~hover_violence, + hoverinfo = 'text', + locations = ~state_abb, + color = ~crime_count, + colors = c("#1a9641", "#ffffbf", "#fdae61", "#d7191c"), + colorbar = list(title = "Total Violent Crimes (million)") + ) |> + + layout( + title = 'US Crimes by State, 2009 - 2014', + geo = list( + scope = 'usa', + projection = list(type = 'albers usa'), + showlakes = TRUE, + lakecolor = toRGB('white') + ) + ) + +# create the map for total property + +property_map <- + plot_geo(crime_data_map |> + filter(crime_type == "total_property"), + locationmode = 'USA-states') |> + + add_trace( + z = ~crime_count, + text = ~hover_property, + hoverinfo = 'text', + locations = ~state_abb, + color = ~crime_count, + colors = c("#2b83ba", "#ffffbf", "#fdae61", "#d7191c"), + colorbar = list(title = "Total Property Crimes (million)") + ) |> + + layout( + title = 'US Crimes by State, 2009 - 2014', + geo = list( + scope = 'usa', + projection = list(type = 'albers usa'), + showlakes = TRUE, + lakecolor = toRGB('white') + ) + ) + +# place the two maps side by side in a stacked layout +fig <- subplot(violence_map, + property_map, + nrows = 2, + margin = 0.05) + +# print the figure +fig + +```