0

I'd like to create a map with points displayed by a custom image.

# Map with this data http://biogeo.ucdavis.edu/data/gadm2.8/rds/DEU_adm0.rds
map <- readRDS((".../DEU_adm0.rds")
map.f <- fortify(map)

# Coordinates
dat <- structure(list(Lat = c(53.0024118, 52.2263017), Lon = c(7.2665689, 
                                                                   10.7678071)), .Names = c("Lat", "Lon"), row.names = 1:2, class = "data.frame")
# Custom Image
img <- readPNG(system.file("img", "Rlogo.png", package="png"))

I've tried the code posted in this answer:

ggplot(map.f) +
  geom_polygon(aes(x = long, y = lat, group = group), 
               fill = "white", colour = "black") +
  coord_map() +
  mapply(function(xx, yy) 
    annotation_raster(img, xmin=xx-1, xmax=xx+1, ymin=yy-0.2, ymax=yy+0.2),
    dat$Lon, dat$Lat) 

However, I get the following error:

annotation_raster only works with Cartesian coordinates

What can I do instead?

Community
  • 1
  • 1
erc
  • 10,113
  • 11
  • 57
  • 88
  • `coord_quickmap()`, but you'll prbly get distortions in the annotations unless you pre-warp the annotated images. Think abt what you're asking ggplot to do. `coord_` transforms x & y values to a different coordinate system. There's no way to tell it to "transform the point where I want to place and image but ignore the coordinate system when you actually place the image". You might be able to work something out by using the `magick` package and making 2 plots. Plot 1 being the map and plot2 being the annotated points. (cont') – hrbrmstr Dec 11 '16 at 13:46
  • Just `spTransform` the coords for the points ahead of time but plot them on a plain x+y grid the same size as the map (use something besides `coord_quickmap` for this). Then use the `magick` functions to combine them. You'll need to ensure both plots have exactly the same dimensions which means granular `theme` & `scale` element control, but it's definitely doable. – hrbrmstr Dec 11 '16 at 13:52
  • @hrbrmstr thanks, using `coord_quickmap()` and playing with x/y min/max looks good enough for my purpose. – erc Dec 12 '16 at 07:53

0 Answers0