From 98c0daf0a96cd395c48ece8e6f282007927169ae Mon Sep 17 00:00:00 2001 From: Samer Mouksassi Date: Tue, 26 Mar 2024 09:59:43 +0200 Subject: [PATCH] added arguments: prob_obs_byexptile, prob_obs_bydose_plac, lineranges_doselabel, proj_bydose these enable various on off customization as per a recent client request --- R/gglogisticexpdist.R | 77 +++++++++++++++++++++++++++++----------- man/gglogisticexpdist.Rd | 12 +++++++ 2 files changed, 68 insertions(+), 21 deletions(-) diff --git a/R/gglogisticexpdist.R b/R/gglogisticexpdist.R index 1a7ee64..c0201ce 100644 --- a/R/gglogisticexpdist.R +++ b/R/gglogisticexpdist.R @@ -39,8 +39,10 @@ plogis <- function(x) exp(x)/(1+exp(x)) #' @param dose_plac_value string identifying placebo in DOSE column #' @param xlab text to be used as x axis label #' @param ylab text to be used as y axis label +#' @param prob_obs_byexptile observed probability by exptile `TRUE`/`FALSE` #' @param prob_text_size probability text size default to 5 #' @param prob_obs_bydose observed probability by dose `TRUE`/`FALSE` +#' @param prob_obs_bydose_plac observed probability by placebo dose `TRUE`/`FALSE` #' @param N_text_size N responders/Ntotal by exposure bin text size default to 5 #' @param binlimits_text_size 5 binlimits text size #' @param binlimits_ypos binlimits y position default to 0 @@ -50,6 +52,8 @@ plogis <- function(x) exp(x)/(1+exp(x)) #' @param dist_scale scaling parameter for ggridges default to 0.9 #' @param lineranges_ypos where to put the lineranges -1 #' @param lineranges_dodge lineranges vertical dodge value 1 +#' @param lineranges_doselabel `TRUE`/`FALSE` +#' @param proj_bydose project the probabilities on logistic curve `TRUE`/`FALSE` #' @param yproj project the probabilities on y axis `TRUE`/`FALSE` #' @param yproj_xpos y projection x position 0 #' @param yproj_dodge y projection dodge value 0.2 @@ -215,8 +219,10 @@ gglogisticexpdist <- function(data = effICGI, dose_plac_value = "Placebo", xlab = "Exposure Values", ylab ="Probability of Response", + prob_obs_byexptile = TRUE, prob_text_size = 5, prob_obs_bydose = TRUE, + prob_obs_bydose_plac = FALSE, N_text_size = 5, binlimits_text_size = 5, binlimits_ypos = 0, @@ -226,6 +232,8 @@ gglogisticexpdist <- function(data = effICGI, dist_scale = 0.9, lineranges_ypos = 0.2, lineranges_dodge = 0.15, + lineranges_doselabel = FALSE, + proj_bydose = TRUE, yproj = TRUE, yproj_xpos = 0, yproj_dodge = 0.2, @@ -500,22 +508,27 @@ gglogisticexpdist <- function(data = effICGI, stat="smooth", method = "glm", method.args = list(family = "binomial"), color="black", alpha = 0.5, - ggplot2::aes(linetype = "Logistic Fit 95% CI"))+ - ggplot2::geom_line(data = predict_by_endpoint_expname_dose, - ggplot2::aes_string(y = "yhat", col = color_fill), - alpha = 0.4, linewidth = 2)+ - ggplot2::geom_line(data = predict_by_endpoint_expname_dose2, - ggplot2::aes_string(y = "yhat", col = color_fill), - alpha = 0.4, linewidth = 2.5)+ - ggplot2::geom_point(data = predict_by_endpoint_expname, - ggplot2::aes_string(x = "medexp", y = "ymid50", col = color_fill), - alpha = 0.4, size = 5) - - + ggplot2::aes(linetype = "Logistic Fit 95% CI")) + + if(proj_bydose){ + p1proj <- p1 + + ggplot2::geom_line(data = predict_by_endpoint_expname_dose, + ggplot2::aes_string(y = "yhat", col = color_fill), + alpha = 0.4, linewidth = 2)+ + ggplot2::geom_line(data = predict_by_endpoint_expname_dose2, + ggplot2::aes_string(y = "yhat", col = color_fill), + alpha = 0.4, linewidth = 2.5)+ + ggplot2::geom_point(data = predict_by_endpoint_expname, + ggplot2::aes_string(x = "medexp", y = "ymid50", col = color_fill), + alpha = 0.4, size = 5) + } + if(!proj_bydose){ + p1proj <- p1 + } if(exposure_distribution=="lineranges") { lineranges_ypos <- as.character(lineranges_ypos) - p1l <- p1 + + p1l <- p1proj + ggplot2::geom_linerange(data = data.long.summaries.dose, linewidth = 2, alpha = 0.4, ggplot2::aes_string(xmin = "quant_10", xmax = "quant_90",y = lineranges_ypos, col = color_fill, @@ -534,22 +547,38 @@ gglogisticexpdist <- function(data = effICGI, group= paste0("interaction(",paste(as.character(c(DOSEinputvar,color_fill)) ,collapse=",",sep=""),")") ), position = ggstance::position_dodgev(height = lineranges_dodge)) + + if(lineranges_doselabel){ + p1lt <- p1l + + ggplot2::geom_text(data=data.long.summaries.dose, vjust = 1, size = 5, show.legend = FALSE, + ggplot2::aes_string(x="quant_90",y = lineranges_ypos, + label = "DOSE", + col = color_fill, + group= paste0("interaction(",paste(as.character(c(DOSEinputvar,color_fill)) ,collapse=",",sep=""),")") + ), + position = ggstance::position_dodgev(height = lineranges_dodge)) + } + if(!lineranges_doselabel){ + p1lt <- p1l + } + } if(exposure_distribution!="lineranges") { - p1l <- p1 + p1lt <- p1proj } - p2e <- p1l + + p2e <- p1lt + ggplot2::geom_pointrange(data = data.long.summaries.exposure, size = 1, ggplot2::aes(shape = "Observed probability by exposure split", x = medexp, y = prob, ymin = prob+1.959*SE, ymax=prob-1.959*SE), alpha = 0.5) if(prob_obs_bydose){ data.long.summaries.dose.plot <- data.long.summaries.dose + if(!prob_obs_bydose_plac){ data.long.summaries.dose.plot[data.long.summaries.dose.plot[,color_fill]==dose_plac_value,"N"] <- NA data.long.summaries.dose.plot[data.long.summaries.dose.plot[,color_fill]==dose_plac_value,"Ntot"] <- NA data.long.summaries.dose.plot[data.long.summaries.dose.plot[,color_fill]==dose_plac_value,"prob"] <- NA - + } p2d <- p2e + ggplot2::geom_pointrange(data = data.long.summaries.dose.plot, alpha = 0.5, size = 1, ggplot2::aes(x = medexp, y = prob, col = !!sym(color_fill), @@ -567,9 +596,15 @@ gglogisticexpdist <- function(data = effICGI, if(!prob_obs_bydose){ p2d <- p2e } - p2 <- p2d + - ggplot2::geom_text(data=data.long.summaries.exposure, vjust = 0, size = prob_text_size, show.legend = FALSE, - ggplot2::aes(x = medexp, y = prob, label = paste(100*round(prob,2),"%","\n",sep="")))+ + if(prob_obs_byexptile) { + p2 <- p2d + + ggplot2::geom_text(data=data.long.summaries.exposure, vjust = 0, size = prob_text_size, show.legend = FALSE, + ggplot2::aes(x = medexp, y = prob, label = paste(100*round(prob,2),"%","\n",sep=""))) + } + if(!prob_obs_byexptile) { + p2 <- p2d + } + p2t <- p2 + ggplot2::geom_text(data = xintercepts, ggplot2::aes(label=round(intercept,1), x = intercept, y = binlimits_ypos) , vjust = 0, size = binlimits_text_size,color = binlimits_color)+ ggplot2::geom_text(data = data.long.summaries.exposure, y = Inf, vjust = 1, size = N_text_size, @@ -578,7 +613,7 @@ gglogisticexpdist <- function(data = effICGI, if(exposure_distribution=="distributions") { data.long.ridges <- data.long data.long.ridges[data.long.ridges[,DOSEinputvar]==dose_plac_value,"expvalue"] <- NA - p2d <- p2 + + p2d <- p2t + ggridges::geom_density_ridges(data = data.long.ridges, ggplot2::aes(x = expvalue, y = keynumeric, group = interaction(!!sym(color_fill),!!sym(DOSEinputvar)), @@ -593,7 +628,7 @@ gglogisticexpdist <- function(data = effICGI, alpha = 0.5, show.legend = FALSE) } if(exposure_distribution!="distributions") { - p2d <- p2 + p2d <- p2t } if(yproj) { diff --git a/man/gglogisticexpdist.Rd b/man/gglogisticexpdist.Rd index a73fa83..fcb7cdf 100644 --- a/man/gglogisticexpdist.Rd +++ b/man/gglogisticexpdist.Rd @@ -18,8 +18,10 @@ gglogisticexpdist( dose_plac_value = "Placebo", xlab = "Exposure Values", ylab = "Probability of Response", + prob_obs_byexptile = TRUE, prob_text_size = 5, prob_obs_bydose = TRUE, + prob_obs_bydose_plac = FALSE, N_text_size = 5, binlimits_text_size = 5, binlimits_ypos = 0, @@ -29,6 +31,8 @@ gglogisticexpdist( dist_scale = 0.9, lineranges_ypos = 0.2, lineranges_dodge = 0.15, + lineranges_doselabel = FALSE, + proj_bydose = TRUE, yproj = TRUE, yproj_xpos = 0, yproj_dodge = 0.2, @@ -65,10 +69,14 @@ gglogisticexpdist( \item{ylab}{text to be used as y axis label} +\item{prob_obs_byexptile}{observed probability by exptile \code{TRUE}/\code{FALSE}} + \item{prob_text_size}{probability text size default to 5} \item{prob_obs_bydose}{observed probability by dose \code{TRUE}/\code{FALSE}} +\item{prob_obs_bydose_plac}{observed probability by placebo dose \code{TRUE}/\code{FALSE}} + \item{N_text_size}{N responders/Ntotal by exposure bin text size default to 5} \item{binlimits_text_size}{5 binlimits text size} @@ -87,6 +95,10 @@ gglogisticexpdist( \item{lineranges_dodge}{lineranges vertical dodge value 1} +\item{lineranges_doselabel}{\code{TRUE}/\code{FALSE}} + +\item{proj_bydose}{project the probabilities on logistic curve \code{TRUE}/\code{FALSE}} + \item{yproj}{project the probabilities on y axis \code{TRUE}/\code{FALSE}} \item{yproj_xpos}{y projection x position 0}