Untitled

library(haven)
setwd("C:\\Users\\anami\\OneDrive\\Documents\\GOALS")
goals<- read_sav("C:\\Users\\anami\\OneDrive\\Documents\\GOALS\\Goals_data_1_20_25 (1).sav")
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
goalsmy <- goals %>%
  mutate(
    svy1date = as.Date(svy1date),
    month_year = format(svy1date, "%b %y"),
    month_order = as.Date(paste0(format(svy1date, "%Y-%m"), "-01"))
  )

# Count participants
goals_my <- goalsmy %>%
  dplyr::group_by(group, month_year, month_order) %>%
  dplyr::summarise(participant_count = dplyr::n(), .groups = "drop")

# Create group label
goals_my$group_label <- factor(goals_my$group,
                                    levels = 1:4,
                                    labels = c("Control", "Cash", "MM", "CashPlus"))

# Order month-year
goals_my <- goals_my %>%
  arrange(month_order)

goals_my$month_year <- factor(goals_my$month_year,
                                   levels = unique(goals_my$month_year))

# Define colors
group_colors <- c(
  "Control" = "#002F6C",
  "Cash" = "#009CA6",
  "MM" = "#F7941D",
  "CashPlus" = "#A7325F"
)

# Total per month for bar labels
bar_totals <- goals_my %>%
  dplyr::group_by(month_year) %>%
  dplyr::summarise(total = sum(participant_count), .groups = "drop") %>%
  mutate(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
training <- goals[, c("group","s1train", "s2train", "s4train")]

# Rename training columns to timepoints
training <- dplyr::rename(training,
                          Baseline = s1train,
                          `12 months` = s2train,
                          `24 months` = s4train)

# Set group names
training$group <- factor(training$group,
                         levels = 1:4,
                         labels = c("Control", "Cash", "MM", "CashPlus"))

# Pivot longer while keeping group
long_df <- training %>%
  tidyr::pivot_longer(
    cols = c("Baseline", "12 months", "24 months"),
    names_to = "Timepoint",
    values_to = "Response"
  ) %>%
  dplyr::filter(!is.na(Response)) %>%
  dplyr::mutate(
    Response = factor(ifelse(Response == 1, "Yes", "No"), levels = c("No", "Yes")),
    Timepoint = factor(Timepoint, levels = c("Baseline", "12 months", "24 months"))
  )

# Summarize counts and percent
summary_df <- long_df %>%
  dplyr::group_by(group, Timepoint, Response) %>%
  dplyr::summarise(n = dplyr::n(), .groups = "drop") %>%
  dplyr::group_by(group, Timepoint) %>%
  dplyr::mutate(percent = n / sum(n))

# 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"
  )