R/combine_groupBy.R

Defines functions combine_groupBy

Documented in combine_groupBy

#' Combine Group Plots
#'
#' Combines network, centrality, and bridge plots into a labeled panel.
#'
#' @param red_group Network graph object (ggplot, grob, or magick-image).
#' @param plot_centralidad_group ggplot centrality plot.
#' @param bridge_plot_group Optional ggplot bridge centrality plot.
#' @param width_a Width of network panel.
#' @param width_bc Width of centrality panels.
#' @param show_plot Logical; display the combined plot.
#'
#' @return A ggplot object with the network (panel A), the centrality plot
#'   (panel B) and, when supplied, the bridge centrality plot (panel C).
#' @export
#' @examples
#' set.seed(123)
#' n <- 300
#' f1 <- rnorm(n)
#' f2 <- 0.3 * f1 + rnorm(n)
#' items <- data.frame(
#'   sapply(1:4, function(i) 0.7 * f1 + rnorm(n, 0, 0.7)),
#'   sapply(1:4, function(i) 0.7 * f2 + rnorm(n, 0, 0.7))
#' )
#' names(items) <- c(paste0("A", 1:4), paste0("B", 1:4))
#'
#' net <- bootnet::estimateNetwork(items, default = "EBICglasso")
#' g <- qgraph::qgraph(net$graph, DoNotPlot = TRUE)
#' r2 <- mgm_error_metrics(items, type = rep("g", 8), level = rep(1, 8))$R2
#' net_plot <- plot_net(net$graph, r2 = r2, groups_list = list(A = 1:4, B = 5:8))
#' cent <- centrality_plots2(g, net)
#' bridge <- centrality_cleveland_plot(g, net, groups = rep(c("A", "B"), each = 4))
#' combine_groupBy(net_plot, cent$plot, bridge$plot, show_plot = FALSE)
#' @importFrom ggplot2 ggplot theme theme_void margin
#' @importFrom gridExtra arrangeGrob
#' @importFrom grid unit gTree gList viewport rasterGrob
#' @importFrom cowplot ggdraw draw_label
#' @importFrom ggplotify as.grob as.ggplot
#' @importFrom magick image_write
#' @importFrom png readPNG
combine_groupBy <- function(
    red_group,
    plot_centralidad_group,
    bridge_plot_group = NULL,
    width_a = 8,
    width_bc = 4.5,
    show_plot = TRUE
) {
  # -- 1) Procesar 'red_group' segun su tipo ----------------------------------
  # Si es una magick-image, leerla a rasterGrob; si es ggplot, convertir a grob;
  # si ya es grob/gtable, usarla directamente.
  if (inherits(red_group, "magick-image")) {
    temp_file <- tempfile(fileext = ".png")
    magick::image_write(red_group, path = temp_file)
    png_img   <- png::readPNG(temp_file)
    red_grob  <- grid::rasterGrob(png_img, interpolate = TRUE)
    unlink(temp_file)

  } else if (inherits(red_group, "ggplot")) {
    red_grob <- ggplotify::as.grob(red_group)

  } else if (
    inherits(red_group, "gtable") ||
    inherits(red_group, "grob") ||
    inherits(red_group, "zeroGrob")
  ) {
    red_grob <- red_group

  } else {
    stop("`red_group` debe ser un ggplot, un grob (grid) o una magick-image.")
  }

  # -- 2) Crear etiquetas A, B y C --------------------------------------------
  label_a <- cowplot::ggdraw() +
    cowplot::draw_label("A", fontface = "bold", size = 14, x = 0.02, hjust = 0) +
    theme_void()
  label_b <- cowplot::ggdraw() +
    cowplot::draw_label("B", fontface = "bold", size = 14, x = 0.02, hjust = 0) +
    theme_void()
  label_c <- cowplot::ggdraw() +
    cowplot::draw_label("C", fontface = "bold", size = 14, x = 0.02, hjust = 0) +
    theme_void()

  # -- 3) Preparar los subplots de centralidad (B) y bridge (C) ---------------
  b_plot <- cowplot::ggdraw(plot_centralidad_group) +
    theme(plot.margin = grid::unit(c(5, 5, 5, 5), "pt"))

  if (!is.null(bridge_plot_group)) {
    c_plot <- cowplot::ggdraw(bridge_plot_group) +
      theme(plot.margin = grid::unit(c(5, 5, 5, 5), "pt"))
  } else {
    c_plot <- cowplot::ggdraw() + theme_void()
  }

  # -- 4) Construir la "columna derecha" (solo B, o B + C) ---------------------
  if (is.null(bridge_plot_group)) {
    right_col_grob <- gridExtra::arrangeGrob(
      gridExtra::arrangeGrob(
        label_b,
        b_plot,
        ncol    = 1,
        heights = c(0.1, 0.9)
      ),
      ncol = 1
    )
    rel_widths <- c(width_a, width_bc)

  } else {
    right_col_grob <- gridExtra::arrangeGrob(
      gridExtra::arrangeGrob(
        label_b,
        b_plot,
        ncol    = 1,
        heights = c(0.1, 0.9)
      ),
      gridExtra::arrangeGrob(
        label_c,
        c_plot,
        ncol    = 1,
        heights = c(0.1, 0.9)
      ),
      ncol    = 1,
      heights = c(0.5, 0.5)
    )
    rel_widths <- c(width_a, width_bc)
  }

  # -- 5) Construir la "columna izquierda" (A + red_grob escalado) -------------
  # Para forzar que red_grob ocupe el 99% de la celda, lo envolvemos en un gTree
  # con un viewport que le da casi todo el espacio (0.99 "npc").
  raster_vp <- grid::gTree(
    children = grid::gList(red_grob),
    vp = grid::viewport(
      width  = grid::unit(0.99, "npc"),  # 99% del ancho de la celda
      height = grid::unit(0.99, "npc"),  # 99% de la altura de la celda
      just   = "centre"
    )
  )

  left_col_grob <- gridExtra::arrangeGrob(
    label_a,
    raster_vp,
    ncol    = 1,
    heights = c(0.05, 0.95)
  )

  # -- 6) Unir izquierda y derecha en un solo gtable --------------------------
  combined_grob <- gridExtra::arrangeGrob(
    left_col_grob,
    right_col_grob,
    ncol   = 2,
    widths = rel_widths
  )

  # -- 7) Convertir el gtable final a un objeto ggplot2 -----------------------
  combined_gg <- ggplotify::as.ggplot(combined_grob)

  # -- 8) Mostrar en pantalla si show_plot = TRUE -----------------------------
  if (isTRUE(show_plot)) {
    print(combined_gg)
  }

  # -- 9) Devolver el objeto ggplot2 listo para ggsave() ----------------------
  return(combined_gg)
}

Try the InterconectaR package in your browser

Any scripts or data that you put into this service are public.

InterconectaR documentation built on Oct. 16, 2026, 1:06 a.m.