# library(tidyverse) Loads tidyverse packages from the tidyverse download
# read_csv uploads csv file into r script and reads through the whole file
# %>% passes the result of one function directly into the next arguement
# count() groups data by the county
# total_closure
# mutate() creates new column and updates existing ones
# case_when() evaluates conditions to assign into the region column
# %in% it will check for matching data
library(tidyverse)── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
✔ dplyr 1.2.1 ✔ readr 2.2.0
✔ forcats 1.0.1 ✔ stringr 1.6.0
✔ ggplot2 4.0.3 ✔ tibble 3.3.1
✔ lubridate 1.9.5 ✔ tidyr 1.3.2
✔ purrr 1.2.2
── 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
road_data <- read_csv("Maryland_Road_Closures_20260613.csv")Rows: 46 Columns: 9
── Column specification ────────────────────────────────────────────────────────
Delimiter: ","
chr (7): county, incident, direction, lanes, created, updated, location
dbl (2): lat, long
ℹ Use `spec()` to retrieve the full column specification for this data.
ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
county_summary <- road_data %>%
count(county, name = "total_closure") %>%
mutate(Region = case_when(
county %in% c("Baltimore", "Baltimore City", "Anne Arundel", "Howard", "Harford", "Carroll") ~ "Economic Center of Maryland",
county %in% c("Montgomery", "Frederick", "Prince George" ) ~ "Dc connectors",
county %in% c("Garret", "Allegany", "Washington") ~ "Industrial materials of Maryland",
TRUE ~ "Low state economic impact"
))
# ggplot() creates a new graph and prepares for county_summary
# aes() short for aesthetics
# x = reorder() commands what goes on the x-axis
# y = total_closure maps the length of the bar
# fill = region colors the bars depending their economic impact
ggplot(county_summary, aes(x = reorder(county, total_closure), y = total_closure, fill = Region)) +
geom_col() +
coord_flip() +
labs(
title = "Closure frequency by county",
subtitle = "Evaluating potential commercial distripution risk across Maryland economic region",
x = "County Jurisdiction",
y = "Number of Incidents)",
caption = "Source: Maryland MDOC state operations center",
fill = "Maryland Region "
) +
scale_fill_brewer(palette = "Set2" ) +
theme_minimal() # Data visualization 2
# library(tidyverse) Loads tidyverse packages from the tidyverse download
# ggplot() creates and prepares a new graph
# geom_bar() tells R on how to draw and apply data for visual purposes
# labs() means labels this adds title, subtilte,
# x = "incident Category' this tells r that x will be named "Incidents category"
# y = "NUmber of Occurrencies" this tells r that y will be named "Number of occurencies
# "caption =" this cites data source
# scale_fill_brewer controls the color pallette when using categorical variables
# theme_minimal() changes the overall background design of plot
library(tidyverse)
ggplot(road_data, aes(x = incident, fill = incident)) +
geom_bar() +
labs(
title = "Types of Road Closure Incidents",
subtitle = "Comparing planned construction frequencies against emergency traffic blockades",
x = "Incident Category",
y = "Number of Occurrences",
caption = "Source: Maryland MDOC state operations center"
) +
scale_fill_brewer(palette = "Pastel1") +
theme_minimal()Warning in RColorBrewer::brewer.pal(n, pal): n too large, allowed maximum for palette Pastel1 is 9
Returning the palette you asked for with that many colors