Skip to content

Commit

Permalink
plot_MoranScatterplot():
Browse files Browse the repository at this point in the history
+ fx plot parameter collusion
+ ad input verification
+ up tests
  • Loading branch information
RLumSK committed Feb 8, 2025
1 parent 4a73542 commit cc0e3b5
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 17 deletions.
37 changes: 25 additions & 12 deletions R/plot_MoranScatterplot.R
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,9 @@ plot_MoranScatterplot <- function(

## Validate input arguments; set variables -----------------------
.validate_class(object, c("RLum.Results", "numeric", "integer"))
## To add:
# - should contain a numerical vector of length 100

if(inherits(object, "numeric") || inherits(object, "integer") )
.validate_length(object, 100)

## get ... arguments
plot_settings <- modifyList(
Expand Down Expand Up @@ -96,11 +97,14 @@ plot_MoranScatterplot <- function(

show_location_ids <- FALSE
show_n_neighbours <- FALSE
if(plot_settings$pch == "show_location_ids")
if(plot_settings$pch == "show_location_ids") {
show_location_ids <- TRUE
else if (plot_settings$pch == "show_n_neighbours")

} else if (plot_settings$pch == "show_n_neighbours") {
show_n_neighbours <- TRUE

}

vs_log <- plot_settings$log # because we need the function "log" later

df_moran_plot <- data.frame(
Expand Down Expand Up @@ -196,14 +200,22 @@ plot_MoranScatterplot <- function(
plot_settings$pch
}

plot(df_moran_plot[,1:2],
pch = vs_pch,
log = vs_log,
xlab = plot_settings$xlab,
ylab = plot_settings$ylab,
cex = plot_settings$cex,
...
)
## plot
## remove used arguments to avoid collusion
args <- list(...)
args <- args[!...names() %in% c("pch", "log", "xlab", "ylab", "cex", "legend")]

do.call(
what = plot,
args = c(
args,
list(
df_moran_plot[,1:2],
pch = vs_pch,
log = vs_log,
xlab = plot_settings$xlab,
ylab = plot_settings$ylab,
cex = plot_settings$cex)))

if(vs_points_appearance == "show_location_ids") {
text(x = df_moran_plot[,1:2],
Expand Down Expand Up @@ -268,3 +280,4 @@ plot_MoranScatterplot <- function(

invisible(df_moran_plot)
}

10 changes: 5 additions & 5 deletions tests/testthat/test_plot_MoranScatterplot.R
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@ test_that("input validation", {
"'df_neighbours' should be of class 'data.frame'")
expect_error(plot_MoranScatterplot(obj, df_neighbours = data.frame()),
"'df_neighbours' should be a data frame with 3 columns")
expect_error(plot_MoranScatterplot(obj, show_legend = "error"),
"'show_legend' should be of class 'logical'")
expect_error(plot_MoranScatterplot(obj, str_y_def = "error"),
"'str_y_def' should be one of 'mean_neighbours' or")
expect_error(plot_MoranScatterplot(1:10),
"'object' should have length 100")
})

test_that("check functionality", {
Expand All @@ -30,14 +30,14 @@ test_that("check functionality", {

## problematic values for the log transform
expect_warning(plot_MoranScatterplot(1:100 - 1, log = "xy",
show_legend = FALSE),
legend = FALSE),
"x-axis values rescaled because of log transform")
expect_warning(plot_MoranScatterplot(1:100 - 2, log = "xy",
show_legend = FALSE),
legend = FALSE),
"x-axis values rescaled because of log transform")

obj.na <- obj
obj.na@data$vn_values[c(24, 73)] <- NA
expect_warning(plot_MoranScatterplot(obj.na, show_legend = TRUE),
expect_warning(plot_MoranScatterplot(obj.na, legend = TRUE),
"Grain observations removed from plot because no neighbours")
})

0 comments on commit cc0e3b5

Please sign in to comment.