diff --git a/NEWS.md b/NEWS.md index 113ca104..e01ad8b2 100644 --- a/NEWS.md +++ b/NEWS.md @@ -25,6 +25,8 @@ * `facet_title_size`: determines the size of the facet titles. * `export_height`: determines the output height of an exported plot in inches. * `export_width`: determines the output width of an exported plot in inches. + * `x_axis_limits`: user supplied x-axis limits for each plot. + * `colours`: determines colours used for the plot. * `fit_drc_4p()` and `parallel_fit_drc_4p()` have been updated in the latest version of **protti**, leading to slight adjustments in their computational results compared to previous versions. * We added new arguments: * `anova_cutoff` lets you define the ANOVA adjusted p-value cutoff (default 0.05). diff --git a/R/drc_4p_plot.R b/R/drc_4p_plot.R index 910e24f3..bd202362 100644 --- a/R/drc_4p_plot.R +++ b/R/drc_4p_plot.R @@ -44,14 +44,19 @@ plot_drc_4p <- function(...) { #' target was provided) should be \code{"free"} or \code{"fixed"}. #' @param x_axis_scale_log10 a logical value that indicates if the x-axis scale should be log10 #' transformed. +#' @param x_axis_limits a numeric vector of length 2, defining the lower and upper x-axis limit. The +#' default is `c(NA, NA)`, meaning the limits are not defined by the user but by the data. +#' @param colours a character vector containing at least three colours. The first is used for the points, +#' the second for the confidence interval and the third for the curve. By default the first two +#' protti colours are used for the points and confidence interval and the curve is black. #' @param export a logical value that indicates if plots should be exported as PDF. The output #' directory will be the current working directory. The name of the file can be chosen using the #' \code{export_name} argument. If only one target is selected and \code{export = TRUE}, #' the plot is exported and in addition returned in R. #' @param export_height a numeric value that specifies the plot height in inches for an exported plot. -#' The default is `37.5`. For a non-facet plot we recommend using 6. +#' The default is `25`. For a non-facet plot we recommend using 8. #' @param export_width a numeric value that specifies the plot height in inches for an exported plot. -#' The default is `45`. For a non-facet plot we recommend using 8. +#' The default is `30`. For a non-facet plot we recommend using 12. #' @param export_name a character value providing the name of the exported file if #' \code{export = TRUE}. #' @@ -119,9 +124,11 @@ drc_4p_plot <- function(data, facet = TRUE, scales = "free", x_axis_scale_log10 = TRUE, + x_axis_limits = c(NA, NA), + colours = NULL, export = FALSE, - export_height = 37.5, - export_width = 45, + export_height = 25, + export_width = 30, export_name = "dose-response_curves") { . <- NULL @@ -132,6 +139,24 @@ drc_4p_plot <- function(data, if (nrow(data) == 0) stop("Target not found in data!") } + if (missing(colours)) { + protti_colours <- "placeholder" # assign a placeholder to prevent a missing global variable warning + utils::data("protti_colours", envir = environment()) # then overwrite it with real data + colours <- c(protti_colours[1:2], "black") + } else { + if (length(colours) < 3) stop("Please provide at least three colours for the plot!") + } + + if (facet == FALSE) { + # If the plot is not faceted and the user didn't provide heights and widths set them here + if (missing(export_height)) { + export_height <- 8 + } + if (missing(export_width)) { + export_width <- 12 + } + } + data <- data %>% dplyr::ungroup() %>% dplyr::mutate(name = paste0( @@ -156,105 +181,11 @@ drc_4p_plot <- function(data, dplyr::select({{ grouping }}, .data$name, .data$group_number, .data$plot_curve) %>% tidyr::unnest(.data$plot_curve) - if (!"all" %in% targets) { - if (length(targets) == 1) { - input_points_plot <- input_points %>% - dplyr::filter({{ grouping }} == targets) - - input_curve_plot <- input_curve %>% - dplyr::filter({{ grouping }} == targets) - - plot <- ggplot2::ggplot( - data = input_points_plot, - ggplot2::aes( - x = {{ dose }}, - y = {{ response }} - ) - ) + - ggplot2::geom_point(size = 2, col = "#5680C1") + - { - if (nrow(input_curve_plot) != 1) { - ggplot2::geom_ribbon( - data = input_curve_plot, - ggplot2::aes( - x = .data$dose, - y = .data$Prediction, - ymin = .data$Lower, - ymax = .data$Upper - ), - alpha = 0.2, - fill = "#B96DAD" - ) - } - } + - { - if (nrow(input_curve_plot) != 1) { - ggplot2::geom_line( - data = input_curve_plot, - ggplot2::aes( - x = .data$dose, - y = .data$Prediction - ), - size = 1.2 - ) - } - } + - ggplot2::labs( - title = unique(input_points_plot$name), - x = paste0("Concentration [", unit, "]"), - y = y_axis_name - ) + - ggplot2::scale_x_log10() + - ggplot2::theme_bw() + - ggplot2::theme( - plot.title = ggplot2::element_text( - size = 18 - ), - axis.text.x = ggplot2::element_text( - size = 15 - ), - axis.text.y = ggplot2::element_text( - size = 15 - ), - axis.title.y = ggplot2::element_text( - size = 15 - ), - axis.title.x = ggplot2::element_text( - size = 15 - ), - legend.title = ggplot2::element_text( - size = 15 - ), - legend.text = ggplot2::element_text( - size = 15 - ), - strip.text.x = ggplot2::element_text( - size = 15 - ), - strip.text = ggplot2::element_text( - size = 15 - ), - strip.background = element_blank() - ) - - if (export == FALSE) { - return(plot) - } else { - grDevices::pdf( - file = paste0(export_name, ".pdf"), - width = 8, - height = 6 - ) - suppressWarnings(print(plot)) - grDevices::dev.off() - return(plot) - } - } else { - input_points <- input_points %>% - dplyr::filter({{ grouping }} %in% targets) - - input_curve <- input_curve %>% - dplyr::filter({{ grouping }} %in% targets) + if (length(targets) == 1) { + # If the facet argument was not defined set it to FALSE + # because there is just one plot + if (missing(facet)) { + facet <- FALSE } } @@ -284,7 +215,7 @@ drc_4p_plot <- function(data, function(x, y, z) { pb$tick() ggplot2::ggplot(data = x, ggplot2::aes(x = {{ dose }}, y = {{ response }})) + - ggplot2::geom_point(size = 2, col = "#5680C1") + + ggplot2::geom_point(size = 2, col = colours[1]) + { if (nrow(y) != 1) { ggplot2::geom_ribbon( @@ -296,7 +227,7 @@ drc_4p_plot <- function(data, ymax = .data$Upper ), alpha = 0.2, - fill = "#B96DAD" + fill = colours[2] ) } } + @@ -308,7 +239,8 @@ drc_4p_plot <- function(data, x = .data$dose, y = .data$Prediction ), - size = 1.2 + size = 1.2, + col = colours[3] ) } } + @@ -331,41 +263,30 @@ drc_4p_plot <- function(data, } } + { - if (x_axis_scale_log10 == TRUE) ggplot2::scale_x_log10() + if (x_axis_scale_log10 == TRUE) { + ggplot2::scale_x_log10(limits = x_axis_limits) + } else { + ggplot2::scale_x_continuous(limits = x_axis_limits) + } } + { if (facet == TRUE) ggplot2::facet_wrap(~ .data$name, scales = scales, ncol = 4) } + ggplot2::theme_bw() + ggplot2::theme( - plot.title = ggplot2::element_text( - size = 18 - ), - axis.text.x = ggplot2::element_text( - size = 15 - ), - axis.text.y = ggplot2::element_text( - size = 15 - ), - axis.title.y = ggplot2::element_text( - size = 15 - ), - axis.title.x = ggplot2::element_text( - size = 15 - ), - legend.title = ggplot2::element_text( - size = 15 - ), - legend.text = ggplot2::element_text( - size = 15 - ), - strip.text.x = ggplot2::element_text( - size = facet_title_size - ), + plot.title = ggplot2::element_text(size = 18), + axis.text.x = ggplot2::element_text(size = 15), + axis.text.y = ggplot2::element_text(size = 15), + axis.title.y = ggplot2::element_text(size = 15), + axis.title.x = ggplot2::element_text(size = 15), + legend.title = ggplot2::element_text(size = 15), + legend.text = ggplot2::element_text(size = 15), + strip.text.x = ggplot2::element_text(size = facet_title_size), strip.background = element_blank() ) } ) + if (export == FALSE) { plots } else { diff --git a/man/drc_4p_plot.Rd b/man/drc_4p_plot.Rd index b7ebe8b8..f53a179b 100644 --- a/man/drc_4p_plot.Rd +++ b/man/drc_4p_plot.Rd @@ -16,9 +16,11 @@ drc_4p_plot( facet = TRUE, scales = "free", x_axis_scale_log10 = TRUE, + x_axis_limits = c(NA, NA), + colours = NULL, export = FALSE, - export_height = 37.5, - export_width = 45, + export_height = 25, + export_width = 30, export_name = "dose-response_curves" ) } @@ -55,16 +57,23 @@ target was provided) should be \code{"free"} or \code{"fixed"}.} \item{x_axis_scale_log10}{a logical value that indicates if the x-axis scale should be log10 transformed.} +\item{x_axis_limits}{a numeric vector of length 2, defining the lower and upper x-axis limit. The +default is \code{c(NA, NA)}, meaning the limits are not defined by the user but by the data.} + +\item{colours}{a character vector containing at least three colours. The first is used for the points, +the second for the confidence interval and the third for the curve. By default the first two +protti colours are used for the points and confidence interval and the curve is black.} + \item{export}{a logical value that indicates if plots should be exported as PDF. The output directory will be the current working directory. The name of the file can be chosen using the \code{export_name} argument. If only one target is selected and \code{export = TRUE}, the plot is exported and in addition returned in R.} \item{export_height}{a numeric value that specifies the plot height in inches for an exported plot. -The default is \code{37.5}. For a non-facet plot we recommend using 6.} +The default is \code{25}. For a non-facet plot we recommend using 8.} \item{export_width}{a numeric value that specifies the plot height in inches for an exported plot. -The default is \code{45}. For a non-facet plot we recommend using 8.} +The default is \code{30}. For a non-facet plot we recommend using 12.} \item{export_name}{a character value providing the name of the exported file if \code{export = TRUE}.} diff --git a/tests/testthat/test-workflow.R b/tests/testthat/test-workflow.R index d73b419d..278b6a21 100644 --- a/tests/testthat/test-workflow.R +++ b/tests/testthat/test-workflow.R @@ -677,7 +677,7 @@ if (Sys.getenv("TEST_PROTTI") == "true") { )) }) - expect_is(p, "ggplot") + expect_is(p, "list") expect_warning(print(p), NA) rlang::with_options(lifecycle_verbosity = "warning", { @@ -707,7 +707,7 @@ if (Sys.getenv("TEST_PROTTI") == "true") { y_axis_name = "test y-Axis" ) - expect_is(p, "ggplot") + expect_is(p, "list") expect_warning(print(p), NA) p_facet <- drc_4p_plot(