简体   繁体   中英

How to add a jpeg logo to the top lhs outer margin of a base graphics plot?

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.

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)

在此处输入图片说明

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM