Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

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.

like image 851
Lucia Avatar asked Aug 31 '25 17:08

Lucia


2 Answers

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...

like image 197
agstudy Avatar answered Sep 02 '25 16:09

agstudy


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.

like image 39
Carl Witthoft Avatar answered Sep 02 '25 18:09

Carl Witthoft