1

I am able to add a jpeg onto a base graphics plot using: ref:Adding a picture to plot in R

require(jpeg)
img <- readJPEG("myfile.jpeg")
#now open a plot window with coordinates
par(oma=c(2, 0, 3, 0))

plot(1:10, ty="n")
mtext("I would like to put logo here", adj=0, cex=1.5, line=1, side=3, outer=TRUE)
#specify the position of the image through bottom-left and top-right coords
rasterImage(img, 2, 2, 4, 4)

But how can I add a jpeg into the top lhs outer margin? I have a logo and I would like to put it there.

jay.sf
  • 60,139
  • 8
  • 53
  • 110
adam.888
  • 7,686
  • 17
  • 70
  • 105

1 Answers1

3

You could set xpd=TRUE to clip the image to the figure region rather than to the plot region. Then in rasterImage() just think the coordinates beyond the edges. You'll have to play around a little with the par() and rasterImage() position vectors.

Example

par(oma=c(2, 0, 5, 0), xpd=TRUE)  # c(bottom, left, top, right)
plot(1:10, ty="n")
rasterImage(img, -0.5, 12, 3, 15)  # c(xleft, ybottom, xright, ytop)

enter image description here

Image Data

library(png)
myurl <- "https://cdn.sstatic.net/Sites/stackoverflow/company/img/logos/so/so-logo.png?v=9c558ec15d8a"
z <- tempfile()
download.file(myurl,z,mode="wb")
img <- readPNG(z)
file.remove(z)
jay.sf
  • 60,139
  • 8
  • 53
  • 110