Skip to contents

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.

Value

A ggplot2 plot with margins adjusted.

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.

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