From e8bc12c9ec198a4633f05bb84fa18b5d5df3b537 Mon Sep 17 00:00:00 2001 From: kartikeya kirar Date: Fri, 31 May 2024 11:31:40 +0530 Subject: [PATCH] 241: adding parameter to adjust dot size. (#242) Fixes https://github.com/insightsengineering/goshawk/issues/241 I have added dot_size as a parameter to adjust the size and default to 3.
goshawk examples ```r pkgload::load_all("goshawk") library(stringr) # original ARM value = dose value arm_mapping <- list( "A: Drug X" = "150mg QD", "B: Placebo" = "Placebo", "C: Combination" = "Combination" ) color_manual <- c("150mg QD" = "#000000", "Placebo" = "#3498DB", "Combination" = "#E74C3C") ADLB <- rADLB var_labels <- lapply(ADLB, function(x) attributes(x)$label) ADLB <- ADLB %>% mutate(AVISITCD = case_when( AVISIT == "SCREENING" ~ "SCR", AVISIT == "BASELINE" ~ "BL", grepl("WEEK", AVISIT) ~ paste( "W", trimws( substr( AVISIT, start = 6, stop = str_locate(AVISIT, "DAY") - 1 ) ) ), TRUE ~ NA_character_ )) %>% mutate(AVISITCDN = case_when( AVISITCD == "SCR" ~ -2, AVISITCD == "BL" ~ 0, grepl("W", AVISITCD) ~ as.numeric(gsub("\\D+", "", AVISITCD)), TRUE ~ NA_real_ )) %>% # use ARMCD values to order treatment in visualization legend mutate(TRTORD = ifelse(grepl("C", ARMCD), 1, ifelse(grepl("B", ARMCD), 2, ifelse(grepl("A", ARMCD), 3, NA) ) )) %>% mutate(ARM = as.character(arm_mapping[match(ARM, names(arm_mapping))])) %>% mutate(ARM = factor(ARM) %>% reorder(TRTORD)) %>% mutate(ANRLO = .5, ANRHI = 1) %>% rowwise() %>% group_by(PARAMCD) %>% mutate(LBSTRESC = ifelse(USUBJID %in% sample(USUBJID, 1, replace = TRUE), paste("<", round(runif(1, min = .5, max = .7))), LBSTRESC )) %>% mutate(LBSTRESC = ifelse(USUBJID %in% sample(USUBJID, 1, replace = TRUE), paste(">", round(runif(1, min = .9, max = 1.2))), LBSTRESC )) %>% ungroup() attr(ADLB[["ARM"]], "label") <- var_labels[["ARM"]] attr(ADLB[["ANRLO"]], "label") <- "Analysis Normal Range Lower Limit" attr(ADLB[["ANRHI"]], "label") <- "Analysis Normal Range Upper Limit" # add LLOQ and ULOQ variables ADLB_LOQS <- goshawk:::h_identify_loq_values(ADLB, "LOQFL") ADLB <- left_join(ADLB, ADLB_LOQS, by = "PARAM") g_spaghettiplot( data = ADLB, subj_id = "USUBJID", biomarker_var = "PARAMCD", biomarker = "CRP", value_var = "AVAL", trt_group = "ARM", time = "AVISITCD", color_manual = color_manual, color_comb = "#39ff14", alpha = .02, xtick = c("BL", "W 1", "W 4"), xlabel = c("Baseline", "Week 1", "Week 4"), rotate_xlab = FALSE, group_stats = "median", hline_vars = c("ANRHI", "ANRLO"), hline_vars_colors = c("pink", "brown"), dot_size = 2 ) g_spaghettiplot( data = ADLB, subj_id = "USUBJID", biomarker_var = "PARAMCD", biomarker = "CRP", value_var = "AVAL", trt_group = "ARM", time = "AVISITCD", color_manual = color_manual, color_comb = "#39ff14", alpha = .02, xtick = c("BL", "W 1", "W 4"), xlabel = c("Baseline", "Week 1", "Week 4"), rotate_xlab = FALSE, group_stats = "median", hline_arb = 1.3, hline_vars = c("ANRHI", "ANRLO", "ULOQN", "LLOQN"), hline_vars_colors = c("pink", "brown", "purple", "gray"), dot_size = 5 ) ```
--- R/g_lineplot.R | 12 ++++++++---- R/g_spaghettiplot.R | 13 +++++++++---- man/g_lineplot.Rd | 9 +++++++-- man/g_spaghettiplot.Rd | 12 +++++++++--- 4 files changed, 33 insertions(+), 13 deletions(-) diff --git a/R/g_lineplot.R b/R/g_lineplot.R index ac6bc712..35da3ff3 100644 --- a/R/g_lineplot.R +++ b/R/g_lineplot.R @@ -29,6 +29,7 @@ #' Default value is `ggplot2::waiver()`. #' @param rotate_xlab boolean whether to rotate x-axis labels. #' @param plot_font_size control font size for title, x-axis, y-axis and legend font. +#' @param dot_size plot dot size. Default to 3. #' @param dodge control position dodge. #' @param plot_height height of produced plot. 989 pixels by default. #' @param count_threshold \code{integer} minimum number observations needed to show the appropriate @@ -207,7 +208,8 @@ #' xtick = c(0, 1, 5), #' xlabel = c("Baseline", "Week 1", "Week 5"), #' rotate_xlab = FALSE, -#' plot_height = 1500 +#' plot_height = 1500, +#' dot_size = 1 #' ) #' #' g_lineplot( @@ -227,7 +229,8 @@ #' xtick = c(0, 1, 5), #' xlabel = c("Baseline", "Week 1", "Week 5"), #' rotate_xlab = FALSE, -#' plot_height = 1500 +#' plot_height = 1500, +#' dot_size = 4 #' ) g_lineplot <- function(label = "Line Plot", data, @@ -254,6 +257,7 @@ g_lineplot <- function(label = "Line Plot", xlabel = xtick, rotate_xlab = FALSE, plot_font_size = 12, + dot_size = 3, dodge = 0.4, plot_height = 989, count_threshold = 0, @@ -405,7 +409,7 @@ g_lineplot <- function(label = "Line Plot", ) ) + ggplot2::theme_bw() + - ggplot2::geom_point(position = pd) + + ggplot2::geom_point(position = pd, size = dot_size) + ggplot2::scale_color_manual( values = color_manual, name = trt_label, guide = ggplot2::guide_legend(ncol = 3, order = 1) ) + @@ -443,7 +447,7 @@ g_lineplot <- function(label = "Line Plot", ggplot2::scale_linetype_manual(" ", values = type_mapping, guide = ggplot2::guide_legend(ncol = 3, order = 1)) + ggplot2::scale_shape_manual(" ", values = shape_mapping, guide = ggplot2::guide_legend(ncol = 3, order = 1)) + ggplot2::theme(legend.key.size = grid::unit(1, "cm")) + - ggplot2::geom_point(position = pd, size = 3) + ggplot2::geom_point(position = pd, size = dot_size) } plot1 <- plot1 + diff --git a/R/g_spaghettiplot.R b/R/g_spaghettiplot.R index d1585cf1..9b0e01f0 100644 --- a/R/g_spaghettiplot.R +++ b/R/g_spaghettiplot.R @@ -30,6 +30,7 @@ #' value is `ggplot2::waiver()`. #' @param rotate_xlab boolean whether to rotate x-axis labels. #' @param font_size control font size for title, x-axis, y-axis and legend font. +#' @param dot_size plot dot size. Default to 2. #' @param group_stats control group mean or median overlay. #' @param hline_arb ('numeric vector') value identifying intercept for arbitrary horizontal lines. #' @param hline_arb_color ('character vector') optional, color for the arbitrary horizontal lines. @@ -145,7 +146,8 @@ #' group_stats = "median", #' hline_arb = 1.3, #' hline_vars = c("ANRHI", "ANRLO", "ULOQN", "LLOQN"), -#' hline_vars_colors = c("pink", "brown", "purple", "gray") +#' hline_vars_colors = c("pink", "brown", "purple", "gray"), +#' dot_size = 3 #' ) #' #' g_spaghettiplot( @@ -166,7 +168,8 @@ #' hline_arb = c(.5, .7, 1), #' hline_arb_color = c("blue", "red", "green"), #' hline_arb_label = c("Arb_Hori_line_A", "Arb_Hori_line_B", "Arb_Hori_line_C"), -#' hline_vars = c("ANRHI", "ANRLO") +#' hline_vars = c("ANRHI", "ANRLO"), +#' dot_size = 4 #' ) #' #' # removing missing levels from the plot with facet_scales @@ -208,7 +211,8 @@ #' hline_arb = c(.5, .7, 1), #' hline_arb_color = c("blue", "red", "green"), #' hline_arb_label = c("Arb_Hori_line_A", "Arb_Hori_line_B", "Arb_Hori_line_C"), -#' hline_vars = c("ANRHI", "ANRLO") +#' hline_vars = c("ANRHI", "ANRLO"), +#' dot_size = 1 #' ) #' g_spaghettiplot <- function(data, @@ -233,6 +237,7 @@ g_spaghettiplot <- function(data, xlabel = xtick, rotate_xlab = FALSE, font_size = 12, + dot_size = 2, group_stats = "NONE", hline_arb = numeric(0), hline_arb_color = "red", @@ -288,7 +293,7 @@ g_spaghettiplot <- function(data, data = plot_data, ggplot2::aes(x = !!sym(time), y = !!sym(value_var), color = !!sym(trt_group), group = !!sym(subj_id)) ) + - ggplot2::geom_point(size = 0.8, na.rm = TRUE, ggplot2::aes(shape = !!sym(loq_flag_var))) + + ggplot2::geom_point(size = dot_size, na.rm = TRUE, ggplot2::aes(shape = !!sym(loq_flag_var))) + ggplot2::geom_line(linewidth = 0.4, alpha = alpha, na.rm = TRUE) + ggplot2::facet_wrap(trt_group, ncol = facet_ncol, scales = facet_scales) + ggplot2::labs(caption = caption_loqs_label) + diff --git a/man/g_lineplot.Rd b/man/g_lineplot.Rd index 225f41ce..49801cdb 100644 --- a/man/g_lineplot.Rd +++ b/man/g_lineplot.Rd @@ -30,6 +30,7 @@ g_lineplot( xlabel = xtick, rotate_xlab = FALSE, plot_font_size = 12, + dot_size = 3, dodge = 0.4, plot_height = 989, count_threshold = 0, @@ -92,6 +93,8 @@ Default value is \code{ggplot2::waiver()}.} \item{plot_font_size}{control font size for title, x-axis, y-axis and legend font.} +\item{dot_size}{plot dot size. Default to 3.} + \item{dodge}{control position dodge.} \item{plot_height}{height of produced plot. 989 pixels by default.} @@ -273,7 +276,8 @@ g_lineplot( xtick = c(0, 1, 5), xlabel = c("Baseline", "Week 1", "Week 5"), rotate_xlab = FALSE, - plot_height = 1500 + plot_height = 1500, + dot_size = 1 ) g_lineplot( @@ -293,7 +297,8 @@ g_lineplot( xtick = c(0, 1, 5), xlabel = c("Baseline", "Week 1", "Week 5"), rotate_xlab = FALSE, - plot_height = 1500 + plot_height = 1500, + dot_size = 4 ) } \author{ diff --git a/man/g_spaghettiplot.Rd b/man/g_spaghettiplot.Rd index ccaa5780..81b40724 100644 --- a/man/g_spaghettiplot.Rd +++ b/man/g_spaghettiplot.Rd @@ -27,6 +27,7 @@ g_spaghettiplot( xlabel = xtick, rotate_xlab = FALSE, font_size = 12, + dot_size = 2, group_stats = "NONE", hline_arb = numeric(0), hline_arb_color = "red", @@ -87,6 +88,8 @@ value is \code{ggplot2::waiver()}.} \item{font_size}{control font size for title, x-axis, y-axis and legend font.} +\item{dot_size}{plot dot size. Default to 2.} + \item{group_stats}{control group mean or median overlay.} \item{hline_arb}{('numeric vector') value identifying intercept for arbitrary horizontal lines.} @@ -207,7 +210,8 @@ g_spaghettiplot( group_stats = "median", hline_arb = 1.3, hline_vars = c("ANRHI", "ANRLO", "ULOQN", "LLOQN"), - hline_vars_colors = c("pink", "brown", "purple", "gray") + hline_vars_colors = c("pink", "brown", "purple", "gray"), + dot_size = 3 ) g_spaghettiplot( @@ -228,7 +232,8 @@ g_spaghettiplot( hline_arb = c(.5, .7, 1), hline_arb_color = c("blue", "red", "green"), hline_arb_label = c("Arb_Hori_line_A", "Arb_Hori_line_B", "Arb_Hori_line_C"), - hline_vars = c("ANRHI", "ANRLO") + hline_vars = c("ANRHI", "ANRLO"), + dot_size = 4 ) # removing missing levels from the plot with facet_scales @@ -270,7 +275,8 @@ g_spaghettiplot( hline_arb = c(.5, .7, 1), hline_arb_color = c("blue", "red", "green"), hline_arb_label = c("Arb_Hori_line_A", "Arb_Hori_line_B", "Arb_Hori_line_C"), - hline_vars = c("ANRHI", "ANRLO") + hline_vars = c("ANRHI", "ANRLO"), + dot_size = 1 ) }