Skip to content

Commit

Permalink
Merge branch 'main' into doris
Browse files Browse the repository at this point in the history
  • Loading branch information
dorisyan1122 authored Dec 15, 2023
2 parents c48c000 + a055fa4 commit fe159be
Show file tree
Hide file tree
Showing 5 changed files with 1,099 additions and 5,647 deletions.
1 change: 1 addition & 0 deletions map.html
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@

195 changes: 195 additions & 0 deletions map.qmd
Original file line number Diff line number Diff line change
Expand Up @@ -191,3 +191,198 @@ fig <- subplot(violence_map,
fig
```
=======
---
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,
'<br>',
"Total Violent Crimes: ",
total_violence),
hover_property = paste(state_abb,
'<br>',
"Total Property Crimes: ",
total_property)
) |>
pivot_longer(
cols = c(total_violence,
total_property),
names_to = "crime_type",
values_to = "crime_count"
)
```

```{r}
# Create and output 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 and output 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
```
Loading

0 comments on commit fe159be

Please sign in to comment.