简体   繁体   中英

ggplot object borders when overlaying in cowplot, patchwork, etc

I'm assembling a mixture of ggplot objects. These objects have coloured backgrounds and I'd like the end product to look seamless. However, they all have a pencil thin white border which I can't find a ggplot theme option to remove - see image

Same issue arranging in cowplot or patchwork so seems to be a property of the ggplot object itself. Here's a reprex that creates the image.

library(ggplot2)
library(patchwork)

background <- ggplot() +
  theme(panel.background = element_blank(),
        plot.background = element_rect(fill = "blue"))

inset <- ggplot() +
  theme(panel.background = element_blank(),
        plot.background = element_rect(fill = "blue"))

layout <- c(
  area(t = 2, l = 1, b = 5, r = 4),
  area(t = 1, l = 3, b = 3, r = 5)
)

background + inset + 
  plot_layout(design = layout)

For some reason I also don't understand, the default background theme also includes a non-transparent colour (in addition to the fill). To make the borders go away, you'd need to make the colour transparent. Example below:

library(ggplot2)
library(patchwork)

background <- ggplot() +
  theme(panel.background = element_blank(),
        plot.background = element_rect(fill = "blue", colour = NA))

inset <- ggplot() +
  theme(panel.background = element_blank(),
        plot.background = element_rect(fill = "blue", colour = NA))

layout <- c(
  area(t = 2, l = 1, b = 5, r = 4),
  area(t = 1, l = 3, b = 3, r = 5)
)

background + inset + 
  plot_layout(design = layout)

Created on 2020-09-17 by the reprex package (v0.3.0)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM