Skip to content

Commit

Permalink
recover plot_raster
Browse files Browse the repository at this point in the history
  • Loading branch information
Schuch666 committed Feb 6, 2024
1 parent 5c75b26 commit db1e498
Show file tree
Hide file tree
Showing 3 changed files with 182 additions and 0 deletions.
3 changes: 3 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

export(chem_edgar)
export(get_edgar)
export(plot_raster)
export(sfx_explode)
export(st_explode)
export(to_as4wrf)
Expand All @@ -19,12 +20,14 @@ export(wrf_profile)
export(wrf_put)
export(wrf_summary)
import(ncdf4)
import(raster)
importFrom(cptcity,cpt)
importFrom(data.table,".N")
importFrom(data.table,".SD")
importFrom(data.table,as.data.table)
importFrom(data.table,rbindlist)
importFrom(grDevices,cm.colors)
importFrom(grDevices,colorRampPalette)
importFrom(grDevices,gray.colors)
importFrom(graphics,.filled.contour)
importFrom(graphics,Axis)
Expand Down
113 changes: 113 additions & 0 deletions R/plot_raster.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
#' Plot raster object
#'
#' @description functions that modified plot from raster package
#'
#' @param r raster
#' @param log TRUE to plot in log-scale
#' @param min log of minimum for plot (default is -3)
#' @param max log of maximum for plot
#' @param legend.shrink legend height (default is 0.98)
#' @param legend.width legend width (default is 3)
#' @param axe to plot axis
#' @param llaxis to plot custom axis
#' @param int argument passed to latitude / longitude functions
#' @param proj TRUE to project the raster to latlon
#' @param col color
#' @param x_adjust to raster shift dx
#' @param y_adjust to raster shift dy
#' @param zlim zlimits to be passed to plot
#' @param hard_zlim bolean, default TRUE, use the maximum color if value is higher than lim[2] and lower than lim[1]
#' @param ... arguments to be passing to stats and plot
#'
#' @import raster
#' @importFrom grDevices colorRampPalette
#'
#' @export
#'
#' @examples
#' m <- readRDS(paste0(system.file("extdata",package="hackWRF"),"/model.Rds"))
#'
#'
plot_raster <- function(r, log = FALSE, min = -3, max,
legend.shrink = 0.98,legend.width = 3,
axe = !llaxis, llaxis = F, int = 10,
proj = FALSE,
col = c('white',
colorRampPalette(colors = c("#D1F5B1",
"#FFDE24FC",
"#C70000"))(39)),
x_adjust = 0,
y_adjust = 0,
zlim = c(cellStats(r,'min'),cellStats(r,'max')),
hard_zlim = TRUE,
...){

if(proj){
r <- projectRaster(r, crs="+proj=longlat +datum=WGS84 +no_defs")
}

if(x_adjust!=0){
r <- raster::shift(x = r,dx=x_adjust)
}
if(y_adjust!=0){
r <- raster::shift(x = r,dy=y_adjust)
}

if(hard_zlim & !log){
r[r[] < zlim[1] ] = zlim[1]
r[r[] > zlim[2] ] = zlim[2]
}

Rlog10 <- function(r,min){
test <- suppressWarnings(log10(x = r))
test[is.infinite(test)] <- min
test[test[] < min ] = min
return(test)
}

if(log){
r_log <- Rlog10(r = r,min = min)
rng <- range(r_log[], na.rm = T)
if(missing(max)){
at <- seq(round(rng[1], 1),round(rng[2], 1),by = 1)
}else{
at <- seq(round(min, 1),round(max, 1),by = 1)
}
label <- paste0('10^',at)
label <- parse(text = label)
label[at == 0] = ' 1'

arg <- list(at=at, labels=label)

if(missing(max)){
plot(x = r_log,
legend.shrink = legend.shrink,
legend.width = legend.width,
axe = axe,
axis.args = arg,
col = col,
...)
}else{
plot(x = r_log,
legend.shrink = legend.shrink,
legend.width = legend.width,
axe = axe,
axis.args = arg,
col = col,
zlim = c(min,max),
...)
}
}else{
plot(x = r,
legend.shrink = legend.shrink,
legend.width = legend.width,
axe = axe,
col = col,
zlim = zlim,
...)
}
if(llaxis){
latitude(int = int)
longitude(int = int)
}
}
66 changes: 66 additions & 0 deletions man/plot_raster.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit db1e498

Please sign in to comment.