Lucia
Lucia

Reputation: 911

How to add a picture as datapoints in a map in R

I am using maps package in R to draw a simple geographic map and then put my data points in it.

My question is that whether there is any way in R to represent data points with a picture of interest, for example, the animal I am working on in my example.

This is just to give a better representation of the distribution of my data points relative to each other for my reader.

Upvotes: 4

Views: 886

Answers (3)

Greg Snow
Greg Snow

Reputation: 49660

In addition to the rasterImage function mentioned by @CarlWitthoft there is also the combination of my.symbols and ms.image from the TeachingDemos package for adding images to a plot (base graphics). The rasterImage approach gives the most control, but my.symbols is more like the regular plotting functions in that you say plot the images centered at these coordinates (and set other options to specify size etc.)

Upvotes: 1

agstudy
agstudy

Reputation: 121608

You can also use grid package. The grid.raster can be used to put some pictures.

Since maps is graphic base package , you need to gridBase to combine the grid/base graphics.

Here an example:

library(maps)
map('usa',boundary=T,fill=T,col='grey')
library(gridBase)
library(grid)
library(png)
vps <- baseViewports()
pushViewport(vps$figure,vps$plot)
camel <- readPNG("camel.png")    ## some animal picture
grid.rect(gp = gpar(fill=NA))

x <- c(-110,-100,-70)
y <- c(30,40,40)

grid.raster(image=camel,x=x,y=y,width=5,   ## it is vectorized
            interpolate=FALSE,default.units = 'native')
upViewport(1)

enter image description here

PS: I am not sure that there are camels in USA...

Upvotes: 4

Carl Witthoft
Carl Witthoft

Reputation: 21532

rasterImage is one way, albeit somewhat laborious. Once you've got the images of interest formatted as raster objects, you can then place them at designated locations (and frame sizes) inside your plot region.

Upvotes: 2

Related Questions