Skip to content

Commit ec86580

Browse files
author
Yunuuuu
committed
use list to construct inset instead of grob
1 parent 8c1e28f commit ec86580

2 files changed

Lines changed: 39 additions & 32 deletions

File tree

R/alignpatch-inset.R

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,10 @@
1515
#' @export
1616
inset <- function(plot, ..., align = "panel", on_top = TRUE,
1717
clip = TRUE, vp = NULL) {
18-
grob <- make_inset(
18+
make_inset(
1919
plot = plot, ..., align = align, on_top = on_top,
2020
clip = clip, vp = vp
2121
)
22-
add_class(grob, "patch_inset")
2322
}
2423

2524
#' @importFrom grid editGrob
@@ -33,11 +32,16 @@ make_inset <- function(plot, ..., align, on_top, clip, vp,
3332
if (!is.grob(grob <- patch(x = plot, ...))) {
3433
cli_abort("{.fn patch} must return a {.cls grob}", call = call)
3534
}
36-
attr(grob, "align") <- align
37-
attr(grob, "clip") <- if (clip) "on" else "off"
38-
attr(grob, "on_top") <- on_top
3935
if (!is.null(vp)) grob <- editGrob(grob, vp = vp)
40-
grob
36+
structure(
37+
list(
38+
grob = grob,
39+
align = align,
40+
clip = if (clip) "on" else "off",
41+
on_top = on_top
42+
),
43+
class = "patch_inset"
44+
)
4145
}
4246

4347
#' @importFrom ggplot2 ggplot_add

R/alignpatch-wrap.R

Lines changed: 29 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -46,45 +46,45 @@ ggwrap <- function(plot, ..., align = "panel", on_top = FALSE,
4646
plot.background = element_blank(),
4747
panel.background = element_blank()
4848
)
49-
grob <- make_inset(
49+
inset <- make_inset(
5050
plot = plot, ..., align = align, on_top = on_top,
5151
clip = clip, vp = vp
5252
)
53-
make_wrap(patch, grob)
53+
make_wrap(patch, inset)
5454
}
5555

56-
make_wrap <- function(patch, grob) UseMethod("make_wrap")
56+
make_wrap <- function(patch, inset) UseMethod("make_wrap")
5757

5858
#' @export
59-
make_wrap.patch_ggplot <- function(patch, grob) {
59+
make_wrap.patch_ggplot <- function(patch, inset) {
6060
patch <- add_class(patch, "wrapped_plot")
61-
make_wrap(patch, grob)
61+
make_wrap(patch, inset)
6262
}
6363

6464
#' @export
65-
make_wrap.ggplot <- function(patch, grob) {
65+
make_wrap.ggplot <- function(patch, inset) {
6666
patch <- add_class(patch, "patch_ggplot")
67-
make_wrap(patch, grob)
67+
make_wrap(patch, inset)
6868
}
6969

7070
#' @export
71-
make_wrap.wrapped_plot <- function(patch, grob) {
72-
if (attr(grob, "on_top")) {
73-
attr(patch, "wrapped_grobs_above") <- c(
74-
attr(patch, "wrapped_grobs_above"), list(grob)
71+
make_wrap.wrapped_plot <- function(patch, inset) {
72+
if (.subset2(inset, "on_top")) {
73+
patch$ggalign_wrapped_insets_above <- c(
74+
patch$ggalign_wrapped_insets_above, list(inset)
7575
)
7676
} else {
77-
attr(patch, "wrapped_grobs_under") <- c(
78-
attr(patch, "wrapped_grobs_under"), list(grob)
77+
patch$ggalign_wrapped_insets_under <- c(
78+
patch$ggalign_wrapped_insets_under, list(inset)
7979
)
8080
}
8181
patch
8282
}
8383

8484
#' @export
85-
make_wrap.alignpatches <- function(patch, grob) {
85+
make_wrap.alignpatches <- function(patch, inset) {
8686
patch <- add_class(patch, "wrapped_plot")
87-
make_wrap(patch, grob)
87+
make_wrap(patch, inset)
8888
}
8989

9090
#################################################
@@ -94,12 +94,14 @@ alignpatch.wrapped_plot <- function(x) {
9494
Parent <- NextMethod()
9595
ggproto(
9696
"PatchWrapped", Parent,
97-
wrapped_grobs_under = attr(x, "wrapped_grobs_under"),
98-
wrapped_grobs_above = attr(x, "wrapped_grobs_above"),
97+
ggalign_wrapped_insets_under = x$ggalign_wrapped_insets_under,
98+
ggalign_wrapped_insets_above = x$ggalign_wrapped_insets_above,
9999
patch_gtable = function(self, plot = Parent$plot) {
100100
ans <- ggproto_parent(Parent, self)$patch_gtable(plot = plot)
101-
ans <- add_wrapped_grobs(ans, self$wrapped_grobs_under, FALSE)
102-
add_wrapped_grobs(ans, self$wrapped_grobs_above, TRUE)
101+
ans <- add_wrapped_insets(
102+
ans, self$ggalign_wrapped_insets_under, FALSE
103+
)
104+
add_wrapped_insets(ans, self$ggalign_wrapped_insets_above, TRUE)
103105
}
104106
)
105107
}
@@ -136,19 +138,20 @@ alignpatch.HeatmapAnnotation <- alignpatch.Heatmap
136138
alignpatch.pheatmap <- function(x) alignpatch(ggwrap(x, align = "full"))
137139

138140
################################################## 3
139-
add_wrapped_grobs <- function(gt, grobs, on_top) {
140-
if (is.null(grobs)) return(gt) # styler: off
141-
for (i in seq_along(grobs)) {
142-
gt <- add_wrapped_grob(gt, .subset2(grobs, i), on_top, i)
141+
add_wrapped_insets <- function(gt, insets, on_top) {
142+
if (is.null(insets)) return(gt) # styler: off
143+
for (i in seq_along(insets)) {
144+
gt <- add_wrapped_inset(gt, .subset2(insets, i), on_top, i)
143145
}
144146
gt
145147
}
146148

147149
#' @importFrom gtable gtable is.gtable gtable_add_grob
148-
add_wrapped_grob <- function(gt, grob, on_top, i) {
149-
align <- attr(grob, "align")
150-
clip <- attr(grob, "clip")
150+
add_wrapped_inset <- function(gt, inset, on_top, i) {
151+
align <- .subset2(inset, "align")
152+
clip <- .subset2(inset, "clip")
151153
layout <- .subset2(gt, "layout")
154+
grob <- .subset2(inset, "grob")
152155
if (on_top) {
153156
z <- Inf
154157
} else {

0 commit comments

Comments
 (0)