4

I want to draw a border around an entire ggplot2 plot, such as the box("figure") function would do in case of a Base R plot.

Have a look at the following example:

data <- data.frame(x = 1:5,
                   y = 1:5)

library("ggplot2")

ggplot(data, aes(x, y)) +
  geom_point()

enter image description here

I would like to add a border around this plot as shown below:

enter image description here

I cannot believe that this information doesn't exist anywhere. Unfortunately, all I find is how to add a panel border.

How could I add a border around the ENTIRE ggplot2 plot?

Joachim Schork
  • 2,025
  • 3
  • 25
  • 48

1 Answers1

11

The theme element plot.background is an element_rect, which normally has a color of NA. Just change this to whatever color you like and adjust the linewidth to control the line width.

ggplot(data, aes(x, y)) +
  geom_point() +
  theme(plot.background = element_rect(color = "deepskyblue3", linewidth = 3))

enter image description here

Allan Cameron
  • 147,086
  • 7
  • 49
  • 87