# Original R analysis from The handgrip demonstration

#| echo: false
#| message: false
#| warning: false
if (!require("pacman")) install.packages("pacman")

# 2. Use p_load instead of library
pacman::p_load(
  ggplot2,
  dplyr,
  googlesheets4,
  tidyverse,
  janitor,
  gt
)




gs4_deauth()

grip_data <- read_sheet("https://docs.google.com/spreadsheets/d/1pbNyGKE1txSLDp-fXFI557XrsjzUH99OjTkGxdAZHO4")


grip_data_clean <- grip_data %>%
 janitor::clean_names() %>%
  rename(
    sex = gender
  )

grip_plot <- ggplot(grip_data_clean, aes(x = age_point_for_plotting, y = nhanes_mean_kg, color = sex, fill = sex)) +
  
  # 1. Add the shaded ribbon for Standard Deviation (Mean +/- 1 SD)
  geom_ribbon(aes(ymin = nhanes_mean_kg - nhanes_mean_sd, 
                  ymax = nhanes_mean_kg + nhanes_mean_sd), 
              alpha = 0.2,      # Make it transparent
              color = NA) +     # Remove the ribbon border line

  # 2. Add the line for the Mean
  geom_line(linewidth = 1) +
  
  # 3. Add points to show exactly where the age point (averaged when needed) data measurements are
  geom_point(size = 2) +

  # 4. Labels and Formatting
  labs(title = "Mean Grip Strength by Sex and Age",
       subtitle = "Shaded area represents +/- 1 Standard Deviation",
       x = "Age (Years)",
       y = "Mean Grip Strength (kg)",
       caption = paste0("Source: NHANES | Plot by Mara Alexeev | ", Sys.Date())) +
  
  theme_minimal() +
  scale_color_manual(values = c("Female" = "#E74C3C", "Male" = "#2980B9"),
                     breaks = c("Male", "Female"),
                     name = "Sex") +
   scale_fill_manual(
    values = c("Female" = "#E74C3C", "Male" = "#2980B9"),
    breaks = c("Male", "Female"),
    name = "Sex") +
  expand_limits(y = 0) 

# Prepare the data
publication_table <- grip_data_clean %>%
  # 1. Sort by the numeric age to ensure 6 comes before 10
  arrange(age_point_for_plotting)%>%
  
  # 2. Create a combined "Mean (SD)" column
  mutate(
    # sprintf ensures consistent decimal places (e.g., "10.0" instead of "10")
    Stats_Label = sprintf("%.1f (%.1f)", nhanes_mean_kg, nhanes_mean_sd)
  ) %>%
  
  # 3. Select only the columns we need for the final output
  select(age_label, sex, Stats_Label) %>%
  
  # 4. Pivot to Wide Format (Males and Females side-by-side)
  pivot_wider(
    names_from = sex,
    values_from = Stats_Label
  )

# Render the formatted table
final_table <- publication_table %>%
  gt() %>%
  
  # Add Main Titles
  tab_header(
    title = md("**Grip Strength Values**"),
    subtitle = "Mean (Standard Deviation) in kg by Age and Sex"
  ) %>%
  
  # Add a "Spanner" to group Male/Female under one header
  tab_spanner(
    label = "Sex",
    columns = c("Male", "Female") # Adjust if your columns are ordered differently
  ) %>%
  
  # Rename the Age column nicely
  cols_label(
    age_label = "Age Group (Years)"
  ) %>%
  
  # Add Source Note at the bottom
  tab_source_note(
    source_note = "Data Source: NHANES"
  ) %>%
  
  # Visual Styling (Optional but recommended for papers)
  opt_row_striping() %>%  # Alternating row colors
  tab_options(
    heading.align = "left",
    table.border.top.style = "solid",
    table.border.top.width = px(3)
  )




grip_plot



final_table
