Ian Fellows
Ian Fellows

Reputation: 17348

Plotting an image at an arbitrary location

I would like to add an image to a plot. I can sucessfully place the image in the range [0 1] using the image function, but I would like to be able to plot it in an alternate location. Here is an example:

require(rgdal)
require(RgoogleMaps)
bb <- qbbox(c(40.702147,40.711614,40.718217),c(-74.015794,-74.012318,-73.998284), TYPE = "all", margin = list(m=rep(5,4), TYPE = c("perc", "abs")[1]));

MyMap <- GetMap.bbox(bb$lonR, bb$latR,destfile = "MyTile3.png", maptype = "satellite")
plot(0:20,0:20)
image(MyMap$myTile,col=attr(MyMap$myTile,"COL"),add=TRUE)

This creates a tiny map located at the origin, I would like to have it span a range of my choosing (In reality it's actual latitude/longitude).

Upvotes: 0

Views: 240

Answers (2)

Greg Snow
Greg Snow

Reputation: 49640

Also look at the subplot function in the TeachingDemos package, or ms.image for use with my.symbols (also in TeachingDemos).

Upvotes: 0

mdsumner
mdsumner

Reputation: 29477

Use the x and y arguments to image(). Note how image(x) will work for a matrix, or a list composed of $x and $y vectors and $z matrix, but you can also pass in image(x = xvec, y = yvec, z = zmat) explicitly.

So, roughly (you will need to check whether you want cell centres [dim(z)] or cell corners [dim(z)+1])

    dims <- dim(seqMyMap$myTile)
    image(x = seq(minX, maxX, length = dims[1]), y = seq(minY, maxY, length = dims[2]), z =  seqMyMap$myTile,col=attr(MyMap$myTile,"COL"),add=TRUE) 

Also, read ?image - it does explain this.

Upvotes: 1

Related Questions