Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add faceting variable to g_lineplot. add code to maintain factor levels. #1226

Merged
merged 7 commits into from
Apr 16, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 27 additions & 6 deletions R/g_lineplot.R
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
#' subtitle. Can be `NA` if `paramcd` is not to be added to the y-axis label or subtitle.
#' * `y_unit` (`string` or `NA`)\cr name of variable with units of `y`. Used for y-axis label and plot's subtitle.
#' Can be `NA` if y unit is not to be added to the y-axis label or subtitle.
#' * `facet_var` (`string` or `NA`)\cr name of the secondary grouping variable used for plot faceting, i.e. treatment
#' arm. Can be `NA` to indicate lack of groups.
#' @param mid (`character` or `NULL`)\cr names of the statistics that will be plotted as midpoints.
#' All the statistics indicated in `mid` variable must be present in the object returned by `sfun`,
#' and be of a `double` or `numeric` type vector of length one.
Expand Down Expand Up @@ -188,6 +190,11 @@
group_var <- variables[["group_var"]]
subject_var <- variables[["subject_var"]]
}
if (is.na(variables["facet_var"])) {
facet_var <- NULL # NULL if facet_var == NA or it is not in variables
} else {
facet_var <- variables[["facet_var"]]
}
checkmate::assert_flag(y_lab_add_paramcd, null.ok = TRUE)
checkmate::assert_flag(subtitle_add_paramcd, null.ok = TRUE)
if ((!is.null(y_lab) && y_lab_add_paramcd) || (!is.null(subtitle) && subtitle_add_paramcd)) {
Expand All @@ -209,18 +216,20 @@
####################################### |
# ---- Compute required statistics ----
####################################### |
if (!is.null(group_var)) {
if (!is.null(facet_var) & !is.null(group_var)) {

Check warning on line 219 in R/g_lineplot.R

View workflow job for this annotation

GitHub Actions / SuperLinter 🦸‍♀️ / Lint R code 🧶

file=R/g_lineplot.R,line=219,col=27,[vector_logic_linter] Conditional expressions require scalar logical operators (&& and ||)
df_grp <- tidyr::expand(df, .data[[facet_var]], .data[[group_var]], .data[[x]]) # expand based on levels of factors
} else if (!is.null(group_var)) {
df_grp <- tidyr::expand(df, .data[[group_var]], .data[[x]]) # expand based on levels of factors
} else {
df_grp <- tidyr::expand(df, NULL, .data[[x]])
}
df_grp <- df_grp %>%
dplyr::full_join(y = df[, c(group_var, x, y)], by = c(group_var, x), multiple = "all") %>%
dplyr::group_by_at(c(group_var, x))
dplyr::full_join(y = df[, c(facet_var, group_var, x, y)], by = c(facet_var, group_var, x), multiple = "all") %>%
dplyr::group_by_at(c(facet_var, group_var, x))

df_stats <- df_grp %>%
dplyr::summarise(
data.frame(t(do.call(c, unname(sfun(.data[[y]], ...)[c(mid, interval)])))),
data.frame(t(do.call(c, unname(sfun(.data[[y]])[c(mid, interval)])))),
.groups = "drop"
)

Expand All @@ -234,7 +243,12 @@
colnames(df_N) <- c(group_var, "N") # nolint
df_N[[strata_N]] <- paste0(df_N[[group_var]], " (N = ", df_N$N, ")") # nolint

# strata_N should not be in clonames(df_stats)
# keep strata factor levels
matches <- sapply(unique(df_N[[group_var]]), function(x) unique(df_N[[paste0(group_var, "_N")]])[grepl(paste0("^", x), unique(df_N[[paste0(group_var, "_N")]]))])

Check warning on line 247 in R/g_lineplot.R

View workflow job for this annotation

GitHub Actions / SuperLinter 🦸‍♀️ / Lint R code 🧶

file=R/g_lineplot.R,line=247,col=121,[line_length_linter] Lines should not be more than 120 characters. This line is 165 characters.
df_N[[paste0(group_var, "_N")]] <- factor(df_N[[group_var]])

Check warning on line 248 in R/g_lineplot.R

View workflow job for this annotation

GitHub Actions / SuperLinter 🦸‍♀️ / Lint R code 🧶

file=R/g_lineplot.R,line=248,col=5,[object_name_linter] Variable and function name style should match snake_case or symbols.
levels(df_N[[paste0(group_var, "_N")]]) <- unlist(matches)

Check warning on line 249 in R/g_lineplot.R

View workflow job for this annotation

GitHub Actions / SuperLinter 🦸‍♀️ / Lint R code 🧶

file=R/g_lineplot.R,line=249,col=12,[object_name_linter] Variable and function name style should match snake_case or symbols.

# strata_N should not be in colnames(df_stats)
checkmate::assert_disjunct(strata_N, colnames(df_stats))

df_stats <- merge(x = df_stats, y = df_N[, c(group_var, strata_N)], by = group_var)
Expand Down Expand Up @@ -346,6 +360,11 @@
ggplot2::scale_color_manual(values = col)
}

if (!is.null(facet_var)) {
p <- p +
facet_grid(cols = vars(df_stats[[facet_var]]))
}

if (!is.null(ggtheme)) {
p <- p + ggtheme
} else {
Expand Down Expand Up @@ -500,6 +519,7 @@
control_lineplot_vars <- function(x = "AVISIT",
y = "AVAL",
group_var = "ARM",
facet_var = NA,
paramcd = "PARAMCD",
y_unit = "AVALU",
subject_var = "USUBJID",
Expand All @@ -518,10 +538,11 @@
checkmate::assert_string(x)
checkmate::assert_string(y)
checkmate::assert_string(group_var, na.ok = TRUE, null.ok = TRUE)
checkmate::assert_string(facet_var, na.ok = TRUE, null.ok = TRUE)
checkmate::assert_string(subject_var, na.ok = TRUE, null.ok = TRUE)
checkmate::assert_string(paramcd, na.ok = TRUE, null.ok = TRUE)
checkmate::assert_string(y_unit, na.ok = TRUE, null.ok = TRUE)

variables <- c(x = x, y = y, group_var = group_var, paramcd = paramcd, y_unit = y_unit, subject_var = subject_var)
variables <- c(x = x, y = y, group_var = group_var, paramcd = paramcd, y_unit = y_unit, subject_var = subject_var, facet_var = facet_var)

Check warning on line 546 in R/g_lineplot.R

View workflow job for this annotation

GitHub Actions / SuperLinter 🦸‍♀️ / Lint R code 🧶

file=R/g_lineplot.R,line=546,col=121,[line_length_linter] Lines should not be more than 120 characters. This line is 139 characters.
return(variables)
}
1 change: 1 addition & 0 deletions man/control_lineplot_vars.Rd

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

2 changes: 2 additions & 0 deletions man/g_lineplot.Rd

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

Loading
Loading