Skip to content

Commit 518da6f

Browse files
committed
New criteria to identify classes, and better legend format
1 parent 923a365 commit 518da6f

1 file changed

Lines changed: 13 additions & 5 deletions

File tree

R/plot_layer_map_layer.R

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -244,7 +244,9 @@ plot_layer <- function(
244244
highestValue <- NULL
245245
}
246246
}
247-
if(all(foundLevels %in% lowerLimits)){ # already classes -> as factor
247+
if(all(foundLevels %in% lowerLimits) & # probably classes -> as factor
248+
all(as.integer(lowerLimits) == lowerLimits)
249+
){
248250
LegendText <- foundLevels
249251
terra::values(r_wgs84)[!is.na(v)] <- factor(v[!is.na(v)])
250252
} else { # numeric data -> derive classes
@@ -275,7 +277,6 @@ plot_layer <- function(
275277
include.lowest = TRUE)
276278
foundLevels <- c(1,2)
277279
}
278-
279280
}
280281

281282
bbox <- matrix(data = terra::ext(r_wgs84),
@@ -311,13 +312,20 @@ plot_layer <- function(
311312

312313
if(plotLegend){
313314
if(length(foundLevels) > 15){
314-
foundLevels <- pretty(foundLevels, n = 15)
315+
foundLevels <- pretty(foundLevels, n = 15, shrink.sml = 0.75)
316+
foundLevels <- foundLevels[
317+
!(foundLevels < 1 | foundLevels > length(LegendText))
318+
]
319+
foundLevels[1] <- 1
320+
foundLevels[length(foundLevels)] <- length(LegendText)
321+
LegendText <- LegendText[foundLevels]
322+
classColors <- classColors[foundLevels]
315323
}
316324
legend(
317325
x = par("usr")[2] + diff(par("usr")[1:2]) / diff(par("plt")[1:2]) * (1 - par("plt")[2]),
318326
y = par("usr")[4] + diff(par("usr")[3:4]) / diff(par("plt")[3:4]) * (1 - par("plt")[4]),
319-
legend = LegendText[foundLevels],
320-
fill = classColors[foundLevels],
327+
legend = LegendText,
328+
fill = classColors,
321329
bg = rgb(1,1,1, 0.5),
322330
title = legendTitle,
323331
box.col = rgb(1,1,1, 0.5),

0 commit comments

Comments
 (0)