-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
282 additions
and
49 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
#' Fit ggplot2 panel or plot in a viewport | ||
#' | ||
#' @param gg A [ggplot2][ggplot2::ggplot] object. | ||
#' @param align A string indicates how to align the `viewport`, "panel" or | ||
#' "plot". | ||
#' @param vp A [viewport][grid::viewport] object. | ||
#' @param sides Which side to draw besides the panel elements. If `NULL`, will | ||
#' draw panel only. | ||
#' @param elements Ggplot elements to draw, can be a list of a character to | ||
#' specify elements for each side separately. Valid elements are "axis", "lab", | ||
#' "guide". Other elements will be ignored. | ||
#' @param gt A [gtable][ggplot2::ggplotGrob] object. | ||
#' @return Draw ggplot object. | ||
#' - align = "panel": Draw ggplot object by fitting exactly the panel to `vp`. | ||
#' - align = "plot": Draw ggplot object in `vp`. | ||
#' @examples | ||
#' p <- ggplot(data.frame(x = 0:10, y = 0:10), aes(x, y)) + | ||
#' geom_point() | ||
#' outerBox <- viewport(width = unit(125, "mm"), height = unit(150, "mm")) | ||
#' innerBox <- viewport( | ||
#' x = unit(0.5, "npc"), y = unit(0.6, "npc"), | ||
#' width = unit(60, "mm"), height = unit(70, "mm"), angle = -30 | ||
#' ) | ||
#' | ||
#' # ggfit_panel ------------ | ||
#' grid.newpage() | ||
#' pushViewport(outerBox) | ||
#' grid.rect(gp = gpar(col = "red", fill = NA)) | ||
#' | ||
#' pushViewport(innerBox) | ||
#' grid.rect(gp = gpar(col = "red", fill = NA, lwd = 2)) | ||
#' ggfit(p, "panel") | ||
#' | ||
#' # ggfit_plot ------------- | ||
#' grid.newpage() | ||
#' pushViewport(outerBox) | ||
#' grid.rect(gp = gpar(col = "red", fill = NA)) | ||
#' | ||
#' pushViewport(innerBox) | ||
#' grid.rect(gp = gpar(col = "red", fill = NA, lwd = 2)) | ||
#' ggfit(p, "plot") | ||
#' @seealso | ||
#' - [ggfit_panel] | ||
#' - [ggfit_plot] | ||
#' @export | ||
ggfit <- function(gg, align = "panel", vp = NULL, | ||
sides = c("b", "t", "l", "r"), | ||
elements = c("axis", "lab", "guide"), | ||
gt = NULL) { | ||
align <- match.arg(align, c("panel", "plot")) | ||
if (align == "panel") { | ||
ggfit_panel(gg, vp, sides = sides, elements = elements, gt = gt) | ||
} else { | ||
ggfit_plot(gg, vp, sides = sides, elements = elements, gt = gt) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
#' Fit ggplot2 in a viewport | ||
#' @inheritParams ggfit | ||
#' @return Draw ggplot object in `vp`. | ||
#' @examples | ||
#' p <- ggplot(data.frame(x = 0:10, y = 0:10), aes(x, y)) + | ||
#' geom_point() | ||
#' outerBox <- viewport(width = unit(125, "mm"), height = unit(150, "mm")) | ||
#' innerBox <- viewport( | ||
#' x = unit(0.5, "npc"), y = unit(0.6, "npc"), | ||
#' width = unit(60, "mm"), height = unit(70, "mm"), angle = -30 | ||
#' ) | ||
#' | ||
#' grid.newpage() | ||
#' pushViewport(outerBox) | ||
#' grid.rect(gp = gpar(col = "red", fill = NA)) | ||
#' | ||
#' pushViewport(innerBox) | ||
#' grid.rect(gp = gpar(col = "red", fill = NA, lwd = 2)) | ||
#' ggfit_plot(p) | ||
#' @export | ||
ggfit_plot <- function(gg, vp = NULL, | ||
sides = c("b", "t", "l", "r"), | ||
elements = c("axis", "lab", "guide"), | ||
gt = NULL) { | ||
if (is.null(gt)) { | ||
stopifnot(ggplot2::is.ggplot(gg)) | ||
gt <- ggplot2::ggplotGrob(gg) | ||
} else { | ||
stopifnot(gtable::is.gtable(gt)) | ||
} | ||
if (!is.null(vp)) stopifnot(inherits(vp, "viewport")) | ||
stopifnot(all(sides %in% c("b", "t", "l", "r"))) | ||
if (!is.character(elements) && !is.list(elements)) { | ||
stop("elements must be a character or a list of character") | ||
} | ||
.ggfit_plot(gt, vp, sides, elements) | ||
} | ||
|
||
.ggfit_plot <- function(gt, vp = NULL, | ||
sides = c("b", "t", "l", "r"), | ||
elements = c("axis", "lab", "guide")) { | ||
if (is.null(vp)) vp <- grid::viewport() | ||
patterns <- c("panel") | ||
if (is.character(elements)) elements <- list(elements) | ||
elements <- rep_len(elements, length(sides)) | ||
for (i in seq_along(sides)) { | ||
s <- .subset(sides, i) | ||
side_element <- .subset2(elements, i) | ||
patterns <- c(patterns, ggpatterns(s, side_element)) | ||
} | ||
draw_grob(vp, gtable::gtable_filter(gt, paste(patterns, collapse = "|"))) | ||
} | ||
|
||
ggpatterns <- function(side, element) { | ||
if (any(element == "lab")) { | ||
lab <- paste("lab", side, sep = "-") | ||
} else { | ||
lab <- NULL | ||
} | ||
if (any(element == "axis")) { | ||
axis <- paste("axis", side, sep = "-") | ||
} else { | ||
axis <- NULL | ||
} | ||
if (any(element == "guide")) { | ||
guide <- paste("guide-box", switch(side, | ||
l = "left", | ||
r = "right", | ||
b = "bottom", | ||
t = "top" | ||
), sep = "-") | ||
} else { | ||
guide <- NULL | ||
} | ||
c(lab, axis, guide) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.