diff options
author | Ken Kellner <ken@kenkellner.com> | 2023-05-16 20:38:36 -0400 |
---|---|---|
committer | Ken Kellner <ken@kenkellner.com> | 2023-05-16 20:38:36 -0400 |
commit | aae0a2026057fabe5bff45d6977464f76f09c8a9 (patch) | |
tree | f3b0b0aee30b040d51e7e8e53afe21c07c30b2e2 | |
parent | 36a3cc230e32b6204e463f176b58d63efcf03a5b (diff) |
Remove deprecated ggplot2 functions
-rw-r--r-- | DESCRIPTION | 7 | ||||
-rw-r--r-- | NAMESPACE | 3 | ||||
-rw-r--r-- | R/distsamp.R | 17 | ||||
-rw-r--r-- | R/gof.R | 12 | ||||
-rw-r--r-- | R/plot_effects.R | 16 | ||||
-rw-r--r-- | R/plot_posteriors.R | 6 | ||||
-rw-r--r-- | R/residuals.R | 17 | ||||
-rw-r--r-- | R/spatial.R | 17 | ||||
-rw-r--r-- | man/gof.Rd | 6 | ||||
-rw-r--r-- | src/Makevars | 2 | ||||
-rw-r--r-- | src/Makevars.win | 2 |
11 files changed, 63 insertions, 42 deletions
diff --git a/DESCRIPTION b/DESCRIPTION index 1671e72..4011db0 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -12,15 +12,16 @@ Imports: gridExtra, lme4, loo, - Matrix, + Matrix (>= 1.5-0), methods, pbapply, Rcpp (>= 0.12.0), + rlang, RSpectra, rstan (>= 2.18.1), rstantools (>= 2.0.0), stats -Suggests: covr, devtools, knitr, pkgdown, raster, rmarkdown, rlang, roxygen2, testthat +Suggests: covr, devtools, knitr, pkgdown, raster, rmarkdown, roxygen2, testthat VignetteBuilder: knitr Description: Fit Bayesian hierarchical models of animal abundance and occurrence via the 'rstan' package, the R interface to the 'Stan' C++ library. @@ -34,7 +35,7 @@ License: GPL (>=3) URL: https://kenkellner.com/ubms/ BugReports: https://github.com/kenkellner/ubms/issues Encoding: UTF-8 -RoxygenNote: 7.2.0 +RoxygenNote: 7.2.2 Biarch: true LinkingTo: BH (>= 1.66.0), @@ -52,7 +52,7 @@ import(Rcpp) importClassesFrom(rstan,stanfit) importFrom(RSpectra,eigs) importFrom(ggplot2,aes) -importFrom(ggplot2,aes_string) +importFrom(ggplot2,after_stat) importFrom(ggplot2,element_blank) importFrom(ggplot2,element_rect) importFrom(ggplot2,element_text) @@ -93,6 +93,7 @@ importFrom(methods,callNextMethod) importFrom(methods,new) importFrom(pbapply,pblapply) importFrom(pbapply,pboptions) +importFrom(rlang,sym) importFrom(rstan,extract) importFrom(rstan,get_elapsed_time) importFrom(rstan,get_stancode) diff --git a/R/distsamp.R b/R/distsamp.R index 021b489..b42b8e0 100644 --- a/R/distsamp.R +++ b/R/distsamp.R @@ -417,32 +417,33 @@ setMethod("sim_fitted", "ubmsFitDistsamp", function(object, submodel, samples, . #Histogram--------------------------------------------------------------------- #' @importFrom graphics hist -#' @importFrom ggplot2 geom_histogram +#' @importFrom ggplot2 geom_histogram after_stat setMethod("hist", "ubmsFitDistsamp", function(x, draws=30, ...){ samples <- get_samples(x, draws) hist_data <- get_hist_data(x) mean_line <- get_mean_line(x) - - out <- ggplot(hist_data, aes_string(x="x")) + - geom_histogram(aes_string(y="..density.."),fill='transparent', + + xval <- sym("x"); val <- sym("val"); ind <- sym("ind"); dens <- sym("density") + out <- ggplot(hist_data, aes(x={{xval}})) + + geom_histogram(aes(y=after_stat({{dens}})),fill='transparent', col='black',breaks=x@response@dist_breaks) #Adjust the histogram height to match the density line bar_height <- ggplot2::ggplot_build(out)$data[[1]]$y[1] adj_factor <- max(mean_line$val, na.rm=TRUE) / bar_height - out <- ggplot(hist_data, aes_string(x="x")) + - geom_histogram(aes_string(y=paste0("..density..*",adj_factor)),fill='transparent', + out <- ggplot(hist_data, aes(x={{xval}})) + + geom_histogram(aes(y=after_stat({{dens}})*adj_factor),fill='transparent', col='black',breaks=x@response@dist_breaks) if(draws > 0){ sample_lines <- get_sample_lines(x, samples) out <- out + - geom_line(data=sample_lines, aes_string(x="x", y="val", group="ind"), + geom_line(data=sample_lines, aes(x={{xval}}, y={{val}}, group={{ind}}), alpha=0.3) } out + - geom_line(data=mean_line, aes_string(x="x", y="val"), col='red') + + geom_line(data=mean_line, aes(x={{xval}}, y={{val}}), col='red') + labs(x=paste0("Distance (", x@response@units_in,")"), y="Density") + theme_bw() + theme(panel.grid.major=element_blank(), panel.grid.minor=element_blank(), @@ -15,13 +15,17 @@ setMethod("show", "ubmsGOF", function(object){ #' @importFrom ggplot2 ggplot aes geom_abline geom_point theme_bw labs #' @importFrom ggplot2 facet_wrap theme element_blank element_text element_rect -#' @importFrom ggplot2 geom_label unit aes_string ggtitle +#' @importFrom ggplot2 geom_label unit ggtitle +#' @importFrom rlang sym setMethod("plot", "ubmsGOF", function(x, ...){ + obs <- sym("obs") + sim <- sym("sim") + lab <- sym("lab") ppval <- data.frame(lab=paste("P =", round(x@post_pred_p, 3))) - ggplot(x@samples, aes_string(x="obs", y="sim")) + - geom_abline(aes(intercept=0, slope=1),size=1.2, col='red') + + ggplot(x@samples, aes(x={{obs}}, y={{sim}})) + + geom_abline(aes(intercept=0, slope=1), linewidth=1.2, col='red') + geom_point(alpha=0.4) + - geom_label(data=ppval, aes_string(x=-Inf, y=Inf, label="lab"), + geom_label(data=ppval, aes(x=-Inf, y=Inf, label={{lab}}), hjust=-0.2, vjust=1.4, size=5, fill='white', label.size=0, label.padding=unit(0.1, "lines")) + diff --git a/R/plot_effects.R b/R/plot_effects.R index cf3f73a..5eb3ffa 100644 --- a/R/plot_effects.R +++ b/R/plot_effects.R @@ -98,9 +98,11 @@ marg_numeric_plot <- function(object, submodel, covariate, quant, plot_df$lower <- stats::lowess(plot_df$lower, f=smooth)[[2]] plot_df$upper <- stats::lowess(plot_df$upper, f=smooth)[[2]] } - - ggplot(data=plot_df, aes_string(x="covariate", y="mn")) + - geom_ribbon(aes_string(ymin="lower", ymax="upper"), alpha=0.3) + + + covariate <- sym("covariate"); mn <- sym("mn") + lower <- sym("lower"); upper <- sym("upper") + ggplot(data=plot_df, aes(x={{covariate}}, y={{mn}})) + + geom_ribbon(aes(ymin={{lower}}, ymax={{upper}}), alpha=0.3) + geom_line() + labs(x = covariate, y = sm@name) + plot_theme() + @@ -117,9 +119,11 @@ marg_factor_plot <- function(object, submodel, covariate, quant, draws){ plot_df <- get_margplot_data(object, submodel, covariate, quant, samples, newdata) - - ggplot(data=plot_df, aes_string(x="covariate", y="mn")) + - geom_errorbar(aes_string(ymin="lower", ymax="upper"), width=0.4) + + + covariate <- sym("covariate"); mn <- sym("mn") + lower <- sym("lower"); upper <- sym("upper") + ggplot(data=plot_df, aes(x={{covariate}}, y={{mn}})) + + geom_errorbar(aes(ymin={{lower}}, ymax={{upper}}), width=0.4) + geom_point(size=2) + labs(x = covariate, y = sm@name) + plot_theme() + diff --git a/R/plot_posteriors.R b/R/plot_posteriors.R index 65e8449..65150c2 100644 --- a/R/plot_posteriors.R +++ b/R/plot_posteriors.R @@ -52,8 +52,9 @@ setMethod("plot_posteriors", "ubmsFit", function(object, pars=NULL, density=FALS plot_dat$parameter <- factor(plot_dat$parameter, levels=pars) if(density){ + samples <- sym("samples"); chain <- sym("chain") out <- ggplot(data=plot_dat) + - stat_density(aes_string(x="samples", col="chain"), geom="line", + stat_density(aes(x={{samples}}, col={{chain}}), geom="line", position="identity", ...) + labs(x="Value", y="Density") + facet_wrap("parameter", scales="free") + @@ -61,8 +62,9 @@ setMethod("plot_posteriors", "ubmsFit", function(object, pars=NULL, density=FALS theme(strip.text=element_text(size=12), strip.background=element_rect("white")) } else { + samples <- sym("samples") out <- ggplot(data=plot_dat) + - geom_histogram(aes_string(x="samples"),fill='gray90',col='black', ...) + + geom_histogram(aes(x={{samples}}),fill='gray90',col='black', ...) + labs(x="Value", y="Count") + facet_wrap("parameter", scales="free") + plot_theme() + diff --git a/R/residuals.R b/R/residuals.R index a34b5e7..6e9d31c 100644 --- a/R/residuals.R +++ b/R/residuals.R @@ -129,8 +129,9 @@ plot_pearson_residuals <- function(x, res, xlab, name){ }) pl_dat <- do.call("rbind", pl_dat) pl_dat <- pl_dat[stats::complete.cases(pl_dat),] - - ggplot(data=pl_dat, aes_string(x="x", y="y")) + + + x <- sym("x"); y <- sym("y") + ggplot(data=pl_dat, aes(x={{x}}, y={{y}})) + geom_hline(aes(yintercept=0), linetype=2) + geom_point() + facet_wrap("ind") + @@ -145,12 +146,14 @@ plot_binned_residuals <- function(x, res, xlab, name, nbins){ get_binned_residuals(x[i,], res[i,], i, nbins)}) pl_dat <- do.call("rbind", pl_dat) pl_dat <- pl_dat[stats::complete.cases(pl_dat),] - - ggplot(data=pl_dat, aes_string(x="x_bar", y="y_bar")) + - geom_ribbon(aes_string(ymin="y_lo", ymax="y_hi"), alpha=0.1) + + + x_bar <- sym("x_bar"); y_bar <- sym("y_bar") + y_lo <- sym("y_lo"); y_hi <- sym("y_hi") + ggplot(data=pl_dat, aes(x={{x_bar}}, y={{y_bar}})) + + geom_ribbon(aes(ymin={{y_lo}}, ymax={{y_hi}}), alpha=0.1) + geom_hline(aes(yintercept=0), linetype=2) + - geom_line(aes_string(y="y_hi"), col='gray', size=1.1) + - geom_line(aes_string(y="y_lo"), col='gray', size=1.1)+ + geom_line(aes(y={{y_hi}}), col='gray', linewidth=1.1) + + geom_line(aes(y={{y_lo}}), col='gray', linewidth=1.1)+ geom_point() + facet_wrap("ind") + ggtitle(paste(name, "submodel residuals plot")) + diff --git a/R/spatial.R b/R/spatial.R index 5c76f06..bf690b9 100644 --- a/R/spatial.R +++ b/R/spatial.R @@ -75,10 +75,11 @@ plot_RSR <- function(coords, A, threshold, focal_site){ neighbors$type <- "Neighbors" focal <- rbind(focal, neighbors) } + x <- sym("x"); y <- sym("y"); type <- sym("type") - ggplot(focal, aes_string(x="x",y="y")) + + ggplot(focal, aes(x={{x}},y={{y}})) + geom_point(data=plot_dat, alpha=0.3) + - geom_point(aes_string(col="type")) + + geom_point(aes(col={{type}})) + scale_color_manual(values=c("red","blue")) + plot_theme() + theme(legend.text=element_text(size=14), @@ -278,9 +279,11 @@ plot_spatial <- function(object, param=c('state','eta'), sites=TRUE, cell_size=N est <- Kmat %*% colMeans(b) } plot_data <- cbind(as.data.frame(coords), est=est) + + x <- sym(nms[1]); y <- sym(nms[2]); est <- sym("est") - out <- ggplot(data=plot_data, aes_string(x=nms[1], y=nms[2])) + - geom_tile(aes_string(fill="est"), width=cell_size, height=cell_size) + + out <- ggplot(data=plot_data, aes(x={{x}}, y={{y}})) + + geom_tile(aes(fill={{est}}), width=cell_size, height=cell_size) + scale_fill_gradientn(colors=terrain.colors(10)) + labs(fill=param) + plot_theme() + @@ -292,15 +295,17 @@ plot_spatial <- function(object, param=c('state','eta'), sites=TRUE, cell_size=N if(inherits(object, "ubmsFitOccuTTD")){ coords_samp$obs <- as.numeric(coords_samp$obs < object@response@surveyLength) } + + obs <- sym("obs") if(inherits(object, c("ubmsFitOccu","ubmsFitOccuTTD","ubmsFitOccuRN"))){ coords_samp$obs <- factor(coords_samp$obs) - out <- out + geom_point(data=coords_samp, aes_string(col="obs"), + out <- out + geom_point(data=coords_samp, aes(col={{obs}}), size=1, pch=19) + scale_color_manual(values=c("gray","black")) + labs(color="Detected") } else { - out <- out + geom_point(data=coords_samp, aes_string(size="obs"), pch=19) + + out <- out + geom_point(data=coords_samp, aes(size={{obs}}), pch=19) + #scale_color_manual(values=c("gray","black")) + labs(size="Minimum\ncount") } @@ -32,12 +32,12 @@ checks } \section{Functions}{ \itemize{ -\item \code{gof,ubmsFitOccu-method}: Applies the MacKenzie-Bailey chi-square goodness of fit test for +\item \code{gof(ubmsFitOccu)}: Applies the MacKenzie-Bailey chi-square goodness of fit test for ocupancy models (MacKenzie and Bailey 2004). -\item \code{gof,ubmsFitAbun-method}: A goodness-of-fit test for N-mixture type models based on Pearson's chi-square. -}} +\item \code{gof(ubmsFitAbun)}: A goodness-of-fit test for N-mixture type models based on Pearson's chi-square. +}} \references{ MacKenzie, D. I., & Bailey, L. L. (2004). Assessing the fit of site-occupancy models. Journal of Agricultural, Biological, diff --git a/src/Makevars b/src/Makevars index e7a95c7..1a7240d 100644 --- a/src/Makevars +++ b/src/Makevars @@ -14,7 +14,7 @@ PKG_LIBS = $(shell "$(R_HOME)/bin$(R_ARCH_BIN)/Rscript" -e "RcppParallel::RcppPa PKG_LIBS+= $(LAPACK_LIBS) $(BLAS_LIBS) $(FLIBS) # COMMENT THIS OUT WHEN TESTING CLANG COMPILER -CXX_STD = CXX14 +# CXX_STD = CXX14 all: $(SHLIB) diff --git a/src/Makevars.win b/src/Makevars.win index a16dabe..01e474c 100644 --- a/src/Makevars.win +++ b/src/Makevars.win @@ -7,7 +7,7 @@ PKG_CXXFLAGS = $(shell "$(R_HOME)/bin$(R_ARCH_BIN)/Rscript" -e "RcppParallel::Cx PKG_LIBS = $(shell "$(R_HOME)/bin$(R_ARCH_BIN)/Rscript" -e "RcppParallel::RcppParallelLibs()") $(shell "$(R_HOME)/bin$(R_ARCH_BIN)/Rscript" -e "StanHeaders:::LdFlags()") PKG_LIBS+= $(LAPACK_LIBS) $(BLAS_LIBS) $(FLIBS) -CXX_STD = CXX14 +# CXX_STD = CXX14 all: $(SHLIB) |