Skip to content

Commit

Permalink
215 add in changes from pr #275 return ggplot obj
Browse files Browse the repository at this point in the history
  • Loading branch information
cthunes committed Dec 5, 2024
1 parent 29cfea3 commit e78590f
Showing 1 changed file with 18 additions and 12 deletions.
30 changes: 18 additions & 12 deletions R/tcplPlot.R
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@
#' \dontrun{
#' tcplPlot(fld = "m4id", val = c(18609966)) ## Create a level 4 plot
#' }
tcplPlot <- function(dat = NULL, type = "mc", fld = "m4id", val = NULL, compare.val = NULL, by = NULL, output = c("console", "pdf", "png", "jpg", "svg", "tiff"), fileprefix = paste0("tcplPlot_", Sys.Date()), multi = NULL, verbose = FALSE, nrow = NULL, ncol = NULL, dpi = 600, flags = FALSE, yuniform = FALSE, yrange=c(NA,NA)) {
tcplPlot <- function(dat = NULL, type = "mc", fld = "m4id", val = NULL, compare.val = NULL, by = NULL, output = c("ggplot", "console", "pdf", "png", "jpg", "svg", "tiff"), fileprefix = paste0("tcplPlot_", Sys.Date()), multi = NULL, verbose = FALSE, nrow = NULL, ncol = NULL, dpi = 600, flags = FALSE, yuniform = FALSE, yrange=c(NA,NA)) {
#variable binding
conc_unit <- bmd <- resp <- compare.dat <- lvl <- compare <- NULL

Expand Down Expand Up @@ -118,7 +118,7 @@ tcplPlot <- function(dat = NULL, type = "mc", fld = "m4id", val = NULL, compare.
nrow = ncol = 1
}
# error message for output="console" and multi=FALSE to avoid multiple plots in console
if(nrow(dat[compare == FALSE]) != 1 && output == "console" && multi == FALSE) stop("More than 1 concentration series returned for given field/val combination. Set output to pdf or reduce the number of curves to 1. Current number of curves: ", nrow(dat))
if(nrow(dat[compare == FALSE]) != 1 && output %in% c("ggplot", "console") && multi == FALSE) stop("More than 1 concentration series returned for given field/val combination. Set output to pdf or reduce the number of curves to 1. Current number of curves: ", nrow(dat[compare == FALSE]))
if(is.null(nrow)){
nrow <- ifelse(verbose,2,2)
}
Expand All @@ -130,15 +130,21 @@ tcplPlot <- function(dat = NULL, type = "mc", fld = "m4id", val = NULL, compare.


if (nrow(dat[compare == FALSE]) == 1) {
# plot single graph
# this needs to be fixed to be more succinct about users selected option
ifelse(output[1] == "console",
# tcplPlotlyplot is the user-defined function found in tcplPlot.R file used to connect tcpl and plotly packages
# tcplggplot is the user-defined function found in tcplPlot.R file used to connect tcpl and ggplot2 packages
return(tcplPlotlyPlot(dat, lvl)),
return(ggsave(filename=paste0(fileprefix,"_",paste0(ifelse(type=="mc",dat$m4id,dat$s2id), collapse = "_"),".",output),
plot= if(is.null(compare.val)) tcplggplot(dat,verbose = verbose, lvl = lvl, flags = flags, yrange = yrange) else tcplggplotCompare(dat[compare == FALSE],dat[compare == TRUE],verbose = verbose, lvl = lvl, flags = flags, yrange = yrange), width = 7, height = 5, dpi=dpi))
)
if (output[1] == "console") {
# tcplPlotlyplot is the user-defined function found in tcplPlot.R file used to connect tcpl and plotly packages
return(tcplPlotlyPlot(dat, lvl))
} else {
# tcplggplot is the user-defined function found in tcplPlot.R file used to connect tcpl and ggplot2 packages
ggplot <- if(is.null(compare.val)) tcplggplot(dat,verbose = verbose, lvl = lvl, flags = flags, yrange = yrange) else
tcplggplotCompare(dat[compare == FALSE],dat[compare == TRUE],verbose = verbose, lvl = lvl, flags = flags, yrange = yrange)
if (output[1] == "ggplot") {
if (verbose) message("ggplot object and verbose table arranged into gtable object. To work with a simple ggplot object, set `verbose = FALSE`.")
return(ggplot)
}
ggsave(filename = paste0(fileprefix, "_", paste0(ifelse(type=="mc", dat$m4id, dat$s2id), collapse = "_"), ".", output),
plot = ggplot, width = 7, height = 5, dpi=dpi)
return(ggplot)
}
} else {
split_dat <- list(dat)
if(!is.null(by)){
Expand Down Expand Up @@ -1447,7 +1453,7 @@ tcplggplotCompare <- function(dat, compare.dat, lvl = 5, verbose = FALSE, flags
t <- tableGrob(details, rows = c("A", "B"))
ifelse(verbose,
return(arrangeGrob(gg, t, nrow = 1, widths = 2:1)),
return(arrangeGrob(gg))
return(gg)
)
} else {
details <- tibble(Hitcall = c(dat$hitc, compare.dat$hitc))
Expand Down

0 comments on commit e78590f

Please sign in to comment.