I'm trying to write a function that takes a ggplot and draws an image of said plot with a logo composite in the bottom right corner.
add_logo <- function(plot, width = 600, height = 400, logo_scale = 7){
frame <- image_graph(width = width, height = height)
plot + theme(plot.margin = unit(c(0.5, 0.5, 1.5, 0.5), "lines"))
dev.off()
logo <- image_read(filepath)
plot_height <- magick::image_info(frame)$height
plot_width <- magick::image_info(frame)$width
logo <- image_scale(logo, as.character(plot_width/logo_scale))
logo_height <- magick::image_info(logo)$height
logo_width <- magick::image_info(logo)$width
x_pos <- plot_width - logo_width - 0.01 * plot_width
y_pos <- plot_height - logo_height - 0.01 * plot_height
comp <- image_composite(frame, logo, offset = paste0("+", x_pos, "+", y_pos))
print(comp)
}
When I call add_logo(plot)
it prints an empty tibble to the console. However, the intended image is printed when I explicitly run the code in the function (not as a function call).
Maybe it's because I've been reading more about environments recently, but that difference in behavior makes me think there could be a disconnect in environments and the magick package when calling the function?