Skip to content
Snippets Groups Projects
plot_traj.R 2.92 KiB
Newer Older
thierrychambert's avatar
thierrychambert committed
##==============================================================================
##                           Plot trajectories                                ==
##==============================================================================
#' Plot demographic trajectories
#'
#' @param N a 4-D array containing demographic projection outputs
#' @param len number of individual trajectories to show on the plot
thierrychambert's avatar
thierrychambert committed
#' @param ... any other graphical input similar to the R plot function
#'
#' @return a plot of individual trajectories, from each simulation run,
#' and the average trajectory of each scenario.
#' Black = reference scenario (sc0). Red = fatality scenario (sc1)
#' @export
#'
#' @import dichromat
#'
#' @examples
#' plot_traj(demo_proj, xlab = "year", ylab = "pop size")
plot_traj <- function(N, len = 50, ...){
thierrychambert's avatar
thierrychambert committed
  TH <- dim(N)[2]
  nsim <- dim(N)[4]
  len <- min(len, nsim)
thierrychambert's avatar
thierrychambert committed

  # Average trend
  N_avg <- apply(N, c(1,2,3), mean)

  # Color palette
  col_sc0 <- make_transparent(colorRampPalette(colors = c("black", "grey"))(nsim), percent = 85)
  col_sc1 <- make_transparent(colorRampPalette(colors = c("red", "orange"))(nsim), percent = 85)

  # Initiate plot
  plot(x = 1:TH, y = colSums(N_avg[,,"sc0"]), type = 'n', col=1, lwd=3, ylim = c(0,max(colSums(N))), ...)

  ## Plot individual trajectories
  sel <- sample(x = nsim, size = len)

thierrychambert's avatar
thierrychambert committed
  # Scenario 0
  for(k in 1:length(sel)) points(x = 1:TH, y = colSums(N[,,"sc0",sel[k]]), type = 'l', col=col_sc0[k])
thierrychambert's avatar
thierrychambert committed

  # Scenario 1
  for(k in 1:length(sel)) points(x = 1:TH, y = colSums(N[,,"sc1",sel[k]]), type = 'l', col=col_sc1[k])
thierrychambert's avatar
thierrychambert committed

  ## Plot average trend for each scenario
  # Scenario 0
  points(x = 1:TH, y = colSums(N_avg[,,"sc0"]), type = 'l', col=1, lwd=3)

  # Scenario 1
  points(x = 1:TH, y = colSums(N_avg[,,"sc1"]), type = 'l', col="red2", lwd=3)

} # End function
################################################################################




##==============================================================================
##                Function to control color transparency                      ==
##==============================================================================
#' Function to control color transparency on a plot
#'
#' @param someColor some color name, such as "green", "red", etc.
#' @param percent the desired % of transparency, between 0 (no transparency) and 100 (full transparency) .
#'
#' @return a Hex Code including the desired transparency level, such as #RRGGBB7F
#' @export
#'
#' @import RColorBrewer
#' @import grDevices
#'
#' @examples
#' make_transparent("green", percent=50)
make_transparent <- function(someColor, percent=100)
{
  newColor <- col2rgb(someColor)
  apply(newColor, 2,
        function(curcoldata){
          rgb(red = curcoldata[1], green = curcoldata[2], blue = curcoldata[3],
              alpha = (100 - percent) * 255 / 100, maxColorValue = 255)
        }
  )
} # End function
################################################################################