Search code examples
rconnectionmapsrastergplots

How to align a map plot and connection plot with a rasterized image in r?


How do I make this map and its points align with my image? [1]: https://i.sstatic.net/d6WRc.png

earth <- file.choose()
earth <- readJPEG(earth, native=TRUE)
par(mar=c(360,360,360,360))
grid.raster(earth)
maps:: map(add=TRUE)
points(x=cldrd$longitude, y=cldrd$latitude, col=c("magenta"), cex=(.7), pch=16)    
points(x=outlrd$longitude, y=outlrd$latitude, col=c("black"), cex=(.2), pch=16)
inter <- gcIntermediate(c(10.451526,51.165691), c(-96.8410503,32.8143702), n=100, addStartEnd=TRUE, breakAtDateLine=F)
lines(inter, col=c("#00ffbf"), lwd=.05)

Solution

  • Read the image and georeference it (it would be preferable to start with georeferenced data, there is plenty to go around)

    library(terra)
    f <- "https://i.sstatic.net/d6WRc.png"
    earth <- rast(f)
    # eyeballing
    ext(earth) <- c(-180, 180, -145, 145)
    plotRGB(earth)
    

    The image is so dark, that I figured I should add some lines for orientation

    w <- geodata::world(path=".")
    lines(w, col="gray")
    

    Now your coordinates

    crds <- rbind(c(10.451526,51.165691), c(-96.8410503,32.8143702))
    points(crds, pch=20, col="red", cex=2)
    
    library(geosphere)
    inter <- gcIntermediate(crds[1,], crds[2,], n=100, addStartEnd=TRUE)
    lines(inter, col="#00ffbf", lwd=2)
    

    enter image description here