Skip to content

Commit

Permalink
fixed #811 single value subissue
Browse files Browse the repository at this point in the history
  • Loading branch information
mtennekes committed Jan 29, 2024
1 parent f76e8ca commit 43e49d7
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 4 deletions.
7 changes: 4 additions & 3 deletions R/tmapScaleIntervals.R
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,15 @@ tmapScaleIntervals = function(x1, scale, legend, o, aes, layer, layer_args, sort
cls = data_class(x1)
maincls = class(scale)[1]

if (attr(cls, "unique") && is.null(scale$breaks)) stop("Unique value, so cannot determine intervals scale range. Please specify breaks.", call. = FALSE)


if (cls[1] != "num") {
if (!is.factor(x1)) x1 = as.factor(x1)
x1 = as.integer(x1)
warning(maincls, " is supposed to be applied to numerical data", call. = FALSE)
}


x1 = without_units(x1)

if (aes %in% c("pattern")) stop("tm_scale_intervals cannot be used for layer ", layer, ", aesthetic ", aes, call. = FALSE)
Expand All @@ -21,7 +23,6 @@ tmapScaleIntervals = function(x1, scale, legend, o, aes, layer, layer_args, sort
with(scale, {
udiv = identical(use_div(breaks, midpoint), TRUE)


if (all(is.na(x1))) return(tmapScale_returnNA(n = length(x1), legend = legend, value.na = value.na, label.na = label.na, label.show = label.show, na.show = legend$na.show, sortRev = sortRev, bypass_ord = bypass_ord))

if (!any(style == c("pretty", "log10_pretty", "fixed"))) {
Expand Down Expand Up @@ -52,7 +53,7 @@ tmapScaleIntervals = function(x1, scale, legend, o, aes, layer, layer_args, sort
breaks[length(breaks)] = breaks[length(breaks)] + 1L
}

q <- num2breaks(x=x1, n=n, style=style, breaks=breaks, interval.closure=interval.closure, var=paste(layer, aes, sep = "-"), as.count = as.count, args = style.args)
q = num2breaks(x=x1, n=n, style=style, breaks=breaks, interval.closure=interval.closure, var=paste(layer, aes, sep = "-"), as.count = as.count, args = style.args)

breaks = q$brks
nbrks = length(breaks)
Expand Down
12 changes: 11 additions & 1 deletion R/tmapScale_.R
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ data_class = function(x, check_for_color_class = FALSE) {
attr(cls, "units") = if (inherits(x, "units")) {
paste0(" [", units(x), "]")
} else ""
attr(cls, "unique") = (length(unique(x)) == 1)
attr(cls, "unique") = (length(na.omit(unique(x)))==1)
cls
}

Expand All @@ -145,6 +145,16 @@ tmapScaleAuto = function(x1, scale, legend, o, aes, layer, layer_args, sortRev,
sc = "bivariate"
} else if (cls[1] == "asis") {
sc = "asis"
} else if (attr(cls, "unique")) {
if ("num" %in% cls) {
sc = "ordinal"
message("The visual variable \"", aes, "\" of the layer, \", layer", "\" contains a unique value. Therefore a discrete scale is applied (tm_scale_discrete).")
} else {
sc = "categorical"
message("The visual variable \"", aes, "\" of the layer, \", layer", "\" contains a unique value. Therefore a categorical scale is applied (tm_scale_categorical).")
}


} else {
sc_opt = getAesOption("scales.var", o, aes, layer, cls = cls)
sc_pref = scale$fun_pref
Expand Down
27 changes: 27 additions & 0 deletions sandbox/issues.R
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,14 @@ tm_shape(lc) +
tm_raster()


## issue Rapsodia86

library(terra)
rast_in <- rast("sandbox/Raster_test_tmap.tif")
rast_in[rast_in==0] <- NA
plot(rast_in)
tm_shape(rast_in) +tm_raster()

# 819
L7file = system.file("tif/L7_ETMs.tif", package = "stars")
L7 = read_stars(L7file)
Expand Down Expand Up @@ -419,3 +427,22 @@ tm_shape(s) + tm_raster()
mapview::mapview(s)
plot(s)
plot(s)


## 820
library(sf)
#> Linking to GEOS 3.11.2, GDAL 3.7.2, PROJ 9.3.0; sf_use_s2() is TRUE
system.file("gpkg/nc.gpkg", package = "sf") |>
read_sf() |>
st_transform('EPSG:32119') -> nc.32119
tm_shape(nc.32119) + tm_polygons(c("SID74", "SID79"), title="SIDS") +
tm_layout(legend.outside=TRUE, panel.labels=c("1974-78", "1979-84")) +
tm_facets(free.scales=FALSE)


tm_shape(nc.32119) + tm_polygons(c("SID74", "SID79"), fill.free = FALSE, fill.legend = tm_legend(title = "SIDS")) +
tm_layout(legend.outside=TRUE, panel.labels=c("1974-78", "1979-84")) +
tm_facets()


##

0 comments on commit 43e49d7

Please sign in to comment.