# Install packages
if (!requireNamespace("data.table", quietly = TRUE)) {
install.packages("data.table")
}
if (!requireNamespace("jsonlite", quietly = TRUE)) {
install.packages("jsonlite")
}
if (!requireNamespace("kohonen", quietly = TRUE)) {
install.packages("kohonen")
}
# Load packages
library(data.table)
library(jsonlite)
library(kohonen)Easy SOM
Note
Hiplot website
This page is the tutorial for source code version of the Hiplot Easy SOM plugin. You can also use the Hiplot website to achieve no code ploting. For more information please see the following link:
Establish the SOM model and conduct the visulization.
Setup
System Requirements: Cross-platform (Linux/MacOS/Windows)
Programming language: R
Dependent packages:
data.table;jsonlite;kohonen
sessioninfo::session_info("attached")β Session info βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
setting value
version R version 4.5.2 (2025-10-31)
os Ubuntu 24.04.3 LTS
system x86_64, linux-gnu
ui X11
language (EN)
collate C.UTF-8
ctype C.UTF-8
tz UTC
date 2026-01-17
pandoc 3.1.3 @ /usr/bin/ (via rmarkdown)
quarto 1.8.27 @ /usr/local/bin/quarto
β Packages βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
package * version date (UTC) lib source
data.table * 1.18.0 2025-12-24 [1] RSPM
jsonlite * 2.0.0 2025-03-27 [1] RSPM
kohonen * 3.0.12 2023-06-09 [1] RSPM
[1] /home/runner/work/_temp/Library
[2] /opt/R/4.5.2/lib/R/site-library
[3] /opt/R/4.5.2/lib/R/library
* ββ Packages attached to the search path.
ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
Data Preparation
# Load data
data <- data.table::fread(jsonlite::read_json("https://hiplot.cn/ui/basic/easy-som/data.json")$exampleData$textarea[[1]])
data <- as.data.frame(data)
# convert data structure
target <- data[,1]
target <- factor(target, levels = unique(target))
data <- data[,-1]
data <- as.data.frame(data)
for (i in 1:ncol(data)) {
data[,i] <- as.numeric(data[,i])
}
data <- as.matrix(data)
set.seed(7)
kohmap <- xyf(scale(data), target, grid = somgrid(xdim=6, ydim=4, topo="hexagonal"), rlen=100)
color_key <- c("#A50026","#D73027","#F46D43","#FDAE61","#FEE090","#FFFFBF","#E0F3F8",
"#ABD9E9","#74ADD1","#4575B4","#313695")
colors <- function (n, alpha, rev = FALSE) {
colorRampPalette(color_key)(n)
}
# View data
head(data[,1:5]) alcohol malic acid ash ash alkalinity magnesium
[1,] 12.86 1.35 2.32 18.0 122
[2,] 12.88 2.99 2.40 20.0 104
[3,] 12.81 2.31 2.40 24.0 98
[4,] 12.70 3.55 2.36 21.5 106
[5,] 12.51 1.24 2.25 17.5 85
[6,] 12.60 2.46 2.20 18.5 94
Visualization
# Easy SOM
p <- function () {
par(mfrow = c(3,2))
xyfpredictions <- classmat2classvec(getCodes(kohmap, 2))
plot(kohmap, type="counts", col = as.integer(target),
palette.name = colors,
pchs = as.integer(target),
main = "Counts plot", shape = "straight", border = NA)
som.hc <- cutree(hclust(object.distances(kohmap, "codes")), 3)
add.cluster.boundaries(kohmap, som.hc)
plot(kohmap, type="mapping",
labels = as.integer(target), col = colors(3)[as.integer(target)],
palette.name = colors,
shape = "straight",
main = "Mapping plot")
## add background colors to units according to their predicted class labels
xyfpredictions <- classmat2classvec(getCodes(kohmap, 2))
bgcols <- colors(3)
plot(kohmap, type="mapping", col = as.integer(target),
pchs = as.integer(target), bgcol = bgcols[as.integer(xyfpredictions)],
main = "Another mapping plot", shape = "straight", border = NA)
similarities <- plot(kohmap, type="quality", shape = "straight",
palette.name = colors)
plot(kohmap, type="codes", shape = "straight",
main = c("Codes X", "Codes Y"), palette.name = colors)
}
p()
