Skip to content
Open
Show file tree
Hide file tree
Changes from all 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
5 changes: 2 additions & 3 deletions DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Package: endpoints
Title: Simulate Correlated Multi-Endpoint Clinical Trial Data
Version: 0.1.1
Version: 0.1.2
Authors@R:
c(person(given = "Colin",
family = "Longhurst",
Expand Down Expand Up @@ -28,13 +28,12 @@ Encoding: UTF-8
Roxygen: list(markdown = TRUE)
RoxygenNote: 7.3.3
Imports:
ggplot2,
GGally,
Matrix,
MASS,
stats,
survival
Suggests:
ggplot2,
tidyr,
geeM,
lme4,
Expand Down
74 changes: 41 additions & 33 deletions R/utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -1418,10 +1418,11 @@ print.summary.makeDataSim <- function(x, ...) {
#' endpoints stored in a \code{"makeDataSim"} object returned by
#' \code{\link{makeData}}.
#'
#' The method uses \code{GGally::ggpairs()} to display scatterplots in the
#' upper triangle and pairwise correlations in the lower triangle for the
#' selected study arm. This is intended as a quick diagnostic tool for
#' inspecting the joint structure of simulated endpoints.
#' The method uses \code{\link[graphics]{pairs}()} to display scatterplots
#' in the upper triangle and Pearson correlation
#' coefficients in the lower triangle for the selected study arm. This is
#' intended as a quick diagnostic tool for inspecting the joint structure of
#' simulated endpoints.
#'
#'
#' @param x An object of class \code{"makeDataSim"}, created by
Expand All @@ -1441,11 +1442,9 @@ print.summary.makeDataSim <- function(x, ...) {
#' endpoints. This affects only the displayed labels in the plot and does not
#' modify the underlying data.
#' @param \dots Additional arguments passed directly to
#' \code{GGally::ggpairs()}. This can be used to customize panels, labels,
#' sizing, or other plot options.
#' @return A \code{GGally::ggpairs} object, which is also a
#' \code{ggplot}-compatible object and can be printed or further modified using
#' standard \pkg{ggplot2} syntax.
#' \code{\link[graphics]{pairs}()}. This can be used to customize panels,
#' labels, sizing, or other plot options.
#' @return A base-graphics plot. Returns \code{invisible(NULL)}.
#' @section Displayed data:
#'
#' The plotting method extracts the simulated endpoint columns recorded in
Expand All @@ -1456,14 +1455,11 @@ print.summary.makeDataSim <- function(x, ...) {
#' \item{Time-to-event endpoints}{\code{TTE_1}, \code{TTE_2}, \dots} } Columns
#' such as \code{trt}, \code{Status_*}, and \code{enrollTime} are not included
#' in the pair plot.
#' @section Panel layout: The returned \code{ggpairs} object is configured as
#' follows: \itemize{ \item upper triangle: scatterplots for continuous-style
#' panels, \item lower triangle: pairwise correlations, \item title: \code{"Arm
#' <k>"} for the selected arm. } A \code{ggplot2::theme_bw()} theme is applied
#' by default.
#' @section Dependencies: This method requires both \pkg{GGally} and
#' \pkg{ggplot2}. If either package is not installed, the method throws an
#' error with an installation message.
#' @section Panel layout: The plot matrix is configured as follows: \itemize{
#' \item upper triangle: scatterplots, \item diagonal: histograms showing
#' the marginal distribution of each endpoint, \item lower triangle: Pearson
#' correlation coefficients, \item title: \code{"Arm <k>"} for the selected
#' arm. }
#' @seealso \code{\link[=summary.makeDataSim]{summary.makeDataSim}} for
#' numerical summaries of the simulated data.
#' @examples
Expand Down Expand Up @@ -1517,13 +1513,6 @@ plot.makeDataSim <- function(x,
names = NULL,
...) {

if (!requireNamespace("GGally", quietly = TRUE)) {
stop("Package 'GGally' is required for plot.makeDataSim(). Please install it.")
}
if (!requireNamespace("ggplot2", quietly = TRUE)) {
stop("Package 'ggplot2' is required for plot.makeDataSim(). Please install it.")
}

dat <- x$data
meta <- x$meta

Expand Down Expand Up @@ -1553,17 +1542,36 @@ plot.makeDataSim <- function(x,
colnames(dat_sub) <- names
}

p <- GGally::ggpairs(
panel_cor <- function(x, y, ...) {
r <- round(stats::cor(x, y, use = "pairwise.complete.obs"), 3)
usr <- graphics::par("usr")
graphics::text(
x = (usr[1] + usr[2]) / 2,
y = (usr[3] + usr[4]) / 2,
labels = paste0("Corr: ", r),
cex = 1.2
)
}

panel_hist <- function(x, ...) {
usr <- graphics::par("usr")
on.exit(graphics::par(usr = usr))
graphics::par(usr = c(usr[1:2], 0, 1.5))
h <- graphics::hist(x, plot = FALSE)
breaks <- h$breaks
n_breaks <- length(breaks)
y <- h$counts / max(h$counts)
graphics::rect(breaks[-n_breaks], 0, breaks[-1], y, col = "grey80", border = "white")
}

graphics::pairs(
dat_sub,
upper = list(continuous = "points"),
lower = list(continuous = "cor"),
progress = FALSE,
main = paste0("Arm ", arm_lbl),
upper.panel = graphics::points,
lower.panel = panel_cor,
diag.panel = panel_hist,
...
) +
ggplot2::theme_bw() +
ggplot2::ggtitle(paste0("Arm ", arm_lbl))

p
)
}


Expand Down
34 changes: 14 additions & 20 deletions man/plot.makeDataSim.Rd

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

Loading