#######################################################################################
#
# Productivity drives the dynamics of a red kite source population that depends on immigration 
#
# Thomas Pfeiffer & Michael Schaub
#
# To be published in Journal of Avian Biology
#
# Article DOI: 10.1111/jav.02984
#
#######################################################################################
#
# Custom functions 
#
# Michael Schaub, 22.8.22
#
#######################################################################################


# 1. Function for computing 95% credible intervals
quant <- function(x) quantile(x, p = c(0.025, 0.975))

#############################

# 2. Function to place labels in a plot (function created by Mike Meredith)
corner.label <- function (label = NULL, x = -1, y = 1, xoff = NA, yoff = NA,
     figcorner = FALSE, ...)
 {
     if (is.na(xoff))
         xoff <- strwidth("m")/2
     if (is.na(yoff))
         yoff <- strheight("m")/2
     par.usr <- par("usr")
     xpos <- par.usr[(3 + x)/2]
     ypos <- par.usr[(3 + y)/2 + 2]
     if (figcorner) {
         par.pin <- par("pin")
         xplotrange <- par.usr[2] - par.usr[1]
         yplotrange <- par.usr[4] - par.usr[3]
         par.mai <- par("mai")
         xmar <- xplotrange * par.mai[3 + x]/par.pin[1]
         ymar <- yplotrange * par.mai[2 + y]/par.pin[2]
         xpos <- xpos + x * xmar
         ypos <- ypos + y * ymar
     }
     if (!is.null(label)) {
         if (figcorner)
             par(xpd = TRUE)
         text(xpos - x * xoff, ypos - y * yoff, label, adj = c((1 +
             # x)/2, (1 + y)/2)) # problem here, no dots
             x)/2, (1 + y)/2), ...)  # added dots
         if (figcorner)
             par(xpd = FALSE)
     }
 #    return(list(x = xpos, y = ypos))
 }

