patchwork icon indicating copy to clipboard operation
patchwork copied to clipboard

Is it possible to incorporate externally generated figures?

Open shahmj opened this issue 3 years ago • 2 comments

Hi,

Is there a way to incorporate externally generated figures while composing layouts with this package?

Thanks, Minita

shahmj avatar Nov 12 '21 17:11 shahmj

You can draw pictures to a ggplot with e.g. cowplot::draw_image(). This is more a question of how to draw pictures to a ggplot, but here is an example:

library(ggplot2)
library(patchwork)

p = ggplot(data = mtcars, aes(x = hp, y = disp)) +
  geom_point()

img = ggplot() +
  cowplot::draw_image("https://cran.r-project.org/Rlogo.svg")

p + img

image

mhovd avatar Nov 30 '21 08:11 mhovd

Thanks, that makes sense. Thanks for sharing an example!

shahmj avatar Nov 30 '21 15:11 shahmj

Hi @mhovd , thanks for the solution. Do you know if there is a way to reduce or eliminate the gray padding around the svg object?

rbutleriii avatar Jan 09 '23 22:01 rbutleriii

You can add a raster image directly with patchwork, treating it as any other plot object

library(ggplot2)
library(patchwork)

p1 <- ggplot(mtcars) + geom_point(aes(mpg, disp))

logo <- system.file('help', 'figures', 'logo.png', package = 'patchwork')
logo <- png::readPNG(logo, native = TRUE)

p1 + logo

Created on 2023-08-08 by the reprex package (v2.0.1)

thomasp85 avatar Aug 08 '23 08:08 thomasp85