R/limit_axes.R

Defines functions limit_axes

limit_axes <- function(kdes, limits = c(.0025, .9975)) {
  #' @name limit_axes
  #' @title calculate enveloping coordinates for uni-variate (posterior) samples
  #' @description calculate space around a posterior distribution to be plotted nicely
  #' @param kdes kde objects
  #' @param limits upper and lower limit of HDI
  #' @return the four required x and y coordinates
  #'
  #' @importFrom magrittr `%>%`
  #' @importFrom purrr map_df
  #' @importFrom tidyr pivot_longer
  #' @importFrom kde1d qkde1d dkde1d
  #'
  #' @export

  par_lims <- kdes %>% map_df(~ qkde1d(limits, .))
  par_lims$variable <- c("thxlo_x", "thxhi_x")
  max_d <- kdes %>% map_df(~ dkde1d(qkde1d(.5, .), .))
  max_d$variable <- "max_dens"
  zero_d <- kdes %>% map_df(~ dkde1d(0, .))
  zero_d$variable <- "zero_dens"
  par_lims <- rbind(par_lims, max_d, zero_d)

  par_lims <- par_lims %>%
    pivot_longer(
      cols = names(.)[names(.) != "variable"],
      names_to = "parameter"
    )

  return(par_lims)
}
MirkoTh/rutils documentation built on May 30, 2024, 9:02 p.m.