Skip to content

Instantly share code, notes, and snippets.

@emanuelhuber
Last active March 21, 2022 08:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save emanuelhuber/c0e0ebbefaaefdec5ae4e970a5457ee8 to your computer and use it in GitHub Desktop.
Save emanuelhuber/c0e0ebbefaaefdec5ae4e970a5457ee8 to your computer and use it in GitHub Desktop.
# Add a logo at the bottom left of a plot (outside plot area).
# obj = raster image
# wight = width in % of figure width
# shift in inch (x, y)
# interpolate Raster (=smooth)
#---
# inspiration: https://stackoverflow.com/a/56018973/16599100
addLogo <- function(obj, width = 0.25, shift = c(0,0), interpolate = TRUE){
xsign <- ifelse(diff(par()$usr[1:2]) > 0, 1, -1)
ysign <- ifelse(diff(par()$usr[3:4]) > 0, 1, -1)
W <- abs(diff(par()$usr[1:2]))
H <- abs(diff(par()$usr[3:4]))
PIN <- par()$pin
x0 <- par()$usr[1] + xsign * (shift[1] - par()$mai[2])/PIN[1]*W
y0 <- par()$usr[3] + ysign * (shift[2] - par()$mai[1])/PIN[2]*H
DIM <- dim(obj) # number of x-y pixels for the image
ARp <- DIM[1]/DIM[2] # pixel aspect ratio (y/x)
wobj <- width * W
hobj <- wobj * ARp * H/W * PIN[1]/PIN[2]
par(xpd = NA)
rasterImage(image = obj,
xleft = x0, xright = x0 + xsign * wobj,
ybottom = y0, ytop = y0 + ysign * hobj,
interpolate = interpolate)
par(xpd = FALSE)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment