# Simple example:
x <- array(1:(20 * 10), dim = c(lat = 10, lon = 20)) / 200
a <- x * 0.6
b <- (1 - x) * 0.6
c <- 1 - (a + b)
lons <- seq(0, 359.5, length = 20)
lats <- seq(-89.5, 89.5, length = 10)
VizMostLikelyQuantileMap(list(a, b, c), lons, lats,
toptitle = 'Most likely tercile map',
bar_titles = paste('% of belonging to', c('a', 'b', 'c')),
brks = 20, width = 10, height = 8,
bar_extra_margin = c(1, 0, 1.5, 0))
# More complex example:
# 1. Generation of sample data
lons <- seq(0, 359.5, length = 40)
lats <- seq(-89.5, 89.5, length = 20)
# Generate sample data (with dimensions time, lat, lon)
sample_data <- sample(1:5, 50 * 20 * 40, replace = TRUE)
dim(sample_data) <- c(time = 50, lat = 20, lon = 40)
# 2. Binning sample data
n_bins <- 4
prob_thresholds <- 1:n_bins / n_bins
prob_thresholds <- prob_thresholds[1:(n_bins - 1)]
thresholds <- quantile(sample_data, prob_thresholds)
binning <- function(x, thresholds) {
n_samples <- length(x)
n_bins <- length(thresholds) + 1
thresholds <- c(thresholds, max(x))
result <- 1:n_bins
lower_threshold <- min(x) - 1
for (i in 1:n_bins) {
result[i] <- sum(x > lower_threshold & x <= thresholds[i]) / n_samples
lower_threshold <- thresholds[i]
}
dim(result) <- c(bin = n_bins)
result
}
bins <- apply(sample_data, 2:3, binning, thresholds)
names(dim(bins))[1] <- "bin"
VizMostLikelyQuantileMap(bins, lons, lats,
toptitle = 'Most likely quantile map',
bar_titles = paste('% of belonging to', letters[1:n_bins]),
brks = 20, width = 10, height = 10,
bar_extra_margin = c(1, 0, 1.5, 0))
Run the code above in your browser using DataLab