Skip to content

Commit fa138ca

Browse files
plot legends in som map
1 parent af82251 commit fa138ca

File tree

8 files changed

+91
-18
lines changed

8 files changed

+91
-18
lines changed

R/api_check.R

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2459,6 +2459,22 @@
24592459
discriminator = "any_of")
24602460
return(invisible(NULL))
24612461
}
2462+
#' @title Check legend defined as tibble
2463+
#' @name .check_legend
2464+
#' @param legend Legend (as tibble)
2465+
#' @return Called for side effects
2466+
#' @keywords internal
2467+
#' @noRd
2468+
.check_legend <- function(legend) {
2469+
.check_set_caller(".check_legend")
2470+
.check_chr_contains(
2471+
x = colnames(legend),
2472+
contains = c("name", "color"),
2473+
discriminator = "all_of",
2474+
msg = .conf("messages", ".check_legend")
2475+
)
2476+
return(invisible(NULL))
2477+
}
24622478
#' @title Checks legend_position
24632479
#' @name .check_legend_position
24642480
#' @param legend_position Character vector with legend position
@@ -2475,6 +2491,23 @@
24752491
)
24762492
return(invisible(NULL))
24772493
}
2494+
#' @title Checks if band is in list of bands
2495+
#' @name .check_band_in_bands
2496+
#' @param band Name of band
2497+
#' @param bands List of bands
2498+
#' @return Called for side effects
2499+
#' @keywords internal
2500+
#' @noRd
2501+
.check_band_in_bands <- function(band, bands) {
2502+
.check_set_caller("check_band_in_bands")
2503+
.check_chr_contains(
2504+
x = bands,
2505+
contains = band,
2506+
discriminator = "one_of",
2507+
msg = .conf("messages", ".check_band_in_bands")
2508+
)
2509+
return(invisible(NULL))
2510+
}
24782511
#' @title Checks shapefile attribute
24792512
#' @name .check_shp_attribute
24802513
#' @param sf_shape sf object read from a shapefile

R/api_colors.R

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -217,3 +217,19 @@
217217
}
218218
return(c4a_pal_name)
219219
}
220+
#' @title Transform an legend from tibble to vector
221+
#' @name .colors_legend_set
222+
#' @keywords internal
223+
#' @noRd
224+
#' @param legend A legend in tibble format
225+
#' @return A valid legend as vector
226+
#'
227+
.colors_legend_set <- function(legend){
228+
if ("tbl_df" %in% class(legend)) {
229+
.check_legend(legend)
230+
legend_vec <- legend[["color"]]
231+
names(legend_vec) <- legend[["name"]]
232+
return(legend_vec)
233+
}
234+
return(legend)
235+
}

R/api_som.R

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -145,24 +145,28 @@
145145
#' of the last iteration of SOM
146146
#' in function sits_cluster_som
147147
#'
148-
#' @param kohonen_obj Object kohonen
148+
#' @param koh Object kohonen
149149
#' provided by package Kohonen
150+
#' @param legend Legend (optional)
150151
#' @return kohonen_obj with a new parameter with the
151152
#' colour of the neuron.
152153
#'
153-
.som_paint_neurons <- function(kohonen_obj) {
154-
# assign one color per unique label
154+
.som_paint_neurons <- function(koh, legend = NULL) {
155155

156+
# convert legend from tibble to vector
157+
if (.has(legend))
158+
legend <- .colors_legend_set(legend)
159+
# assign one color per unique label
156160
colors <- .colors_get(
157-
labels = kohonen_obj[["neuron_label"]],
158-
legend = NULL,
161+
labels = unique(koh[["som_properties"]][["neuron_label"]]),
162+
legend = legend,
159163
palette = "Set3",
160164
rev = TRUE
161165
)
162-
labels <- kohonen_obj[["neuron_label"]]
163-
kohonen_obj[["paint_map"]] <- unname(colors[labels])
166+
labels <- koh[["som_properties"]][["neuron_label"]]
167+
koh[["som_properties"]][["paint_map"]] <- unname(colors[labels])
164168

165-
return(kohonen_obj)
169+
return(koh)
166170
}
167171

168172
#' @title Adjacency matrix

R/sits_plot.R

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1420,6 +1420,8 @@ plot.class_cube <- function(x, y, ...,
14201420
.check_int_parameter(max_cog_size, min = 512)
14211421
# check legend position
14221422
.check_legend_position(legend_position)
1423+
# check legend - convert to vector if legend is tibble
1424+
legend <- .colors_legend_set(legend)
14231425
# check for color_palette parameter (sits 1.4.1)
14241426
dots <- list(...)
14251427
# get tmap params from dots
@@ -1747,7 +1749,8 @@ plot.som_evaluate_cluster <- function(x, y, ...,
17471749
#' @param ... Further specifications for \link{plot}.
17481750
#' @param type Type of plot: "codes" for neuron weight (time series) and
17491751
#' "mapping" for the number of samples allocated in a neuron.
1750-
#' @param band What band will be plotted.
1752+
#' @param legend Legend with colors to be plotted
1753+
#' @param band What band will be plotted (character)
17511754
#'
17521755
#' @return Called for side effects.
17531756
#'
@@ -1764,23 +1767,39 @@ plot.som_evaluate_cluster <- function(x, y, ...,
17641767
#' }
17651768
#' @export
17661769
#'
1767-
plot.som_map <- function(x, y, ..., type = "codes", band = 1) {
1770+
plot.som_map <- function(x, y, ..., type = "codes", legend = NULL, band = NULL) {
17681771
stopifnot(missing(y))
17691772
koh <- x
17701773
if (!inherits(koh, "som_map")) {
17711774
message(.conf("messages", ".plot_som_map"))
17721775
return(invisible(NULL))
17731776
}
1777+
# set band
1778+
bands <- names(koh[["som_properties"]][["codes"]])
1779+
# check if band name is available
1780+
if (.has(band)) {
1781+
.check_band_in_bands(band, bands)
1782+
# create a numeric vector for plotting
1783+
bands_koh <- seq_len(length(bands))
1784+
names(bands_koh) <- bands
1785+
whatmap <- bands_koh[[band]]
1786+
} else {
1787+
whatmap <- 1
1788+
}
1789+
1790+
1791+
# paint neurons
1792+
koh <- .som_paint_neurons(koh, legend)
17741793
if (type == "mapping") {
17751794
graphics::plot(koh[["som_properties"]],
17761795
bgcol = koh[["som_properties"]][["paint_map"]],
1777-
"mapping", whatmap = band,
1796+
"mapping", whatmap = whatmap,
17781797
codeRendering = "lines"
17791798
)
17801799
} else if (type == "codes") {
17811800
graphics::plot(koh[["som_properties"]],
17821801
bgcol = koh[["som_properties"]][["paint_map"]],
1783-
"codes", whatmap = band,
1802+
"codes", whatmap = whatmap,
17841803
codeRendering = "lines"
17851804
)
17861805
}

R/sits_som.R

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -192,10 +192,6 @@ sits_som_map <- function(data,
192192
labels_max <- unlist(lab_max)
193193
# prepare a color assignment to the SOM map
194194
kohonen_obj[["neuron_label"]] <- labels_max
195-
# only paint neurons if number of labels is greater than one
196-
if (length(unique(labels_max)) > 1) {
197-
kohonen_obj <- .som_paint_neurons(kohonen_obj)
198-
}
199195
# return the som_map object
200196
som_map <-
201197
list(

inst/extdata/config_messages.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
#
33
.check_apply: "invalid function provided to be applied"
44
.check_available_bands: 'requested band(s) not available in the cube'
5+
.check_band_in_bands: "requested band(s) not available"
56
.check_bbox: "input is not a valid bbox"
67
.check_bw_rgb_bands: "either 'band' parameter or 'red', 'green', and 'blue' parameters should be informed"
78
.check_crs: "invalid crs information in image files"
@@ -54,6 +55,7 @@
5455
.check_labels: "missing labels in some or all of reference data"
5556
.check_labels_class_cube: "labels do not match number of classes in cube"
5657
.check_labels_probs_cube: "labels are not available in probs cube"
58+
.check_legend: "when defined as a tibble, legend needs name and color columns"
5759
.check_legend_position: "legend position is either inside or outside"
5860
.check_length: "invalid length for parameter"
5961
.check_lgl: "invalid logical value"

man/plot.som_map.Rd

Lines changed: 4 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

sits.Rproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
Version: 1.0
2+
ProjectId: 30147940-5ac6-4daa-88b6-6d66533383e5
23

34
RestoreWorkspace: Default
45
SaveWorkspace: Ask

0 commit comments

Comments
 (0)