diff --git a/DESCRIPTION b/DESCRIPTION index e46b711..3225a92 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -1,6 +1,6 @@ Package: gg.layers Title: ggplot layers -Version: 0.1.1 +Version: 0.1.2 Authors@R: c( person("Dongdong", "Kong", role = c("aut", "cre"), email = "kongdd.sysu@gmail.com", comment = c(ORCID = "0000-0003-1836-8172"))) @@ -25,8 +25,8 @@ Imports: methods Suggests: testthat (>= 3.0.0), - data.table, rcolors, + scales, sf, stars, raster, mapview, lattice, @@ -39,4 +39,4 @@ Config/testthat/edition: 3 VignetteBuilder: knitr LazyData: true Roxygen: list(markdown = TRUE) -RoxygenNote: 7.2.3 +RoxygenNote: 7.3.0 diff --git a/NAMESPACE b/NAMESPACE index 175b3c4..6e53e0b 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -104,7 +104,6 @@ export(label_num) export(layer_PosNeg) export(layer_PosNeg_sign) export(layer_barchart) -export(layer_statistic) export(make_colorbar) export(mutate) export(polygon.fullhatch) @@ -149,6 +148,7 @@ import(magrittr) import(rtrend) importFrom(broom,glance) importFrom(broom,tidy) +importFrom(data.table,":=") importFrom(data.table,as.data.table) importFrom(data.table,data.table) importFrom(data.table,is.data.table) @@ -187,6 +187,10 @@ importFrom(ggpp,geom_text_npc) importFrom(ggtext,element_markdown) importFrom(ggtext,element_textbox) importFrom(ggtext,geom_richtext) +importFrom(graphics,abline) +importFrom(graphics,axis) +importFrom(graphics,par) +importFrom(graphics,polygon) importFrom(grid,gpar) importFrom(grid,grobHeight) importFrom(grid,grobTree) @@ -196,21 +200,23 @@ importFrom(grid,unit) importFrom(gridtext,richtext_grob) importFrom(gtable,gtable) importFrom(gtable,gtable_add_grob) -importFrom(magrittr,`%<>%`) -importFrom(magrittr,`%>%`) -importFrom(rlang,`%||%`) +importFrom(magrittr,"%<>%") +importFrom(magrittr,"%>%") +importFrom(rlang,"%||%") importFrom(rlang,inject) importFrom(rlang,list2) importFrom(rlang,try_fetch) importFrom(rtrend,slope_mk) importFrom(rtrend,slope_p) importFrom(stats,approx) +importFrom(stats,cor) importFrom(stats,cor.test) importFrom(stats,lm) importFrom(stats,median) importFrom(stats,predict) importFrom(stats,qf) importFrom(stats,quantile) +importFrom(stats,sd) importFrom(stats,setNames) importFrom(stringr,str_replace_all) importFrom(utils,str) diff --git a/NEWS.md b/NEWS.md index 4cea85a..50511c9 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,3 +1,8 @@ +# gg.layers 0.1.2 + +- fix `st_hatched_polygon` for `MULTIPOLYGON` +- reduce R cmd check warnings + # gg.layers 0.1.0 * Added a `NEWS.md` file to track changes to the package. diff --git a/R/GOF.R b/R/GOF.R index f6d9209..7f29c1b 100644 --- a/R/GOF.R +++ b/R/GOF.R @@ -2,8 +2,8 @@ #' #' Good of fitting #' -#' @param yobs Numeric vector, observations -#' @param ysim Numeric vector, corresponding simulated values +#' @param obs Numeric vector, observations +#' @param sim Numeric vector, corresponding simulated values #' @param w Numeric vector, weights of every points. If w included, when #' calculating mean, Bias, MAE, RMSE and NSE, w will be taken into considered. #' @param include.cv If true, cv will be included. @@ -30,31 +30,31 @@ #' 4. Zhang Xiaoyang (2015), http://dx.doi.org/10.1016/j.rse.2014.10.012 #' #' @examples -#' yobs = rnorm(100) -#' ysim = yobs + rnorm(100)/4 -#' GOF(yobs, ysim) +#' obs = rnorm(100) +#' sim = obs + rnorm(100)/4 +#' GOF(obs, sim) #' #' @importFrom dplyr tibble #' @export -GOF <- function(yobs, ysim, w, include.cv = FALSE, include.r = TRUE){ - if (missing(w)) w <- rep(1, length(yobs)) +GOF <- function(obs, sim, w, include.cv = FALSE, include.r = TRUE){ + if (missing(w)) w <- rep(1, length(obs)) - # remove NA_real_ and Inf values in ysim, yobs and w + # remove NA_real_ and Inf values in sim, obs and w valid <- function(x) !is.na(x) & is.finite(x) - I <- which(valid(ysim) & valid(yobs) & valid(w)) - # n_obs <- length(yobs) + I <- which(valid(sim) & valid(obs) & valid(w)) + # n_obs <- length(obs) n_sim <- length(I) - ysim <- ysim[I] - yobs <- yobs[I] + sim <- sim[I] + obs <- obs[I] w <- w[I] if (include.cv) { - CV_obs <- cv_coef(yobs, w) - CV_sim <- cv_coef(ysim, w) + CV_obs <- cv_coef(obs, w) + CV_sim <- cv_coef(sim, w) } - if (is_empty(yobs)){ + if (is_empty(obs)){ out <- c(RMSE = NA_real_, KGE = NA_real_, NSE = NA_real_, MAE = NA_real_, AI = NA_real_, @@ -67,16 +67,16 @@ GOF <- function(yobs, ysim, w, include.cv = FALSE, include.r = TRUE){ # R2: the portion of regression explained variance, also known as # coefficient of determination - KGE = KGE(ysim, yobs) + KGE = KGE(sim, obs) # https://en.wikipedia.org/wiki/Coefficient_of_determination # https://en.wikipedia.org/wiki/Explained_sum_of_squares - y_mean <- sum(yobs * w) / sum(w) + y_mean <- sum(obs * w) / sum(w) - SSR <- sum( (ysim - y_mean)^2 * w) - SST <- sum( (yobs - y_mean)^2 * w) + SSR <- sum( (sim - y_mean)^2 * w) + SST <- sum( (obs - y_mean)^2 * w) # R2 <- SSR / SST - RE <- ysim - yobs + RE <- sim - obs Bias <- sum ( w*RE) /sum(w) # bias Bias_perc <- Bias/y_mean # bias percentage MAE <- sum ( w*abs(RE))/sum(w) # mean absolute error @@ -93,7 +93,7 @@ GOF <- function(yobs, ysim, w, include.cv = FALSE, include.r = TRUE){ pvalue <- NA_real_ tryCatch({ - cor.obj <- cor.test(yobs, ysim, use = "complete.obs") + cor.obj <- cor.test(obs, sim, use = "complete.obs") R <- cor.obj$estimate[[1]] pvalue <- cor.obj$p.value }, error = function(e){ @@ -102,16 +102,16 @@ GOF <- function(yobs, ysim, w, include.cv = FALSE, include.r = TRUE){ R2 = R^2 } # In Linear regression, R2 = R^2 (R is pearson cor) - # R2 <- summary(lm(ysim ~ yobs))$r.squared # low efficient + # R2 <- summary(lm(sim ~ obs))$r.squared # low efficient # AI: Agreement Index (only good values(w==1) calculate AI) AI <- NA_real_ I2 <- which(w == 1) if (length(I2) >= 2) { - yobs = yobs[I2] - ysim = ysim[I2] - y_mean = mean(yobs) - AI = 1 - sum( (ysim - yobs)^2 ) / sum( (abs(ysim - y_mean) + abs(yobs - y_mean))^2 ) + obs = obs[I2] + sim = sim[I2] + y_mean = mean(obs) + AI = 1 - sum( (sim - obs)^2 ) / sum( (abs(sim - y_mean) + abs(obs - y_mean))^2 ) } out <- tibble(R, pvalue, R2, NSE, KGE, RMSE, MAE, @@ -138,20 +138,20 @@ KGE <- function(obs, sim, w = c(1, 1, 1), ...) { #' @rdname GOF #' @export -NSE <- function(yobs, ysim, w, ...) { - if (missing(w)) w <- rep(1, length(yobs)) +NSE <- function(obs, sim, w, ...) { + if (missing(w)) w <- rep(1, length(obs)) - ind <- valindex(yobs, ysim) + ind <- valindex(obs, sim) w <- w[ind] - y_mean <- sum(yobs[ind] * w) / sum(w) + y_mean <- sum(obs[ind] * w) / sum(w) # R2: the portion of regression explained variance, also known as # coefficient of determination - # SSR <- sum((ysim - y_mean)^2 * w) - SST <- sum((yobs[ind] - y_mean)^2 * w) + # SSR <- sum((sim - y_mean)^2 * w) + SST <- sum((obs[ind] - y_mean)^2 * w) # R2 <- SSR / SST - RE <- ysim[ind] - yobs[ind] + RE <- sim[ind] - obs[ind] # Bias <- sum(w * RE) / sum(w) # bias # Bias_perc <- Bias / y_mean # bias percentage # MAE <- sum(w * abs(RE)) / sum(w) # mean absolute error diff --git a/R/colorbar_triangle.R b/R/colorbar_triangle.R index 8cbbd73..f3ec4cb 100644 --- a/R/colorbar_triangle.R +++ b/R/colorbar_triangle.R @@ -1,5 +1,7 @@ #' colourbar_triangle #' +#' @param ... parameters passed to [ggplot2::guide_colourbar] +#' #' @example R/examples/ex-colorbar_triangle.R #' #' @references diff --git a/R/geom_prcpRunoff.R b/R/geom_prcpRunoff.R index 4081d0c..8fa5609 100644 --- a/R/geom_prcpRunoff.R +++ b/R/geom_prcpRunoff.R @@ -23,8 +23,8 @@ theme_dual_axis <- function(col_left = "darkorange", col_right = "blue") { ) } -#' @importFrom rlang `%||%` -#' @importFrom magrittr `%<>%` `%>%` +#' @importFrom rlang %||% +#' @importFrom magrittr %<>% %>% #' @importFrom ggplot2 ggproto GeomLine aes layer GeomPrcpRunoff <- ggproto( "GeomPrcpRunoff", GeomLine, diff --git a/R/gg.layers-package.R b/R/gg.layers-package.R index e78a42c..9b084e2 100644 --- a/R/gg.layers-package.R +++ b/R/gg.layers-package.R @@ -6,26 +6,34 @@ #' @import grid magrittr ggplot2 #' @importFrom gridtext richtext_grob #' @importFrom grid gpar -#' @importFrom stats median approx setNames quantile lm predict cor.test qf -#' @importFrom dplyr first last tibble +#' @importFrom stats median approx setNames quantile lm predict cor.test qf cor sd +#' @importFrom dplyr first last tibble +#' @importFrom data.table := +#' @importFrom graphics abline par axis polygon #' @importFrom utils str ## usethis namespace: end NULL # ' @importFrom ggplot2 geom_abline .onLoad <- function(libname, pkgname) { - + if (getRversion() >= "2.15.1") { + utils::globalVariables( + c( + ".", ".SD", ".N", "..vars" + ) + ) + } } #' @keywords internal -#' @export +#' @export init_lattice <- function() { - # lattice.layers:::`+.trellis` - # environment(latticeExtra:::`+.trellis`) - suppressWarnings({ - eval(parse(text = 'environment(draw.colorkey) <- environment(lattice::xyplot)')) - eval(parse(text = 'assignInNamespace("draw.colorkey", draw.colorkey, ns="lattice")')) - }) - # asign_func(draw.colorkey, lattice::draw.colorkey) - # invisible() + # lattice.layers:::`+.trellis` + # environment(latticeExtra:::`+.trellis`) + suppressWarnings({ + eval(parse(text = "environment(draw.colorkey) <- environment(lattice::xyplot)")) + eval(parse(text = 'assignInNamespace("draw.colorkey", draw.colorkey, ns="lattice")')) + }) + # asign_func(draw.colorkey, lattice::draw.colorkey) + # invisible() } diff --git a/R/layer_statistic.R b/R/layer_statistic.R index 783d5e4..3b4d205 100644 --- a/R/layer_statistic.R +++ b/R/layer_statistic.R @@ -1,24 +1,26 @@ -#' @export -layer_statistic <- function( - mapping = NULL, data = NULL, - x = 0, y = 1, hjust = 0, vjust = 1, family = "Times", - digit = 2, - color = NULL, ...) -{ - fun <- function(data, coords) { - l = data$z %>% stat_statistic() +# #' @export +# layer_statistic <- function( +# mapping = NULL, data = NULL, +# x = 0, y = 1, hjust = 0, vjust = 1, family = "Times", +# digit = 2, +# color = NULL, ...) +# { +# fun <- function(data, coords) { +# l = data$z %>% stat_statistic() - fmt = glue(" = %.{digit}f ± %.{digit}f") - num = sprintf(fmt, l$mean, l$sd) - label = eval(substitute(expression(bar(italic(u))*" ± "*italic(sd)~num), - list(num = num))) +# fmt = glue(" = %.{digit}f ± %.{digit}f") +# num = sprintf(fmt, l$mean, l$sd) +# label = eval(substitute(expression(bar(italic(u))*" ± "*italic(sd)~num), +# list(num = num))) - element_grob_text(label = label, - x = x, y = y, - vjust = vjust, hjust = hjust, family = family, ...) - # richtextGrob(label, x, y, hjust, vjust, mar, - # family = family, fontface = fontface, fontsize = fontsize, - # color = color, ...) - } - grid_panel(fun, mapping, data) -} +# element_grob_text(label = label, +# x = x, y = y, +# vjust = vjust, hjust = hjust, family = family, ...) +# # richtextGrob(label, x, y, hjust, vjust, mar, +# # family = family, fontface = fontface, fontsize = fontsize, +# # color = color, ...) +# } +# grid_panel(fun, mapping, data) +# } + +# stat_statistic is missing diff --git a/R/stat_gof.R b/R/stat_gof.R index 2a289ed..53118ff 100644 --- a/R/stat_gof.R +++ b/R/stat_gof.R @@ -26,7 +26,9 @@ StatGOF <- ggproto("StatGOF", Stat, #' stat_gof #' #' @inheritParams geom_richtext_npc -#' +#' @param label.format format string for label, default `fmt_gof` +#' @param show.bias whether to show bias +#' #' @return No return. This function is used to calculate data for gglot2 `geom_*`, #' just like [ggplot2::stat_smooth()]. #' @@ -55,6 +57,7 @@ stat_gof <- function(mapping = NULL, data = NULL, ) } +#' @inheritParams ggplot2::element_text #' @rdname stat_gof #' @export geom_gof <- function(mapping = NULL, data = NULL, diff --git a/R/stat_prob_2d.R b/R/stat_prob_2d.R index b18691f..6493eb3 100644 --- a/R/stat_prob_2d.R +++ b/R/stat_prob_2d.R @@ -7,6 +7,9 @@ snake_class = ggplot2:::snake_class data_frame0 <- function(...) vctrs::data_frame(..., .name_repair = "minimal") #' prob density +#' +#' @param geom Use to override the default connection between [geom_prob_2d()] and [stat_prob_2d]. +#' #' @inheritParams ggplot2::stat_density_2d #' @example R/examples/ex-stat_prob_2d.R #' @export diff --git a/R/stat_signPattern.R b/R/stat_signPattern.R index b647eea..928fdae 100644 --- a/R/stat_signPattern.R +++ b/R/stat_signPattern.R @@ -16,6 +16,8 @@ StatSignPattern <- ggproto("StatSignPattern", StatSf, #' stat_signPattern #' #' @inheritParams ggpattern::geom_sf_pattern +#' @param geom Use to override the default connection between [geom_signPattern()] +#' and [stat_signPattern]. #' @param ... other parameters to [ggpattern::geom_sf_pattern()] #' #' @seealso [ggpattern::geom_sf_pattern()] diff --git a/R/str_format.R b/R/str_format.R index 29f17bf..2146862 100644 --- a/R/str_format.R +++ b/R/str_format.R @@ -43,6 +43,9 @@ label_num <- function(fmt = "%.2f") { function(x) sprintf(fmt, x) } +#' @param labels character vector +#' @param ... ignored +#' #' @rdname str_mk #' @export label_mk <- function(labels, ...) { diff --git a/man/GOF.Rd b/man/GOF.Rd index 1fe0746..8a5e6d9 100644 --- a/man/GOF.Rd +++ b/man/GOF.Rd @@ -6,16 +6,16 @@ \alias{NSE} \title{GOF} \usage{ -GOF(yobs, ysim, w, include.cv = FALSE, include.r = TRUE) +GOF(obs, sim, w, include.cv = FALSE, include.r = TRUE) KGE(obs, sim, w = c(1, 1, 1), ...) -NSE(yobs, ysim, w, ...) +NSE(obs, sim, w, ...) } \arguments{ -\item{yobs}{Numeric vector, observations} +\item{obs}{Numeric vector, observations} -\item{ysim}{Numeric vector, corresponding simulated values} +\item{sim}{Numeric vector, corresponding simulated values} \item{w}{Numeric vector, weights of every points. If w included, when calculating mean, Bias, MAE, RMSE and NSE, w will be taken into considered.} @@ -44,9 +44,9 @@ calculate. See details in Zhang et al., (2015). Good of fitting } \examples{ -yobs = rnorm(100) -ysim = yobs + rnorm(100)/4 -GOF(yobs, ysim) +obs = rnorm(100) +sim = obs + rnorm(100)/4 +GOF(obs, sim) } \references{ diff --git a/man/colourbar_triangle.Rd b/man/colourbar_triangle.Rd index f507c84..f43ffee 100644 --- a/man/colourbar_triangle.Rd +++ b/man/colourbar_triangle.Rd @@ -6,6 +6,9 @@ \usage{ colourbar_triangle(...) } +\arguments{ +\item{...}{parameters passed to \link[ggplot2:guide_colourbar]{ggplot2::guide_colourbar}} +} \description{ colourbar_triangle } diff --git a/man/geom_taylor.Rd b/man/geom_taylor.Rd index 59cf23d..190cc45 100644 --- a/man/geom_taylor.Rd +++ b/man/geom_taylor.Rd @@ -12,6 +12,7 @@ geom_taylor( ..., obs.colour = "black", obs.size = 5, + show.obs.label = TRUE, na.rm = FALSE, show.legend = NA, inherit.aes = TRUE diff --git a/man/stat_gof.Rd b/man/stat_gof.Rd index ca2d6bb..ab1919e 100644 --- a/man/stat_gof.Rd +++ b/man/stat_gof.Rd @@ -92,6 +92,12 @@ a warning. If \code{TRUE}, missing values are silently removed.} It can also be a named logical vector to finely select the aesthetics to display.} +\item{show.bias}{whether to show bias} + +\item{label.format}{format string for label, default \code{fmt_gof}} + +\item{x}{A single number specifying size relative to parent element.} + \item{inherit.aes}{If \code{FALSE}, overrides the default aesthetics, rather than combining with them. This is most useful for helper functions that define both data and aesthetics and shouldn't inherit behaviour from @@ -105,6 +111,12 @@ to the paired geom/stat.} \item{stat}{The statistical transformation to use on the data for this layer, as a string.} +\item{hjust}{Horizontal justification (in \eqn{[0, 1]})} + +\item{vjust}{Vertical justification (in \eqn{[0, 1]})} + +\item{size}{text size in pts.} + \item{formula}{an object of class \code{"\link[stats]{formula}"} (or one that can be coerced to that class): a symbolic description of the model to be fitted. The details of model specification are given @@ -113,6 +125,8 @@ layer, as a string.} \item{digits}{the number of \emph{significant} digits to be passed to \code{\link{format}(\link[stats]{coef}(x), .)} when \code{\link{print}()}ing.} + +\item{family}{Font family} } \value{ No return. This function is used to calculate data for gglot2 \verb{geom_*}, diff --git a/man/stat_prob_2d.Rd b/man/stat_prob_2d.Rd index bd1b8e9..8a04bc6 100644 --- a/man/stat_prob_2d.Rd +++ b/man/stat_prob_2d.Rd @@ -41,6 +41,8 @@ the plot data. The return value must be a \code{data.frame}, and will be used as the layer data. A \code{function} can be created from a \code{formula} (e.g. \code{~ head(.x, 10)}).} +\item{geom}{Use to override the default connection between \code{\link[=geom_prob_2d]{geom_prob_2d()}} and \link{stat_prob_2d}.} + \item{position}{Position adjustment, either as a string naming the adjustment (e.g. \code{"jitter"} to use \code{position_jitter}), or the result of a call to a position adjustment function. Use the latter if you need to change the diff --git a/man/stat_signPattern.Rd b/man/stat_signPattern.Rd index 4b0b5f9..7cd8275 100644 --- a/man/stat_signPattern.Rd +++ b/man/stat_signPattern.Rd @@ -35,6 +35,9 @@ the plot data. The return value must be a \code{data.frame}, and will be used as the layer data. A \code{function} can be created from a \code{formula} (e.g. \code{~ head(.x, 10)}).} +\item{geom}{Use to override the default connection between \code{\link[=geom_signPattern]{geom_signPattern()}} +and \link{stat_signPattern}.} + \item{position}{Position adjustment, either as a string naming the adjustment (e.g. \code{"jitter"} to use \code{position_jitter}), or the result of a call to a position adjustment function. Use the latter if you need to change the diff --git a/man/str_mk.Rd b/man/str_mk.Rd index 9bce10d..70de6a1 100644 --- a/man/str_mk.Rd +++ b/man/str_mk.Rd @@ -11,6 +11,10 @@ label_mk(labels, ...) } \arguments{ \item{x}{character vector} + +\item{labels}{character vector} + +\item{...}{ignored} } \description{ markdown superscript and subscript