Skip to content

Commit

Permalink
update riskfactors_diff function
Browse files Browse the repository at this point in the history
  • Loading branch information
SaranjeetKaur committed Aug 30, 2024
1 parent 70c7a7b commit 2ce5c74
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 32 deletions.
4 changes: 2 additions & 2 deletions R/data-process.R
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
45 changes: 30 additions & 15 deletions R/health-plots.R
Original file line number Diff line number Diff line change
Expand Up @@ -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)) {
Expand All @@ -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
Expand Down
4 changes: 2 additions & 2 deletions man/gen_data_weighted_rf.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 14 additions & 2 deletions man/riskfactors_diff.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

31 changes: 20 additions & 11 deletions tests/testthat/test-health-plots.R
Original file line number Diff line number Diff line change
Expand Up @@ -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'.")
})

Expand Down

0 comments on commit 2ce5c74

Please sign in to comment.