0

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?

Electrino
  • 2,636
  • 3
  • 18
  • 40

1 Answers1

2

You can add the annotations in a loop

xmin = b$data[[1]]$xmin
xmax = b$data[[1]]$xmax
ymin = b$data[[1]]$ymin
ymax = b$data[[1]]$ymax

for (i in seq_len(nrow(data))) {
  p = p + annotation_custom(g, xmin = xmin[i], xmax = xmax[i], 
                               ymin = ymin[i], ymax = ymax[i])
}

enter image description here

dww
  • 30,425
  • 5
  • 68
  • 111