How to add an image as data points on a map in R

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

My question is if there is any way in R to represent data points with a picture of interest, such as an animal that I am working on in my example.

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

+3


source to share


3 answers


You can also use a mesh package. grid.raster

can be used to place some images.

Since it maps

is a graphics base package, you need to gridBase

merge the grid / base graphics.

Here's 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'm not sure there are camels in the USA ...

+3


source


rasterImage

is one way, albeit somewhat laborious. Once you have images of interest as bitmap objects, you can place them at specific locations (and frame sizes) within your plot area.



+2


source


In addition to the feature rasterImage

mentioned by @CarlWitthoft, there is also a combination my.symbols

and ms.image

from the TeachingDemos package for adding images to a plot (basic graphics). The approach rasterImage

gives the most control, but is my.symbols

more like a normal plotting function in which you say draw images centered in those coordinates (and set other parameters to specify size, etc.).

+1


source







All Articles