Skip to content

Commit

Permalink
Fixing ggplot aes_string
Browse files Browse the repository at this point in the history
  • Loading branch information
rvalavi committed Jan 27, 2023
1 parent 439176d commit df47801
Show file tree
Hide file tree
Showing 8 changed files with 639 additions and 29 deletions.
1 change: 1 addition & 0 deletions .Rbuildignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@
^Meta$
cran_comments.md
.github/
^LICENSE\.md$
4 changes: 2 additions & 2 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ Package: blockCV
Type: Package
Title: Spatial and Environmental Blocking for K-Fold and LOO Cross-Validation
Version: 3.0-0
Date: 2023-01-01
Date: 2023-01-28
Authors@R: c(person("Roozbeh", "Valavi", role = c("aut", "cre"),
email = "[email protected]"),
person("Jane", "Elith", role = "aut",
Expand All @@ -17,7 +17,7 @@ URL: https://github.com/rvalavi/blockCV
BugReports: https://github.com/rvalavi/blockCV/issues
Maintainer: Roozbeh Valavi <[email protected]>
Description: Creating spatially or environmentally separated folds for cross-validation to provide a robust error estimation in spatially structured environments; Investigating and visualising the effective range of spatial autocorrelation in continuous raster covariates and point samples to find an initial realistic distance band to separate training and testing datasets spatially described in Valavi, R. et al. (2019) <doi:10.1111/2041-210X.13107>.
License: GPL-3
License: GPL (>= 3)
Encoding: UTF-8
LazyData: true
Depends:
Expand Down
595 changes: 595 additions & 0 deletions LICENSE.md

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,5 @@ export(spatialBlock)
import(sf)
importFrom(Rcpp,sourceCpp)
importFrom(grDevices,gray.colors)
importFrom(rlang,.data)
useDynLib(blockCV, .registration = TRUE)
19 changes: 12 additions & 7 deletions R/cv_explorer.R
Original file line number Diff line number Diff line change
Expand Up @@ -108,10 +108,9 @@ cv_block_size <- function(r, # priority
colnames(map_df) <- c("x", "y", "value")

base_plot <- ggplot2::ggplot() +
ggplot2::geom_tile(data = map_df,
ggplot2::aes_string(x = "x",
y = "y",
fill = "value")) +
ggplot2::geom_tile(
data = map_df,
ggplot2::aes(x = .data$x, y = .data$y, fill = .data$value)) +
ggplot2::scale_fill_gradientn(colours = gray.colors(20, alpha = 1)) +
ggplot2::guides(fill = "none")

Expand All @@ -122,7 +121,8 @@ cv_block_size <- function(r, # priority
if(!is.null(x)){
geom_x <- ggplot2::geom_sf(
data = x,
ggplot2::aes_string(colour = switch(!is.null(column), column, NULL)),
switch(!is.null(column), ggplot2::aes(colour = {{ column }}), NULL),
# ggplot2::aes(colour = {{ switch(!is.null(column), column, NULL) }}),
inherit.aes = FALSE,
alpha = 0.5
)
Expand Down Expand Up @@ -154,9 +154,14 @@ cv_block_size <- function(r, # priority
)

# create shiny server and main code
server <- function(input, output){
server <- function(input, output, session){
output$ggplot <- shiny::renderPlot({

# stop app after session ends
session$onSessionEnded(function() {
shiny::stopApp()
})

plot_size <- if(sf::st_is_longlat(x_obj)) round(input$num) / 111325 else round(input$num)
vis_block <- sf::st_make_grid(x_obj, cellsize = plot_size, what = "polygons")

Expand All @@ -178,5 +183,5 @@ cv_block_size <- function(r, # priority
})
}
# starting the shiny app
shiny::shinyApp(ui = ui, server = server)
shiny::shinyApp(ui = ui, server = server, )
}
35 changes: 21 additions & 14 deletions R/cv_plot.R
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
#' @param remove_na logical; whether to remove excluded points in \code{cv_buffer} from the plot
#'
#' @importFrom grDevices gray.colors
#' @importFrom rlang .data
#' @return a ggplot object
#' @export
#'
Expand Down Expand Up @@ -83,8 +84,9 @@ cv_plot <- function(
na.rm = TRUE)
colnames(map_df) <- c("x", "y", "value")

geom_rast <- ggplot2::geom_tile(data = map_df,
ggplot2::aes_string(x="x", y="y", fill="value"))
geom_rast <- ggplot2::geom_tile(
data = map_df,
ggplot2::aes(x = .data$x, y = .data$y, fill = .data$value))
geom_rast_col <- ggplot2::scale_fill_gradientn(colours = raster_colors)
}
# make geom_sf for spatial blocks
Expand All @@ -100,18 +102,16 @@ cv_plot <- function(

if(!missing(x)){
x_long <- .x_to_long(x, cv, num_plot = num_plots)

# exclude NAs from cv_buffer
if(methods::is(cv, "cv_buffer") && remove_na){
# if(methods::is(cv, "cv_buffer") && remove_na){
if(.is_loo(cv) && remove_na){
x_long <- x_long[which(complete.cases(x_long$value)), ]
}

} else{
# stop if x is missing for buffer and cluster
if(!methods::is(cv, "cv_spatial")) stop("'x' is required for plotting cv_cluster and cv_buffer.")
}


if(missing(x)){
if(methods::is(cv, "cv_spatial")){

Expand All @@ -122,8 +122,9 @@ cv_plot <- function(
fill = "orangered4",
alpha = 0.04,
size = 0.2) +
ggplot2::geom_sf_text(ggplot2::aes_string(label = "folds"),
size = label_size, fun.geometry = sf::st_centroid) +
ggplot2::geom_sf_text(
ggplot2::aes(label = .data$folds),
size = label_size, fun.geometry = sf::st_centroid) +
ggplot2::labs(x = "", y = "") + # or set the axes labes to NULL
ggplot2::scale_x_continuous(guide = ggplot2::guide_axis(check.overlap = TRUE)) +
ggplot2::theme_minimal() +
Expand All @@ -137,10 +138,10 @@ cv_plot <- function(
switch(!is.null(r), geom_rast, NULL) +
switch(!is.null(r), geom_rast_col, NULL) +
switch(methods::is(cv, "cv_spatial"), geom_poly, NULL) +
ggplot2::geom_sf(ggplot2::aes_string(col = "value"),
ggplot2::geom_sf(ggplot2::aes(col = .data$value),
alpha = points_alpha) +
ggplot2::scale_color_manual(values = points_colors, na.value = "#BEBEBE03") +
ggplot2::facet_wrap(~folds, nrow = nrow, ncol = ncol) +
ggplot2::facet_wrap(~.data$folds, nrow = nrow, ncol = ncol) +
ggplot2::labs(x = "", y = "", col = "") + # set the axes labes to NULL
ggplot2::theme_bw() +
ggplot2::guides(fill = "none")
Expand All @@ -151,6 +152,11 @@ cv_plot <- function(
}


# is it a LOO CV object?
.is_loo <- function(x){
methods::is(x, "cv_buffer") || methods::is(x, "cv_nndm")
}

# transform x and fold numbers for plotting
.x_to_long <- function(x, cv, num_plot=1:10){
# get the folds list
Expand All @@ -171,13 +177,14 @@ cv_plot <- function(
num_plot <- num_plot[num_plot <= k]
}
# get the length of unique ids
if(methods::is(cv, "cv_buffer")){
# if(methods::is(cv, "cv_buffer")){
if(.is_loo(cv)){
len <- length(unique(unlist(cv$folds_list)))
} else{
len <- length(unlist(folds_list[[1]]))
if(len != nrow(x)){
stop("Number of rows in 'x' does not match the folds in 'cv'!")
}
}
if(len != nrow(x)){
stop("Number of rows in 'x' does not match the folds in 'cv'!")
}
# create a dataframe temp
df <- data.frame(id = seq_len(len))
Expand Down
5 changes: 3 additions & 2 deletions R/cv_similarity.R
Original file line number Diff line number Diff line change
Expand Up @@ -140,8 +140,9 @@ cv_similarity <- function(cv,
# which geom to choose
geom_exta <- if(methods::is(cv, "cv_buffer")) goem_buffer else geom_other

p1 <- ggplot2::ggplot(data = mes_reshp,
ggplot2::aes_string(x = "folds", y = "value", col = "value")) +
p1 <- ggplot2::ggplot(
data = mes_reshp,
ggplot2::aes(x = .data$folds, y = .data$value, col = .data$value)) +
ggplot2::geom_hline(yintercept = 0, color = "grey50", linetype = 2) +
geom_exta +
switch(!methods::is(cv, "cv_buffer"), geom_vio, NULL) +
Expand Down
8 changes: 4 additions & 4 deletions R/cv_spatial_autocor.R
Original file line number Diff line number Diff line change
Expand Up @@ -285,10 +285,10 @@ summary.cv_spatial_autocor <- function(object, ...){
# stat = "identity", data = vario_data,) +
ggplot2::geom_point(size = 4) +
ggplot2::geom_segment(
ggplot2::aes_string(x = "layers",
xend = "layers",
y = 0,
yend = "range"),
ggplot2::aes(x = .data$layers,
xend = .data$layers,
y = 0,
yend = .data$range),
size = 1.5
) +
ggplot2::labs(x = "Variables", y = "Range (km)") +
Expand Down

0 comments on commit df47801

Please sign in to comment.