library(tidyverse)
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ dplyr 1.1.4 ✔ readr 2.1.6
## ✔ forcats 1.0.1 ✔ stringr 1.6.0
## ✔ ggplot2 4.0.1 ✔ tibble 3.3.0
## ✔ lubridate 1.9.4 ✔ tidyr 1.3.2
## ✔ purrr 1.2.0
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag() masks stats::lag()
## ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
library(dplyr)
library(crimedata)
boston_crime <- get_crime_data(year = 2016, cities = "Boston", type = "core")
ggplot(boston_crime, aes(x = offense_type)) +
geom_bar(fill = "blue") +
labs(title = "Crimes in Boston (2016)", x = "Offense Type", y = "Count") +
theme_minimal() +
theme(axis.text.x = element_text(angle = 45, vjust = 1, hjust = 1))
This visualization shows a count of all of the crimes in Boston in 2016
by offense type. This can shape the next steps by seeing the most common
offenses to narrow where to focus the study.
boston_monthly <- boston_crime %>%
mutate(month = floor_date(date_single, "month")) %>%
group_by(month, offense_group) %>%
summarise(total = n())
## `summarise()` has grouped output by 'month'. You can override using the
## `.groups` argument.
ggplot(boston_monthly, aes(x = month, y = total, color = offense_group)) +
geom_line() +
labs(title = "Boston Crime Trends by Category (2016)") +
theme_minimal()
This visualization shows the most common offense types during each month
of the year in 2016 in Boston. This might help with a later analysis by
seeing if crime patterns change during the time of the year or if it is
consistent throughout the year.
#Visualization 3
boston_days <- boston_crime %>%
mutate(date_single = as.Date(date_single)) %>%
mutate(day_name = wday(date_single, label = TRUE, abbr = FALSE)) %>%
group_by(day_name, offense_type) %>%
summarise(count = n(), .groups = "drop")
ggplot(boston_days, aes(x = offense_type, y = count, fill = offense_type)) +
geom_col() +
facet_wrap(~day_name) +
theme_minimal() +
theme(legend.position = "none", panel.spacing = unit(2, "lines"), panel.border = element_rect(color = "grey", fill = NA, size = 0.5), axis.text.x = element_text(angle = 45, hjust = 1)) +
labs( title = "Boston Crime Profiles by Day of the Week (2016)", x = "Offense Type", y = "Frequency")
## Warning: The `size` argument of `element_rect()` is deprecated as of ggplot2 3.4.0.
## ℹ Please use the `linewidth` argument instead.
## This warning is displayed once every 8 hours.
## Call `lifecycle::last_lifecycle_warnings()` to see where this warning was
## generated.
These graphs show offense types by days of the week in Boston in 2016.
This might help to show if certain types of crimes occur more on certain
days of the week or if it is consistent across days.