library(haven)
setwd("C:\\Users\\anami\\OneDrive\\Documents\\GOALS")
<- read_sav("C:\\Users\\anami\\OneDrive\\Documents\\GOALS\\Goals_data_1_20_25 (1).sav") goals
Untitled
library(ggplot2)
library(dplyr)
Attaching package: 'dplyr'
The following objects are masked from 'package:stats':
filter, lag
The following objects are masked from 'package:base':
intersect, setdiff, setequal, union
# Create month-year fields
<- goals %>%
goalsmy mutate(
svy1date = as.Date(svy1date),
month_year = format(svy1date, "%b %y"),
month_order = as.Date(paste0(format(svy1date, "%Y-%m"), "-01"))
)
# Count participants
<- goalsmy %>%
goals_my ::group_by(group, month_year, month_order) %>%
dplyr::summarise(participant_count = dplyr::n(), .groups = "drop")
dplyr
# Create group label
$group_label <- factor(goals_my$group,
goals_mylevels = 1:4,
labels = c("Control", "Cash", "MM", "CashPlus"))
# Order month-year
<- goals_my %>%
goals_my arrange(month_order)
$month_year <- factor(goals_my$month_year,
goals_mylevels = unique(goals_my$month_year))
# Define colors
<- c(
group_colors "Control" = "#002F6C",
"Cash" = "#009CA6",
"MM" = "#F7941D",
"CashPlus" = "#A7325F"
)
# Total per month for bar labels
<- goals_my %>%
bar_totals ::group_by(month_year) %>%
dplyr::summarise(total = sum(participant_count), .groups = "drop") %>%
dplyrmutate(month_year = factor(month_year, levels = levels(goals_my$month_year)))
# Final stacked bar chart
ggplot(goals_my, aes(x = month_year, y = participant_count, fill = group_label)) +
geom_col(width = 0.6) +
geom_text(data = bar_totals,
aes(x = month_year, y = total + 5, label = total),
inherit.aes = FALSE,
size = 3.5) +
scale_fill_manual(values = group_colors, name = "Group") +
scale_y_continuous(breaks = seq(0, 250, 25)) +
labs(
x = NULL,
y = "Number of GOALS Participants",
title = "Month-Year of GOALS Participants Baseline Interview"
+
) theme_minimal(base_size = 13) +
theme(
plot.title = element_text(size = 13),
axis.title.y = element_text(size = 11),
axis.text.x = element_text(angle = 0, hjust = 0.5),
legend.position = "right"
)
# Load libraries
library(dplyr)
library(tidyr)
library(ggplot2)
#make subset
<- goals[, c("group","s1train", "s2train", "s4train")]
training
# Rename training columns to timepoints
<- dplyr::rename(training,
training Baseline = s1train,
`12 months` = s2train,
`24 months` = s4train)
# Set group names
$group <- factor(training$group,
traininglevels = 1:4,
labels = c("Control", "Cash", "MM", "CashPlus"))
# Pivot longer while keeping group
<- training %>%
long_df ::pivot_longer(
tidyrcols = c("Baseline", "12 months", "24 months"),
names_to = "Timepoint",
values_to = "Response"
%>%
) ::filter(!is.na(Response)) %>%
dplyr::mutate(
dplyrResponse = factor(ifelse(Response == 1, "Yes", "No"), levels = c("No", "Yes")),
Timepoint = factor(Timepoint, levels = c("Baseline", "12 months", "24 months"))
)
# Summarize counts and percent
<- long_df %>%
summary_df ::group_by(group, Timepoint, Response) %>%
dplyr::summarise(n = dplyr::n(), .groups = "drop") %>%
dplyr::group_by(group, Timepoint) %>%
dplyr::mutate(percent = n / sum(n))
dplyr
# Plot the chart
ggplot(summary_df, aes(x = group, y = percent, fill = Response)) +
geom_col(position = "stack", width = 0.5) +
facet_wrap(~Timepoint, nrow = 1) +
scale_y_continuous(
breaks = seq(0, 1, by = 0.2),
labels = scales::percent_format(accuracy = 1),
expand = c(0, 0)
+
) scale_fill_manual(values = c("gray70", "#002467")) +
labs(
title = "Participants Currently Taking Training Courses or Education Classes",
x = NULL,
y = "Percent of Participants",
fill = "Response"
+
) theme_minimal(base_size = 13) +
theme(
plot.title = element_text(size = 13, face = "bold", hjust = 0.5),
axis.text.x = element_text(size = 8),
axis.title.x = element_text(size = 11),
axis.title.y = element_text(size = 11),
strip.text = element_text(size = 12),
panel.spacing.x = unit(1.5, "lines"),
legend.position = "right"
)