## ----include = FALSE----------------------------------------------------------
knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>"
)

## ----setup--------------------------------------------------------------------
knitr::opts_chunk$set(warning = FALSE, message = FALSE)
library(chiOpenData)
library(ggplot2)
library(dplyr)

## ----chi-list-datasets--------------------------------------------------------
chi_list_datasets() |> head()

## ----chi-311-pull-------------------------------------------------------------
chi_motor_vehicle_collisions_data <- chi_pull_dataset(
  dataset = "ijzp-q8t2", limit = 2, timeout_sec = 90)

chi_motor_vehicle_collisions_data <- chi_pull_dataset(
  dataset = "crimes_2001_to_present", limit = 2, timeout_sec = 90)

## ----filter-location----------------------------------------------------------

chicago_crimes_street <- chi_pull_dataset(dataset = "ijzp-q8t2",limit = 3, timeout_sec = 90, filters = list(location_description = "STREET"))
chicago_crimes_street

# Checking to see the filtering worked
chicago_crimes_street |>
  distinct(location_description)

## ----filter-chi-crimes--------------------------------------------------------
# Creating the dataset
chicago_crimes <- chi_pull_dataset(dataset = "ijzp-q8t2", limit = 50, timeout_sec = 90, filters = list(location_description = "STREET", domestic = FALSE))

# Calling head of our new dataset
chicago_crimes |>
  slice_head(n = 6)

# Quick check to make sure our filtering worked
chicago_crimes |>
  summarize(rows = n())

chicago_crimes |>
  distinct(location_description)

chicago_crimes |>
  distinct(domestic)

## ----compaint-type-graph, fig.alt="Bar chart showing the frequency of crime types happening on the street that are not domestic.", fig.cap="Bar chart showing the frequency of crime types happening on the street that are not domestic.", fig.height=5, fig.width=7----
# Visualizing the distribution, ordered by frequency
chicago_crimes |>
  count(primary_type) |>
  ggplot(aes(
    x = n,
    y = reorder(primary_type, n)
  )) +
  geom_col(fill = "steelblue") +
  theme_minimal() +
  labs(
    title = "Top 50 Crime Types on the Street That Are Not Domestic",
    x = "Number of Crimes",
    y = "Primary Crime Type"
  )

