From db1e4986f9d61528cbc2396eeba6b4083baf4e1b Mon Sep 17 00:00:00 2001 From: Schuch Date: Tue, 6 Feb 2024 14:53:42 -0500 Subject: [PATCH] recover plot_raster --- NAMESPACE | 3 ++ R/plot_raster.R | 113 +++++++++++++++++++++++++++++++++++++++++++++ man/plot_raster.Rd | 66 ++++++++++++++++++++++++++ 3 files changed, 182 insertions(+) create mode 100644 R/plot_raster.R create mode 100644 man/plot_raster.Rd diff --git a/NAMESPACE b/NAMESPACE index a499221..19bbffd 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -2,6 +2,7 @@ export(chem_edgar) export(get_edgar) +export(plot_raster) export(sfx_explode) export(st_explode) export(to_as4wrf) @@ -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) diff --git a/R/plot_raster.R b/R/plot_raster.R new file mode 100644 index 0000000..b6237d5 --- /dev/null +++ b/R/plot_raster.R @@ -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) + } +} diff --git a/man/plot_raster.Rd b/man/plot_raster.Rd new file mode 100644 index 0000000..40a75d6 --- /dev/null +++ b/man/plot_raster.Rd @@ -0,0 +1,66 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/plot_raster.R +\name{plot_raster} +\alias{plot_raster} +\title{Plot raster object} +\usage{ +plot_raster( + 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, + ... +) +} +\arguments{ +\item{r}{raster} + +\item{log}{TRUE to plot in log-scale} + +\item{min}{log of minimum for plot (default is -3)} + +\item{max}{log of maximum for plot} + +\item{legend.shrink}{legend height (default is 0.98)} + +\item{legend.width}{legend width (default is 3)} + +\item{axe}{to plot axis} + +\item{llaxis}{to plot custom axis} + +\item{int}{argument passed to latitude / longitude functions} + +\item{proj}{TRUE to project the raster to latlon} + +\item{col}{color} + +\item{x_adjust}{to raster shift dx} + +\item{y_adjust}{to raster shift dy} + +\item{zlim}{zlimits to be passed to plot} + +\item{hard_zlim}{bolean, default TRUE, use the maximum color if value is higher than lim\link{2} and lower than lim\link{1}} + +\item{...}{arguments to be passing to stats and plot} +} +\description{ +functions that modified plot from raster package +} +\examples{ +m <- readRDS(paste0(system.file("extdata",package="hackWRF"),"/model.Rds")) + + +}