remargin: Adjust margins of a ggplot2 plot

View source: R/panel.R

remarginR Documentation

Adjust margins of a ggplot2 plot

Description

To avoid the overlay rectangles moving around when the plot margins change, you can use this function to set specific margins for your plot. You will probably want to specify a large enough margin so that the axes and legends don't go out of the plot area.

Usage

remargin(g, t, r, b, l, unit = "npc")

Arguments

g

A ggplot2 plot.

t, r, b, l

Top, right, bottom, and left margins to set.

unit

Unit for the margins (see grid::unit() for permissible units). The default, "npc", refers to fractions of the overall plot area.

Details

Note that this only works with ggplot2 plots. For base plots, you can set the margins using par(plt = c(x1, x2, y1, y2)). See graphics::par() for details.

Value

A ggplot2 plot with margins adjusted.

Examples

plot1 = ggplot2::ggplot(data.frame(x = rnorm(10), y = rnorm(10))) +
    ggplot2::geom_point(ggplot2::aes(x, y))
plot2 = remargin(plot1, 0.1, 0.1, 0.1, 0.1) # plot with 10% margins all around


overshiny documentation built on Sept. 9, 2025, 5:50 p.m.