在R中,在整个ggplot2图形周围添加边框

4
我希望在整个ggplot2图形周围绘制一个边框,就像Base R图中的box("figure")函数所做的那样。
请查看以下示例:
data <- data.frame(x = 1:5,
                   y = 1:5)

library("ggplot2")

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

enter image description here

我想在下面所示的图表周围添加边框:

enter image description here

我无法相信这些信息在任何地方都不存在。不幸的是,我只找到了如何添加面板边框

我应该如何在整个ggplot2图形周围添加边框?

1个回答

12

主题元素plot.background是一个element_rect,通常颜色为NA。只需将其更改为您喜欢的任何颜色,并调整linewidth以控制线宽。

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

enter image description here


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接