From eea00276117615a828f1f9c5a5a922e961da9892 Mon Sep 17 00:00:00 2001 From: Daniel Date: Sun, 4 Aug 2024 00:37:18 +0200 Subject: [PATCH] tweak plot --- DESCRIPTION | 2 +- R/plot.check_dag.R | 69 +++++++++++++++++++++------------------ man/plot.see_check_dag.Rd | 5 +-- 3 files changed, 42 insertions(+), 34 deletions(-) diff --git a/DESCRIPTION b/DESCRIPTION index 970146aa1..f58bf5ef3 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -1,7 +1,7 @@ Type: Package Package: see Title: Model Visualisation Toolbox for 'easystats' and 'ggplot2' -Version: 0.8.5.2 +Version: 0.8.5.3 Authors@R: c(person(given = "Daniel", family = "Lüdecke", diff --git a/R/plot.check_dag.R b/R/plot.check_dag.R index 2e438ca82..782ac70a7 100644 --- a/R/plot.check_dag.R +++ b/R/plot.check_dag.R @@ -4,6 +4,7 @@ #' #' @param x A `check_dag` object. #' @param size_point Numeric value specifying size of point geoms. +#' @param size_point Numeric value specifying size of text elements. #' @param colors Character vector of length five, indicating the colors (in #' hex-format) for different types of variables. #' @param which Character string indicating which plot to show. Can be either @@ -42,7 +43,8 @@ #' plot(dag) #' @export plot.see_check_dag <- function(x, - size_point = 15, + size_point = 20, + size_text = 4.5, colors = NULL, which = "all", check_colliders = TRUE, @@ -87,45 +89,50 @@ plot.see_check_dag <- function(x, } names(point_colors) <- c("outcome", "exposure", "adjusted", "unadjusted", "collider") - plot1 <- ggplot2::ggplot(p1$data, ggplot2::aes(x = .data$x, y = .data$y)) + - geom_point_borderless(ggplot2::aes(fill = .data$type), size = size_point) + + # these geoms are shared by both plots + common_layers <- list( + geom_point_borderless( + ggplot2::aes(fill = .data$type), + size = size_point + ), ggdag::geom_dag_edges( ggplot2::aes( xend = .data$xend, yend = .data$yend, edge_alpha = .data$adjusted ) - ) + - ggdag::scale_adjusted() + - ggdag::geom_dag_label(ggplot2::aes(label = .data$name)) + - ggdag::theme_dag() + - ggplot2::scale_fill_manual(values = point_colors) + - ggplot2::ggtitle("Current model") + - ggplot2::guides(edge_alpha = "none") + + ), + ggdag::scale_adjusted(), + ggdag::geom_dag_label( + ggplot2::aes(label = .data$name, color = .data$type), + size = size_text + ), + ggdag::theme_dag( + legend.text = ggplot2::element_text(size = 2.75 * size_text), + legend.position = "bottom", + legend.justification = c(0, 1) + ), + ggplot2::scale_fill_manual(values = point_colors), + ggplot2::guides( + edge_alpha = "none", + fill = ggplot2::guide_legend(override.aes = list(size = size_point / 4)) + ), ggdag::expand_plot( - expand_x = ggplot2::expansion(c(0.05, 0.05)), - expand_y = ggplot2::expansion(c(0.05, 0.05)) - ) + expand_x = ggplot2::expansion(c(0.2, 0.2)), + expand_y = ggplot2::expansion(c(0.2, 0.2)) + ), + ggplot2::labs(fill = NULL) + ) + + # plot1 - current model + plot1 <- ggplot2::ggplot(p1$data, ggplot2::aes(x = .data$x, y = .data$y)) + + common_layers + + ggplot2::ggtitle("Current model") + # plot2 - required model plot2 <- ggplot2::ggplot(p2$data, ggplot2::aes(x = .data$x, y = .data$y)) + - geom_point_borderless(ggplot2::aes(fill = .data$type), size = size_point) + - ggdag::geom_dag_edges( - ggplot2::aes( - xend = .data$xend, - yend = .data$yend, - edge_alpha = .data$adjusted - ) - ) + - ggdag::scale_adjusted() + - ggdag::geom_dag_label(ggplot2::aes(label = .data$name)) + - ggdag::theme_dag() + - ggplot2::scale_fill_manual(values = point_colors) + - ggplot2::ggtitle("Required model") + - ggplot2::guides(edge_alpha = "none") + - ggdag::expand_plot( - expand_x = ggplot2::expansion(c(0.05, 0.05)), - expand_y = ggplot2::expansion(c(0.05, 0.05)) - ) + common_layers + + ggplot2::ggtitle("Required model") if (which == "all") { # fix legends diff --git a/man/plot.see_check_dag.Rd b/man/plot.see_check_dag.Rd index f1b00acd4..2366094d5 100644 --- a/man/plot.see_check_dag.Rd +++ b/man/plot.see_check_dag.Rd @@ -6,7 +6,8 @@ \usage{ \method{plot}{see_check_dag}( x, - size_point = 15, + size_point = 20, + size_text = 4.5, colors = NULL, which = "all", check_colliders = TRUE, @@ -16,7 +17,7 @@ \arguments{ \item{x}{A \code{check_dag} object.} -\item{size_point}{Numeric value specifying size of point geoms.} +\item{size_point}{Numeric value specifying size of text elements.} \item{colors}{Character vector of length five, indicating the colors (in hex-format) for different types of variables.}