17

I'm trying to add a picture (jpeg,png doesn't care) to a plot which is defined by the layout function. For example:

a<-c(1,2,3,4,5)

b<-c(2,4,8,16,32)

m <- matrix(c(1,1,1,1,2,3,2,3), nrow = 2, ncol = 4)

layout(m); hist(a);boxplot(a~b);plot(b~a)*

Instead of the histogram on position 1 I want to add an image (In my case it's a map)

I don't know how to deal with the jpeg package, maybe you can help me!

David Arenburg
  • 91,361
  • 17
  • 137
  • 196
LarsTap
  • 173
  • 1
  • 1
  • 4

3 Answers3

16

Regarding Rodrigo's comment, I created a function that should preserve the image's pixel aspect ratio (addImg).

addImg <- function(
  obj, # an image file imported as an array (e.g. png::readPNG, jpeg::readJPEG)
  x = NULL, # mid x coordinate for image
  y = NULL, # mid y coordinate for image
  width = NULL, # width of image (in x coordinate units)
  interpolate = TRUE # (passed to graphics::rasterImage) A logical vector (or scalar) indicating whether to apply linear interpolation to the image when drawing. 
){
  if(is.null(x) | is.null(y) | is.null(width)){stop("Must provide args 'x', 'y', and 'width'")}
  USR <- par()$usr # A vector of the form c(x1, x2, y1, y2) giving the extremes of the user coordinates of the plotting region
  PIN <- par()$pin # The current plot dimensions, (width, height), in inches
  DIM <- dim(obj) # number of x-y pixels for the image
  ARp <- DIM[1]/DIM[2] # pixel aspect ratio (y/x)
  WIDi <- width/(USR[2]-USR[1])*PIN[1] # convert width units to inches
  HEIi <- WIDi * ARp # height in inches
  HEIu <- HEIi/PIN[2]*(USR[4]-USR[3]) # height in units
  rasterImage(image = obj, 
    xleft = x-(width/2), xright = x+(width/2),
    ybottom = y-(HEIu/2), ytop = y+(HEIu/2), 
    interpolate = interpolate)
}

Example of use:

library(png)
myurl <- "https://upload.wikimedia.org/wikipedia/commons/thumb/e/e1/Jupiter_%28transparent%29.png/242px-Jupiter_%28transparent%29.png"
z <- tempfile()
download.file(myurl,z,mode="wb")
pic <- readPNG(z)
file.remove(z) # cleanup
dim(pic)

png("plot.png", width = 5, height = 4, units = "in", res = 400)
  par(mar = c(3,3,0.5,0.5))
  image(volcano)
  addImg(pic, x = 0.3, y = 0.5, width = 0.2)
dev.off()

enter image description here

Marc in the box
  • 11,769
  • 4
  • 47
  • 97
13

You need to read your png or jpeg file through the png and jpeg packages. Then, with the rasterImage function you can draw the image on a plot. Say that your file is myfile.jpeg, you can try this:

require(jpeg)
img<-readJPEG("myfile.jpeg")
#now open a plot window with coordinates
plot(1:10,ty="n")
#specify the position of the image through bottom-left and top-right coords
rasterImage(img,2,2,4,4)

The above code will draw the image between the (2,2) and (4,4) points.

nicola
  • 24,005
  • 3
  • 35
  • 56
  • 2
    This will distort the image, if it's not a perfect square. Any option to specify just one coordinate (like, where the top-left corner of the image should be)? – Rodrigo Mar 15 '18 at 04:01
  • @Rodrigo - You are right regarding the distortion - I added a function below to preserve the aspect ratio. – Marc in the box May 07 '19 at 08:59
7

Just wanted to offer an alternative solution from the builtin "grid" package called grid.raster

From what I can tell it acts very much like rasterImage, but takes in normalized units, "npc" --a bonus in my opinion, and preserves aspect ratio unless you set both width and height. For my purposes, i just set either/or and the image seems to scale perfectly.

library(png)
library(grid)

x11()
mypng = readPNG('homer.png')
image(volcano)
grid.raster(mypng, x=.3, y=.3, width=.25) # print homer in ll conrner
grid.raster(mypng, x=.9, y=.7, width=.5) # print bigger homer in ur corner

while(!is.null(dev.list())) Sys.sleep(1)

here's a link

brotherJ4mes
  • 159
  • 1
  • 5
  • I am wondering how to create only a white background and not the "volcano". I also would like to change the x and y axis. However, I don't know how to do that. – Dolphin94 Jan 06 '21 at 15:00