From 2ce5c7476924b440cf074c995396e913e7d1c8f6 Mon Sep 17 00:00:00 2001 From: Saranjeet Kaur Date: Fri, 30 Aug 2024 15:29:33 +0100 Subject: [PATCH] update riskfactors_diff function --- R/data-process.R | 4 +-- R/health-plots.R | 45 ++++++++++++++++++++---------- man/gen_data_weighted_rf.Rd | 4 +-- man/riskfactors_diff.Rd | 16 +++++++++-- tests/testthat/test-health-plots.R | 31 ++++++++++++-------- 5 files changed, 68 insertions(+), 32 deletions(-) diff --git a/R/data-process.R b/R/data-process.R index 9090284..6f11870 100644 --- a/R/data-process.R +++ b/R/data-process.R @@ -90,12 +90,12 @@ gen_data_weighted <- function(data) { return(data_weighted) } -#' Calculate Differences for Various Metrics +#' Calculate Differences for Risk Factors #' #' This function calculates the differences between intervention and baseline values for risk factors. #' #' @param data_weighted A data frame containing weighted mean values for various metrics. -#' @return A data frame with differences between intervention and baseline values for various metrics. +#' @return A data frame with differences between intervention and baseline values for risk factors. #' @export gen_data_weighted_rf <- function(data_weighted) { data_weighted_rf <- dplyr::select(data_weighted, diff --git a/R/health-plots.R b/R/health-plots.R index ffe39b5..2023fb3 100644 --- a/R/health-plots.R +++ b/R/health-plots.R @@ -51,10 +51,17 @@ riskfactors <- function(riskft, data_mean_weighted) { #' #' @param riskft_diff A character string specifying the difference in risk factor to plot. #' Options are: "bmi", "ei", "obesity", "sodium". -#' @param data_mean_weighted_rf_wide A data frame containing the weighted mean values of risk factors. +#' @param data_weighted_rf_wide_collapse A data frame with differences between intervention and baseline values for risk factors. +#' @param scale_y_continuous_limits A numeric vector specifying the limits of the scales for continuous y aesthetics. +#' @param scale_y_continuous_breaks A numeric vector specifying the breaks of the scales for continuous y aesthetics. +#' @param scale_y_continuous_labels A numeric vector specifying the labels of the scales for continuous y aesthetics. #' @return A ggplot object representing the specified plot. #' @export -riskfactors_diff <- function(riskft_diff, data_mean_weighted_rf_wide) { +riskfactors_diff <- function(riskft_diff, + data_weighted_rf_wide_collapse, + scale_y_continuous_limits = NULL, + scale_y_continuous_breaks = ggplot2::waiver(), + scale_y_continuous_labels = ggplot2::waiver()) { riskft_diffs <- c("bmi", "ei", "obesity", "sodium") if (!(riskft_diff %in% riskft_diffs)) { @@ -68,29 +75,37 @@ riskfactors_diff <- function(riskft_diff, data_mean_weighted_rf_wide) { sodium = "Sodium") y_value <- switch(riskft_diff, - bmi = "diff_bmi", - ei = "diff_ei", - obesity = "diff_obesity", - sodium = "diff_sodium") + bmi = "diff_bmi_mean", + ei = "diff_ei_mean", + obesity = "diff_obesity_mean", + sodium = "diff_sodium_mean") plot_title <- switch(riskft_diff, - bmi = "Reduction in BMI under intervention", - ei = "Reduction in energy intake (kcal) under intervention", - obesity = "Reduction in obesity prevalence under intervention", - sodium = "Reduction in sodium (mg) under intervention") + bmi = "Reduction in BMI by income class", + ei = "Reduction in energy intake (kcal) by income class", + obesity = "Reduction in obesity prevalence by income class", + sodium = "Reduction in sodium (mg) by income class") - ggplot2::ggplot(data = data_mean_weighted_rf_wide, - ggplot2::aes(x = data_mean_weighted_rf_wide$timediff, - y = get(y_value))) + - ggplot2::geom_line(colour = "blue", linewidth = 1) + + ggplot2::ggplot(data = data_weighted_rf_wide_collapse, + ggplot2::aes(x = data_weighted_rf_wide_collapse$time, + y = get(y_value), + colour = data_weighted_rf_wide_collapse$income)) + + ggplot2::geom_line(linewidth = 1) + + ggplot2::geom_ribbon(ggplot2::aes(ymin = data_weighted_rf_wide_collapse$diff_sodium_min, + ymax = data_weighted_rf_wide_collapse$diff_sodium_max), + alpha = 0.2) + ggplot2::ggtitle(plot_title) + ggplot2::xlab("Year") + ggplot2::ylab(y_label) + ggplot2::scale_x_continuous(limits = c(-3, 32), breaks = c(-3, 2, 7, 12, 17, 22, 27, 32), labels = c(2020, 2025, 2030, 2035, 2040, 2045, 2050, 2055)) + + ggplot2::scale_y_continuous(limits = scale_y_continuous_limits, + breaks = scale_y_continuous_breaks, + labels = scale_y_continuous_labels) + ggplot2::labs(alt = "A line plot showing the reduction in a specified risk factor under intervention over time") + - hgps_theme() + hgps_theme() + + ggplot2::theme(legend.position = c(0.85,0.22)) } #' Plot of Incidence Difference diff --git a/man/gen_data_weighted_rf.Rd b/man/gen_data_weighted_rf.Rd index 3b23c1a..0b2330b 100644 --- a/man/gen_data_weighted_rf.Rd +++ b/man/gen_data_weighted_rf.Rd @@ -2,7 +2,7 @@ % Please edit documentation in R/data-process.R \name{gen_data_weighted_rf} \alias{gen_data_weighted_rf} -\title{Calculate Differences for Various Metrics} +\title{Calculate Differences for Risk Factors} \usage{ gen_data_weighted_rf(data_weighted) } @@ -10,7 +10,7 @@ gen_data_weighted_rf(data_weighted) \item{data_weighted}{A data frame containing weighted mean values for various metrics.} } \value{ -A data frame with differences between intervention and baseline values for various metrics. +A data frame with differences between intervention and baseline values for risk factors. } \description{ This function calculates the differences between intervention and baseline values for risk factors. diff --git a/man/riskfactors_diff.Rd b/man/riskfactors_diff.Rd index 2dacec3..8e6e332 100644 --- a/man/riskfactors_diff.Rd +++ b/man/riskfactors_diff.Rd @@ -4,13 +4,25 @@ \alias{riskfactors_diff} \title{Plot of Difference in Risk Factor} \usage{ -riskfactors_diff(riskft_diff, data_mean_weighted_rf_wide) +riskfactors_diff( + riskft_diff, + data_weighted_rf_wide_collapse, + scale_y_continuous_limits = NULL, + scale_y_continuous_breaks = ggplot2::waiver(), + scale_y_continuous_labels = ggplot2::waiver() +) } \arguments{ \item{riskft_diff}{A character string specifying the difference in risk factor to plot. Options are: "bmi", "ei", "obesity", "sodium".} -\item{data_mean_weighted_rf_wide}{A data frame containing the weighted mean values of risk factors.} +\item{data_weighted_rf_wide_collapse}{A data frame with differences between intervention and baseline values for risk factors.} + +\item{scale_y_continuous_limits}{A numeric vector specifying the limits of the scales for continuous y aesthetics.} + +\item{scale_y_continuous_breaks}{A numeric vector specifying the breaks of the scales for continuous y aesthetics.} + +\item{scale_y_continuous_labels}{A numeric vector specifying the labels of the scales for continuous y aesthetics.} } \value{ A ggplot object representing the specified plot. diff --git a/tests/testthat/test-health-plots.R b/tests/testthat/test-health-plots.R index 3242e3a..d3ba87d 100644 --- a/tests/testthat/test-health-plots.R +++ b/tests/testthat/test-health-plots.R @@ -36,27 +36,36 @@ test_that("riskfactors function works correctly", { # Testing riskfactors_diff() function test_that("riskfactors_diff function works correctly", { # Create sample data - data_mean_weighted_rf_wide <- data.frame( - timediff = seq(-9, 21, by = 1), - diff_bmi = runif(31, -3, 0), - diff_ei = runif(31, -200, 0), - diff_obesity = runif(31, -0.3, 0), - diff_sodium = runif(31, -700, 0) + data_weighted_rf_wide_collapse <- data.frame( + time = seq(-9, 21, by = 1), + income = c(rep("low",9), rep("middle", 16), rep("high", 6)), + diff_bmi_mean = runif(31, -3, 0), + diff_bmi_max = runif(31, -5, 0), + diff_bmi_min = runif(31, -1, 0), + diff_ei_mean = runif(31, -200, 0), + diff_ei_max = runif(31, -300, 0), + diff_ei_min = runif(31, -100, 0), + diff_obesity_mean = runif(31, -0.3, 0), + diff_obesity_max = runif(31, -0.5, 0), + diff_obesity_min = runif(31, -0.1, 0), + diff_sodium_mean = runif(31, -700, 0), + diff_sodium_max = runif(31, -1000, 0), + diff_sodium_min = runif(31, -300, 0) ) # Test for valid input - plot_ei <- riskfactors_diff("ei", data_mean_weighted_rf_wide) + plot_ei <- riskfactors_diff("ei", data_weighted_rf_wide_collapse, scale_y_continuous_limits = c(-38.3,0), scale_y_continuous_breaks = c(-38.3,-19.2,0), scale_y_continuous_labels = c(-38.3,-19.2,0)) expect_s3_class(plot_ei, "ggplot") - expect_equal(plot_ei$labels$title, "Reduction in energy intake (kcal) under intervention") + expect_equal(plot_ei$labels$title, "Reduction in energy intake (kcal) by income class") expect_equal(plot_ei$labels$y, "Energy") - plot_obesity <- riskfactors_diff("obesity", data_mean_weighted_rf_wide) + plot_obesity <- riskfactors_diff("obesity", data_weighted_rf_wide_collapse, scale_y_continuous_limits = c(-0.0135,0), scale_y_continuous_breaks = c(-0.0135,-0.00675,0), scale_y_continuous_labels = c("-1.35%","-0.675%","0")) expect_s3_class(plot_obesity, "ggplot") - expect_equal(plot_obesity$labels$title, "Reduction in obesity prevalence under intervention") + expect_equal(plot_obesity$labels$title, "Reduction in obesity prevalence by income class") expect_equal(plot_obesity$labels$y, "Obesity") # Test for invalid input - expect_error(riskfactors_diff("invalid_riskft_diff", data_mean_weighted_rf_wide), + expect_error(riskfactors_diff("invalid_riskft_diff", data_weighted_rf_wide_collapse), "Invalid risk factor difference. Choose from: 'bmi', 'ei', 'obesity', 'sodium'.") })