Skip to content

Instantly share code, notes, and snippets.

@jackbrookes
Last active July 25, 2017 17:14
Show Gist options
  • Save jackbrookes/271e89f975490444731279dd72d0125d to your computer and use it in GitHub Desktop.
Save jackbrookes/271e89f975490444731279dd72d0125d to your computer and use it in GitHub Desktop.
Example of how to combine plots with images side-by-side in cowplot for publication ready figures.
library(png)
library(grid)
library(tidyverse)
# generate random data
my.data <- data.frame(x = c(1:10), y = rnorm(10))
# plot random data
g <- ggplot(my.data, aes(x = x, y = y)) +
geom_point()
# define plot png function
plot_png <- function(fname){
img <- readPNG(fname) %>%
rasterGrob(interpolate=FALSE)
ggplot() +
annotation_custom(img, xmin=-Inf, xmax=Inf, ymin=-Inf, ymax=Inf)
}
# create our ggplot-compatible png
img <- plot_png("task.png")
# plot images together in grid
plot_grid(g, img, labels = letters)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment