Skip to content

Commit

Permalink
Merge pull request #35 from hanneoberman/main
Browse files Browse the repository at this point in the history
Update vignette with more info about the `ggmice()` function, fix issues #32 and #33
  • Loading branch information
hanneoberman committed Mar 18, 2022
2 parents 1da51fc + c9621ef commit 25f918c
Show file tree
Hide file tree
Showing 10 changed files with 159 additions and 81 deletions.
6 changes: 4 additions & 2 deletions R/corr.R
Expand Up @@ -32,7 +32,7 @@ plot_corr <- function(data, vrb = "all", label = FALSE, square = TRUE, diagonal
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(low = ggplot2::alpha("deepskyblue", 0.6), mid = "lightyellow", high = ggplot2::alpha("orangered", 0.6), na.value = "white", limits = c(-1, 1)) +
ggplot2::scale_fill_gradient2(low = ggplot2::alpha("deepskyblue", 0.6), mid = "lightyellow", high = ggplot2::alpha("orangered", 0.6), na.value = "grey90", limits = c(-1, 1)) +
ggplot2::labs(
x = "Imputation model predictor",
y = "Variable to impute",
Expand All @@ -45,7 +45,9 @@ plot_corr <- function(data, vrb = "all", label = FALSE, square = TRUE, diagonal
gg <- gg + ggplot2::geom_text(color = "black", show.legend = FALSE, na.rm = TRUE)
}
if (square) {
gg <- gg + ggplot2::coord_fixed()
gg <- gg + ggplot2::coord_fixed(expand = FALSE)
} else {
gg <- gg + ggplot2::coord_cartesian(expand = FALSE)
}
if (rotate) {
gg <- gg + ggplot2::theme(axis.text.x.top = ggplot2::element_text(angle = 90))
Expand Down
2 changes: 1 addition & 1 deletion R/ggmice.R
@@ -1,6 +1,6 @@
#' Plot incomplete or imputed data
#'
#' @param data An incomplete dataset (of class `data.frame` or `matrix`), or an object of class [`mice::mids`].
#' @param data An incomplete dataset (of class `data.frame`), or an object of class [`mice::mids`].
#' @param mapping A list of aesthetic mappings created with [ggplot2::aes()].
#'
#' @return An object of class [`ggplot2::ggplot`].
Expand Down
4 changes: 3 additions & 1 deletion R/pattern.R
Expand Up @@ -87,7 +87,9 @@ plot_pattern <- function(data, vrb = "all", square = FALSE, rotate = FALSE, clus
) +
theme_minimice()
if (square) {
gg <- gg + ggplot2::coord_fixed()
gg <- gg + ggplot2::coord_fixed(expand = FALSE)
} else {
gg <- gg + ggplot2::coord_cartesian(expand = FALSE)
}
if (rotate) {
gg <- gg + ggplot2::theme(axis.text.x = ggplot2::element_text(angle = 90))
Expand Down
12 changes: 7 additions & 5 deletions R/pred.R
Expand Up @@ -8,10 +8,10 @@
#' @return An object of class `ggplot2::ggplot`.
#'
#' @examples
#' pred <- mice::make.predictorMatrix(mice::nhanes)
#' plot_pred(pred, label = TRUE)
#' pred <- mice::quickpred(mice::nhanes)
#' plot_pred(pred)
#' @export
plot_pred <- function(data, label = FALSE, square = TRUE, rotate = FALSE) {
plot_pred <- function(data, label = TRUE, square = TRUE, rotate = FALSE) {
if (!is.matrix(data) | dim(data)[1] != dim(data)[2]) {
stop("Predictor matrix should be a square matrix, try using mice::make.predictorMatrix() or mice::quickpred().")
}
Expand All @@ -26,7 +26,7 @@ plot_pred <- function(data, label = FALSE, square = TRUE, rotate = FALSE) {
ggplot2::geom_tile(color = "black", alpha = 0.6) +
ggplot2::scale_x_discrete(limits = vrbs, position = "top") +
ggplot2::scale_y_discrete(limits = rev(vrbs)) +
ggplot2::scale_fill_manual(values = c("yes" = "grey75", "no" = "white")) + ## 006CC2B3
ggplot2::scale_fill_manual(values = c("yes" = "grey50", "no" = "grey90")) + ## 006CC2B3
ggplot2::labs(
x = "Imputation model predictor",
y = "Variable to impute",
Expand All @@ -38,7 +38,9 @@ plot_pred <- function(data, label = FALSE, square = TRUE, rotate = FALSE) {
gg <- gg + ggplot2::geom_text(color = "black", show.legend = FALSE)
}
if (square) {
gg <- gg + ggplot2::coord_fixed()
gg <- gg + ggplot2::coord_fixed(expand = FALSE)
} else {
gg <- gg + ggplot2::coord_cartesian(expand = FALSE)
}
if (rotate) {
gg <- gg + ggplot2::theme(axis.text.x.top = ggplot2::element_text(angle = 90))
Expand Down
3 changes: 2 additions & 1 deletion R/theme.R
Expand Up @@ -25,7 +25,8 @@ theme_minimice <- function() {
legend.justification = "right",
strip.placement = "outside",
panel.grid.minor = ggplot2::element_blank(),
panel.grid.major = ggplot2::element_line(colour = "grey95"),
panel.grid.major = ggplot2::element_blank(),
axis.ticks = ggplot2::element_line(colour = "black"),
axis.title.y.right = ggplot2::element_text(margin = ggplot2::margin(l = 6))
)
}
Expand Down
2 changes: 1 addition & 1 deletion README.Rmd
Expand Up @@ -30,7 +30,7 @@ Enhance a `mice` imputation workflow with visualizations for incomplete and/or i

## Installation

You can install the latest `ggmice` release from [CRAN](https://cran.r-project.org/) with:
You can install the latest `ggmice` release from [CRAN](https://CRAN.R-project.org/package=ggmice) with:

``` r
install.packages("ggmice")
Expand Down
2 changes: 1 addition & 1 deletion README.md
Expand Up @@ -26,7 +26,7 @@ convergence, or compare observed versus imputed data.
## Installation

You can install the latest `ggmice` release from
[CRAN](https://cran.r-project.org/) with:
[CRAN](https://CRAN.R-project.org/package=ggmice) with:

``` r
install.packages("ggmice")
Expand Down
2 changes: 1 addition & 1 deletion man/ggmice.Rd

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

6 changes: 3 additions & 3 deletions man/plot_pred.Rd

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

0 comments on commit 25f918c

Please sign in to comment.