moveBox: Move a boxGrob

View source: R/boxGrobs_move.R View source: R/boxGrobs_move.R

moveBoxR Documentation

Move a boxGrob

Description

Moves a boxGrob/boxPropGrob by modifying it's viewport. This can be useful if you want to create a series of boxes whose position are relative to each other and depend on each box's width/height.

Usage

moveBox(
  element,
  x = NULL,
  y = NULL,
  space = c("absolute", "relative"),
  just = NULL,
  subelement = NULL
)

Arguments

element

A boxGrob/boxPropGrob object.

x

A unit element or a numeric that can be converted to a npc unit object.

y

A unit element or a numeric that can be converted to a npc unit object.

space

We can provide absolute that confers the box absolute position within the parent viewport. If relative the movement is related to the current position.

just

The justification of an argument as used by viewport some tiny differences: (1) you only want to change the justification in the vertical direction you can retain the existing justification by using NA, e.g. c(NA, 'top'), (2) if you specify only one string and that string is either top or bottom it will assume vertical justification.

subelement

If a list of boxes is provided, this can be a name, index, a deep path (e.g. c("detail", 1)) or a vector of names/indices to target a single nested element to move. You can also pass multiple targets as a list of paths (e.g. list(c("detail", 1), c("followup", 1))). The function will return the original list with the targeted element(s) replaced by their moved version(s).

Value

The element with updated viewport and coordinates

See Also

Other flowchart components: align(), append(), boxGrob(), boxHeaderGrob(), boxPropGrob(), boxShapes, connectGrob(), coords(), distance(), flowchart(), insert(), move(), spread()

Examples

library(grid)
grid.newpage()

pushViewport(viewport(y = 0.85, height = unit(.3, "npc")))
grid.rect(gp = gpar(col = "steelblue"))
grid.text("Example 1", y = .9, x = unit(10, "mm"), just = "left")
box <- boxGrob("A simple box", x = .5, y = .5) # Start at the middle
moveBox(box, x = -.2, space = "relative") # Move to the left
popViewport()


## Advanced example (skipped during R CMD check)
## Not run:
# Advanced example: create a nested list of treatment boxes, spread them horizontally,
# then move a single nested element (`Ibuprofen`) using `subelement` via a pipe.
pushViewport(viewport(y = 0.35, height = unit(.7, "npc") - unit(2, "mm")))
grid.rect(gp = gpar(col = "lightgreen"))

boxes <- list(
    population = boxGrob("Population"),
    treatment = list(
        Ibuprofen = boxGrob("Ibuprofen"),
        Paracetamol = boxGrob("Paracetamol"),
        Aspirin = boxGrob("Aspirin")
    ),
    followup = paste("Follow-up 4 weeks:",
        " - EQ-5D 5L",
        " - Lab",
        sep = "\n"
    ) |>
        boxGrob(just = "left")
) |>
    alignHorizontal(position = "center") |>
    spreadVertical() |>
    spreadHorizontal(
        from = unit(0.1, "npc"),
        to = unit(0.9, "npc"),
        type = "center",
        subelement = "treatment"
    ) |>
    moveBox(
        y = 0.1,
        space = "relative",
        subelement = c("treatment", "Ibuprofen")
    )

boxes
connectGrob(boxes$population, boxes$treatment, type = "N")
connectGrob(boxes$treatment, boxes$followup, type = "N")
## End(Not run)

Gmisc documentation built on March 6, 2026, 9:09 a.m.