Daily steps trends in biomarkers cohort
######## steps
# average steps by age and sex (14yo_females_steps etc)
dat_steps_split_sex <- dat %>%
group_by(age_years, sex) %>%
summarize(steps = mean(ad_step_count_sum_0_24hr, na.rm = TRUE), .groups = "drop")
# no smoothening
dat_steps_split_sex_unsmooth <- dat_steps_split_sex %>%
group_by(sex)
# line with facets by sex -- smoothed line with real data points
ggplot(dat_steps_split_sex_unsmooth, aes(x = age_years, y = steps)) +
geom_point(data = dat, aes(x = age_years, y = ad_step_count_sum_0_24hr), color = "#7859645D", size = 1) +
geom_smooth(method = "loess", span = 0.99, color = "#785964", fill = "#78596433", linewidth = 1.5) +
labs(y = "Average Daily Steps", x = "Age") +
facet_wrap(~ sex, labeller = as_labeller(c("male" = "Men", "female" = "Women"))) +
scale_y_continuous(labels = function(x) format_thousands(x))
## `geom_smooth()` using formula = 'y ~ x'

Activity trends in biomarkers cohort
Smoothed
custom_colors <- c("dur_day_total_vig_min_pla" = "#C5A359", "dur_day_total_mod_min_pla" = "#BCCEA1", "dur_day_total_lig_min_pla" = "#CC3D12")
# stacked area plot with facets by sex -- most smoothed
ggplot(dat_long_minutes_split_sex_99smooth, aes(x = age_years, y = minutes_smoothed, fill = activity)) +
#geom_point(data = dat, aes(x = age_years, y = minutes), color = "#7859645D", size = 1) +
geom_area(alpha=0.9) +
labs(y = "Average Daily Minutes", x = "Age") +
facet_wrap(~ sex, labeller = as_labeller(c("male" = "Men", "female" = "Women"))) +
scale_fill_manual(name = "Activity Intensity", values = custom_colors,
labels = c("Vigorous", "Moderate", "Light"))

Unsmoothed
ggplot(dat_long_minutes_split_sex_unsmooth, aes(x = age_years, y = minutes, fill = activity)) +
geom_area(alpha=0.9) +
labs(y = "Average Daily Minutes", x = "Age") +
facet_wrap(~ sex, labeller = as_labeller(c("male" = "Men", "female" = "Women"))) +
scale_fill_manual(name = "Activity Intensity", values = custom_colors,
labels = c("Vigorous", "Moderate", "Light"))

# Reshape data to long format -- each individual has 3 rows, one with light, one with mod, one with vig in the "minutes column", labeled in the "activity" column
dat_long_full <- dat_full %>%
filter(!is.na(dur_day_total_vig_min_pla) & !is.na(dur_day_total_mod_min_pla) & !is.na(dur_day_total_lig_min_pla)) %>%
pivot_longer(cols = starts_with("dur_day_total_"),
names_to = "activity",
values_to = "minutes")
# Convert activity names to a factor for consistent ordering in the plot
dat_long_full$activity <- factor(dat_long_full$activity, levels = c("dur_day_total_vig_min_pla", "dur_day_total_mod_min_pla", "dur_day_total_lig_min_pla"))
# average for each activity type by age and sex (14yo_females_light etc)
dat_long_full_minutes_split_sex <- dat_long_full %>%
group_by(age_years, sex, activity) %>%
summarize(minutes = mean(minutes, na.rm = TRUE), .groups = "drop")
# LOESS smoothening -- most smoothed
dat_long_full_minutes_split_sex_99smooth <- dat_long_full_minutes_split_sex %>%
group_by(sex, activity) %>%
mutate(minutes_smoothed = predict(loess(minutes ~ age_years, span = 0.99))) # Adjust span for smoothing
# LOESS smoothening -- less smoothed
dat_long_full_minutes_split_sex_20smooth <- dat_long_full_minutes_split_sex %>%
group_by(sex, activity) %>%
mutate(minutes_smoothed = predict(loess(minutes ~ age_years, span = 0.2))) # Adjust span for smoothing
# no smoothening
dat_long_full_minutes_split_sex_unsmooth <- dat_long_full_minutes_split_sex %>%
group_by(sex, activity)
Steps in full cohort
# average steps by age and sex (14yo_females_steps etc)
dat_full_steps_split_sex <- dat_full %>%
group_by(age_years, sex) %>%
summarize(steps = mean(ad_step_count_sum_0_24hr, na.rm = TRUE), .groups = "drop")
# no smoothening
dat_full_steps_split_sex_unsmooth <- dat_full_steps_split_sex %>%
group_by(sex)
# line with facets by sex -- smoothed line with real data points
ggplot(dat_full_steps_split_sex_unsmooth, aes(x = age_years, y = steps)) +
geom_point(data = dat_full, aes(x = age_years, y = ad_step_count_sum_0_24hr), color = "#7859645D", size = 1) +
geom_smooth(method = "loess", span = 0.99, color = "#785964", fill = "#78596433", linewidth = 1.5) +
labs(y = "Average Daily Steps", x = "Age") +
facet_wrap(~ sex, labeller = as_labeller(c("male" = "Men", "female" = "Women"))) +
scale_y_continuous(labels = function(x) format_thousands(x))
## `geom_smooth()` using formula = 'y ~ x'
## Warning: Removed 2 rows containing non-finite outside the scale range
## (`stat_smooth()`).
## Warning: Removed 195 rows containing missing values or values outside the scale range
## (`geom_point()`).

Activity trends in full cohort
Smoothed
custom_colors <- c("dur_day_total_vig_min_pla" = "#C5A359", "dur_day_total_mod_min_pla" = "#BCCEA1", "dur_day_total_lig_min_pla" = "#CC3D12")
# stacked area plot with facets by sex -- most smoothed
ggplot(dat_long_full_minutes_split_sex_99smooth, aes(x = age_years, y = minutes_smoothed, fill = activity)) +
geom_area(alpha=0.9) +
labs(y = "Average Daily Minutes", x = "Age") +
facet_wrap(~ sex, labeller = as_labeller(c("male" = "Men", "female" = "Women"))) +
scale_fill_manual(name = "Activity Intensity", values = custom_colors,
labels = c("Vigorous", "Moderate", "Light"))

Unsmoothed
ggplot(dat_long_full_minutes_split_sex_unsmooth, aes(x = age_years, y = minutes, fill = activity)) +
geom_area(alpha=0.9) +
labs(y = "Average Daily Minutes", x = "Age") +
facet_wrap(~ sex, labeller = as_labeller(c("male" = "Men", "female" = "Women"))) +
scale_fill_manual(name = "Activity Intensity", values = custom_colors,
labels = c("Vigorous", "Moderate", "Light"))
