3

I have a vector of map tiles that I would like to plot in ggplot.

For instance I would like to place this image:

download.file("http://tile.stamen.com/toner/14/8601/5873.png", "tile_8601_5873.png", mode = "wb")

Into the "fill" of tile1:

data.frame(x = c(8601, 8602), y = c(5873, 5873), tile = c('tile1', 'tile2')) %>% 
ggplot(aes(x = x, y= y, fill = tile)) +
  geom_tile() +
  coord_equal()

This is a minimal example of the output I'm looking for—although I actually have dozens of tiles to which I'm looking to overlap tiles.

enter image description here

Dambo
  • 3,318
  • 5
  • 30
  • 79
  • this is basically a duplicate question... https://stackoverflow.com/questions/28206611/adding-custom-image-to-geom-polygon-fill-in-ggplot. see the related comments especially the ones concerning use of images in fill – tjebo May 01 '20 at 14:07

1 Answers1

3

What about this, based on this anwser?

library(png)
img <- readPNG("test.png")
g <- rasterGrob(img, interpolate=TRUE)

data.frame(x = c(8601, 8602), y = c(5873, 5873), tile = c('tile1', 'tile2')) %>% 
  ggplot(aes(x = x, y= y, fill = tile)) +
  geom_tile() +
  annotation_custom(g, xmin=8600.5, xmax=8601.5, ymin=5872.5, ymax=5873.50) +
  coord_equal()

For me, this worked. Of course, you can adjust the way that the coordinates are determined.

enter image description here

Jrm_FRL
  • 1,394
  • 5
  • 15
  • This would work, thanks. I'll just wait to accept the answer in case someone shows it's possible to pass images directly to the `fill` – Dambo Apr 28 '20 at 14:06