'How to place multiple pngs on a plot in R?

Similar questions have been asked here and here. However, they don't exactly solve my issue.

I'm trying to plot a heatmap. But then on every tile of the heatmap, I want to place a png. Here's my code so far:

library(ggplot2)
library(png)
library(grid)

# get png
img <- readPNG(system.file("img", "Rlogo.png", package="png"))
g <- rasterGrob(img, interpolate=TRUE)

# create heatmap
x <- LETTERS[1:5]
y <- paste0("var", seq(1,5))
data <- expand.grid(X=x, Y=y)
data$Z <- runif(25, 0, 5)

# Heatmap 
p <- ggplot(data, aes(X, Y, fill= Z)) + 
  geom_tile() + 
  theme(aspect.ratio = 1)

# place png
p +
  annotation_custom(g, xmin = 0.5, xmax = 1.5, ymin = 0.5, ymax = 1.5)

The above code plots this:

graph with png

But what Im trying to do is place the png on every single tile go the heatmap. Now, I can retrieve the coordinates for each tile like so:

b <- ggplot_build(p)
b$data[[1]]$xmin
b$data[[1]]$xmax
b$data[[1]]$ymin
b$data[[1]]$ymax

But I don't know how to place the png on every tile. I was hoping to not have to have an annotation_custom argument for every single tile. I'm trying to somewhat automate the process.

Is there any way that this can be done?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source