Skip to content
Draft
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
10 changes: 8 additions & 2 deletions R/plot_corr.R
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#' @param diagonal Logical indicating whether the correlation of each variable with itself should be displayed.
#' @param rotate Logical indicating whether the variable name labels should be rotated 90 degrees.
#' @param caption Logical indicating whether the figure caption should be displayed.
#' @param grid Logical indicating whether borders should be present between tiles.
#'
#' @return An object of class [ggplot2::ggplot].
#'
Expand All @@ -20,7 +21,8 @@ plot_corr <-
square = TRUE,
diagonal = FALSE,
rotate = FALSE,
caption = TRUE) {
caption = TRUE,
grid = TRUE) {
if (is.matrix(data) && ncol(data) > 1) {
data <- as.data.frame(data)
}
Expand Down Expand Up @@ -73,7 +75,6 @@ plot_corr <-
label = .data$corr,
fill = .data$corr
)) +
ggplot2::geom_tile(color = "black", alpha = 0.6) +
ggplot2::scale_x_discrete(limits = vrb, position = "top") +
ggplot2::scale_y_discrete(limits = rev(vrb)) +
ggplot2::scale_fill_gradient2(
Expand All @@ -84,6 +85,11 @@ plot_corr <-
limits = c(-1, 1)
) +
theme_minimice()
if (grid) {
gg <- gg + ggplot2::geom_tile(color = "black", alpha = 0.6)
} else {
gg <- gg + ggplot2::geom_tile(alpha = 0.6)
}
if (caption) {
gg <- gg +
ggplot2::labs(
Expand Down
11 changes: 8 additions & 3 deletions R/plot_pattern.R
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#' @param cluster Optional character string specifying which variable should be used for clustering (e.g., for multilevel data).
#' @param npat Optional numeric input specifying the number of missing data patterns to be visualized, defaults to all patterns.
#' @param caption Logical indicating whether the figure caption should be displayed.
#' @param grid Logical indicating whether borders should be present between tiles.
#'
#' @return An object of class [ggplot2::ggplot].
#'
Expand All @@ -20,7 +21,8 @@ plot_pattern <-
rotate = FALSE,
cluster = NULL,
npat = NULL,
caption = TRUE) {
caption = TRUE,
grid = TRUE) {
# input processing
if (is.matrix(data) && ncol(data) > 1) {
data <- as.data.frame(data)
Expand Down Expand Up @@ -150,7 +152,6 @@ plot_pattern <-
alpha = 0.1 + .data$.opacity / 2
)
) +
ggplot2::geom_tile(color = "black") +
ggplot2::scale_fill_manual(values = c(
"observed" = "#006CC2B3",
"missing" = "#B61A51B3"
Expand All @@ -177,6 +178,11 @@ plot_pattern <-
theme_minimice()

# additional arguments
if (grid) {
gg <- gg + ggplot2::geom_tile(color = "black")
} else {
gg <- gg + ggplot2::geom_raster()
}
if (square) {
gg <- gg + ggplot2::coord_fixed(expand = FALSE)
} else {
Expand Down Expand Up @@ -209,7 +215,6 @@ plot_pattern <-
pat[rws, cls]))
}
}

return(gg)
}

Expand Down
11 changes: 8 additions & 3 deletions R/plot_pred.R
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#' @param label Logical indicating whether predictor matrix values should be displayed.
#' @param square Logical indicating whether the plot tiles should be squares.
#' @param rotate Logical indicating whether the variable name labels should be rotated 90 degrees.
#' @param grid Logical indicating whether borders should be present between tiles.
#'
#' @return An object of class `ggplot2::ggplot`.
#'
Expand All @@ -19,7 +20,8 @@ plot_pred <-
method = NULL,
label = TRUE,
square = TRUE,
rotate = FALSE) {
rotate = FALSE,
grid = TRUE) {
verify_data(data, pred = TRUE)
p <- nrow(data)
if (!is.null(method) && is.character(method)) {
Expand Down Expand Up @@ -69,7 +71,6 @@ plot_pred <-
label = .data$ind,
fill = .data$clr
)) +
ggplot2::geom_tile(color = "black", alpha = 0.6) +
ggplot2::scale_x_discrete(limits = vrbs, position = "top") +
ggplot2::scale_y_reverse(
breaks = 1:p,
Expand All @@ -92,7 +93,11 @@ plot_pred <-
color = ""
) +
theme_minimice()

if (grid) {
gg <- gg + ggplot2::geom_tile(color = "black", alpha = 0.6)
} else {
gg <- gg + ggplot2::geom_tile(alpha = 0.6)
}
if (all(method == "")) {
gg <-
gg + ggplot2::theme(axis.ticks.y.right = ggplot2::element_blank())
Expand Down
5 changes: 4 additions & 1 deletion man/plot_corr.Rd

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

5 changes: 4 additions & 1 deletion man/plot_pattern.Rd

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

5 changes: 4 additions & 1 deletion man/plot_pred.Rd

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

3 changes: 2 additions & 1 deletion tests/testthat/test-plot_corr.R
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ test_that("plot_corr creates ggplot object", {
label = TRUE,
square = FALSE,
diagonal = TRUE,
rotate = TRUE
rotate = TRUE,
grid = FALSE
),
"ggplot")
})
Expand Down
2 changes: 1 addition & 1 deletion tests/testthat/test-plot_pattern.R
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ dat <- mice::nhanes
# tests
test_that("plot_pattern produces plot", {
expect_s3_class(plot_pattern(dat), "ggplot")
expect_s3_class(plot_pattern(dat, square = FALSE, rotate = TRUE, cluster = "age", npat = 2), "ggplot")
expect_s3_class(plot_pattern(dat, square = FALSE, rotate = TRUE, cluster = "age", grid = FALSE, npat = 2), "ggplot")
expect_s3_class(plot_pattern(cbind(dat, "testvar" = NA), caption = FALSE), "ggplot")
})

Expand Down
3 changes: 2 additions & 1 deletion tests/testthat/test-plot_pred.R
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ test_that("plot_pred creates ggplot object", {
method = c("pmm"),
label = FALSE,
square = FALSE,
rotate = TRUE
rotate = TRUE,
grid = FALSE
),
"ggplot")
expect_s3_class(plot_pred(pred, vrb = c("age", "bmi")), "ggplot")
Expand Down