55# ' @param raster SpatRaster. Raster to be sampled
66# ' @param sample numeric. Number of points to sample
77# ' @param axis character. Which axis coordinates of the raster to return
8+ # ' @param name character. The name of the raster variable
89# ' @param logger Stores all notification messages to be displayed in the Log
910# ' Window. Insert the logger reactive list here for running in
1011# ' shiny, otherwise leave the default NULL
11- # ' @return a dataframe containing the axis values and the cell values
12+ # ' @return a function that generates a scatterplot
1213# ' @author Simon Smart <simon.smart@@cantab.net>
1314# ' @examples
1415# ' if (check_suggests(example = TRUE)) {
1516# ' raster <- terra::rast(ncol = 8, nrow = 8)
1617# ' raster[] <- sapply(1:terra::ncell(raster), function(x){
1718# ' rnorm(1, ifelse(x %% 8 != 0, x %% 8, 8), 3)})
18- # ' scatterplot <- plot_scatter(raster, sample = 10, axis = "y")
19+ # ' scatterplot <- plot_scatter(raster, sample = 10, axis = "Longitude", name = "Example")
20+ # ' scatterplot()
1921# ' } else {
2022# ' message('reinstall with install.packages("shinyscholar", dependencies = TRUE)
2123# ' to run this example')
2224# ' }
2325# ' @export
24- plot_scatter <- function (raster , sample , axis , logger = NULL ) {
26+ plot_scatter <- function (raster , sample , axis , name , logger = NULL ) {
2527
2628 check_suggests()
2729
@@ -35,13 +37,18 @@ plot_scatter <- function(raster, sample, axis, logger = NULL) {
3537 return ()
3638 }
3739
38- if (! (axis %in% c(" x " , " y " ))){
39- logger | > writeLog(type = " error" , " axis must be either x or y " )
40+ if (! (axis %in% c(" Longitude " , " Latitude " ))){
41+ logger | > writeLog(type = " error" , " axis must be either Longitude or Latitude " )
4042 return ()
4143 }
4244
43- samp <- terra :: spatSample(raster , sample , method = " random" , xy = TRUE , as.df = TRUE )
44- colnames(samp )[3 ] <- " value"
45- samp [, c(axis , " value" )]
45+ if (axis == " Longitude" ){short_axis <- " x" } else {short_axis <- " y" }
46+
47+ sampled <- terra :: spatSample(raster , sample , method = " random" , xy = TRUE , as.df = TRUE )
48+ colnames(sampled )[3 ] <- " value"
49+ sampled <- sampled [, c(short_axis , " value" )]
50+
51+ function (){plot(sampled [[1 ]], sampled [[2 ]], xlab = axis , ylab = name )}
52+
4653
4754}
0 commit comments