library(tidyverse)
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ dplyr 1.1.4 ✔ readr 2.1.4
## ✔ forcats 1.0.0 ✔ stringr 1.5.1
## ✔ ggplot2 3.4.4 ✔ tibble 3.2.1
## ✔ lubridate 1.9.3 ✔ tidyr 1.3.0
## ✔ purrr 1.0.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
library(writexl)
closureData <- read_csv("ClosureCases_clean.csv")
## Rows: 34 Columns: 25
## ── Column specification ────────────────────────────────────────────────────────
## Delimiter: ","
## chr (20): caseNbr, lastName, firstName, Cty, DOB, Gender, Race, Program, dat...
## dbl (3): Age, lengthRefClsd, Payment
## lgl (2): restitutionPaid, Notes
##
## ℹ 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.
intakeData <- read_csv("IntakeCases_clean.csv")
## Rows: 44 Columns: 25
## ── Column specification ────────────────────────────────────────────────────────
## Delimiter: ","
## chr (23): caseNbr, lastName, firstName, Program, DOB, Gender, dateIncident, ...
## dbl (2): Age, lengthRef_Intake
##
## ℹ 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.
refData <- read_csv("ReferralsCases_clean.csv")
## Rows: 58 Columns: 18
## ── Column specification ────────────────────────────────────────────────────────
## Delimiter: ","
## chr (17): caseNbr, lastName, firstName, Cty, Program, DOB, Gender, dateIncid...
## dbl (1): lengthIncidentRef
##
## ℹ 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.
ACRJ <- read_csv("ACRJ_clean.csv")
## Rows: 68 Columns: 15
## ── Column specification ────────────────────────────────────────────────────────
## Delimiter: ","
## chr (15): caseNbr, Location, caseCategory, caseType, Style, lastName, caseSt...
##
## ℹ 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.
ACRJ <- ACRJ |>
rename(Status = 7, offenseType = 9, docketNbr = 1, dateIncident = 10)
#where dateIncident = date of first offense (if multiple)
closureData <- closureData |>
subset(select = -caseType)
refData <- refData |>
mutate(Age = as.numeric(Age)) |>
mutate(lengthIncidentRef = as.numeric(Age))
## Warning: There was 1 warning in `mutate()`.
## ℹ In argument: `Age = as.numeric(Age)`.
## Caused by warning:
## ! NAs introduced by coercion
intakeData <- intakeData |>
mutate(lengthIncidentRef = as.numeric(lengthIncidentRef))
## Warning: There was 1 warning in `mutate()`.
## ℹ In argument: `lengthIncidentRef = as.numeric(lengthIncidentRef)`.
## Caused by warning:
## ! NAs introduced by coercion
mergedData <- intakeData |>
full_join(closureData) |>
full_join(refData) |>
full_join(ACRJ)|>
distinct(docketNbr, Program, lastName, .keep_all = TRUE)
## Joining with `by = join_by(caseNbr, lastName, firstName, Program, DOB, Gender,
## dateIncident, Age, dateRef, dateReRef, Race, docketNbr, Court, Notes)`
## Joining with `by = join_by(caseNbr, lastName, firstName, Program, DOB, Gender,
## dateIncident, Age, dateCharged, dateRef, dateReRef, lengthIncidentRef, Race,
## docketNbr, Court, Status, Notes, Cty)`
## Joining with `by = join_by(lastName, Program, dateIncident, docketNbr, Status,
## Outcome)`
write_xlsx(mergedData, "mergedData.xlsx")
DSADFinal_Data <- read_csv("DSADFinal_Data.csv")
## Rows: 144 Columns: 19
## ── Column specification ────────────────────────────────────────────────────────
## Delimiter: ","
## chr (13): caseNbr, lastName, firstName, Program, DOB, Gender, Race, docketN...
## dbl (2): Age, lengthIncidentRef
## date (4): dateIncident, dateRef, dateIntake, dateClsd
##
## ℹ 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.
# Realized that two vars were actually the same... merged these and fixed the format
DSADFinal_Data <- DSADFinal_Data |>
mutate(dateClsd = coalesce(clsdDate, dateDivClsd)) |>
filter(!(is.na(dateIncident) & is.na(dateRef) & is.na(dateClsd) & is.na(dateIntake)))|>
mutate(dateIncident = as.Date(dateIncident, format = "%m/%d/%y")) |>
mutate(dateRef = as.Date(dateRef, format = "%m/%d/%y")) |>
mutate(dateIntake = as.Date(dateIntake, format = "%m/%d/%y")) |>
mutate(dateClsd = as.Date(dateClsd, format = "%m/%d/%y"))
# save the data set back to directory
write_csv(DSADFinal_Data, "DSADFinal_Data.csv")
# First draft graph before addressing similar groups
ggplot(DSADFinal_Data, aes(x = "", fill = factor(Outcome))) +
geom_bar(width = 1) +
coord_polar(theta = "y") +
theme_minimal() +
labs(fill = "Program Status", title = "
Status of ACRJ Programs")
pieData <- DSADFinal_Data |>
filter(!is.na(Outcome)) |>
mutate(outcomeCats = case_when(
Outcome %in% c("Completed Successfully", "Successful", "Followed Court Order") ~ "Successful",
Outcome %in% c("Unsuccessful", "Did Not Follow Court Order") ~ "Unsuccessful",
# Add other conditions here if necessary
TRUE ~ Outcome # Keeps all other outcomes as they are
))
# Calculating the counts and percentages for each category
outcome_data <- pieData %>%
count(outcomeCats) %>%
mutate(percentage = n / sum(n) * 100)
# Creating the pie chart
pieChart <-
ggplot(outcome_data, aes(x = "", y = percentage, fill = outcomeCats)) +
geom_bar(stat = "identity", width = 1) +
coord_polar("y", start = 0) +
geom_text(aes(label = paste0(round(percentage, 1), "%")),
position = position_stack(vjust = 0.5),
size = 2, # Adjust this value for text size
color = "white") +
theme_void() +
theme(plot.background = element_rect(fill = "white", color = NA), # Set background color to white
panel.background = element_rect(fill = "white", color = NA), # Ensure panel background is also white
plot.title = element_text(size = 20, hjust = 0.5, vjust = 0.5), # Increase title size and center it
plot.margin = margin(1, 1, 1, 1, "cm")
) +
labs(fill = "Outcome", title = "Distribution of Outcomes")
# saving the plot at a larger width to prevent overlap (still appears overlapping in R, but not after saving)
pieChart
ggsave("pie_chart.png", plot = pieChart, width = 10, height = 8, dpi = 300)
barData <- DSADFinal_Data |>
filter(!Age > 90) #Removes the supposed 123 y/o
ggplot(barData, aes(x = Age)) +
geom_histogram(bins = 30, fill = "blue", color = "black") +
theme_minimal() +
labs(title = "Age Distribution of Participants", x = "Age", y = "Count")
histoData <- DSADFinal_Data|>
#Did not have any data on Age
filter(!Program =="Facility Assessment/Screening") |>
filter(Age < 100) #remove the 123 y/o
ggplot(histoData, aes(x = factor(Program), y = Age)) +
geom_boxplot() +
theme_minimal() +
labs(title = "Age Distribution by Program", x = "Program", y = "Age") +
theme(axis.text.x = element_text(size = 8, angle = 45, hjust = 1))
# functions like a smooth version of a histogram, peak shows the length of time that most cases take
ggplot(DSADFinal_Data, aes(x = lengthIncidentRef)) +
geom_density(fill = "blue", alpha = 0.5) +
labs(title = "Density of Time Between Incident and Referral", x = "Time Duration", y = "Density") +
theme_minimal()
## Warning: Removed 61 rows containing non-finite values (`stat_density()`).
# filter by gender to look at the length of time to referral
violinData <-DSADFinal_Data|>
filter(Gender == "Man" | Gender == "Woman")
ggplot(violinData, aes(x = Gender, y = lengthIncidentRef)) +
geom_violin(trim = FALSE) +
labs(title = "Distribution of Time Between Incident and Referral by Gender", x = "Gender", y = "Time Duration") +
theme_minimal()
## Warning: Removed 2 rows containing non-finite values (`stat_ydensity()`).
### Formatting the dates
# not necessarily a practical graph...
x_limits <- as.Date(c("2023-05-01", "2023-09-28"))
ggplot(DSADFinal_Data, aes(x = dateRef, group = Program, color = Program)) +
geom_line(stat = "count") +
labs(title = "Trends in Program Types 2023", x = "Date of Referral", y = "Count of Offenses") +
xlim(x_limits) +
theme_minimal()
## Warning: Removed 75 rows containing non-finite values (`stat_count()`).
# overlap of date incident and referral
x_limitsLong <- as.Date(c("2020-05-20", "2023-06-22"))
# Reshape the data
long_df <- DSADFinal_Data |>
gather(key = "Type", value = "Date", dateIncident, dateRef)
ggplot(long_df, aes(x = Date, color = Type)) +
geom_density() +
labs(title = "Overlap of Date of Incident and Date of Referral", x = "Date", y = "Density") +
xlim(x_limitsLong)+
theme_minimal()
## Warning: Removed 145 rows containing non-finite values (`stat_density()`).