【发布时间】:2021-01-19 19:48:49
【问题描述】:
我正在使用 R 编程语言并通过关注 this tutorial 了解 kohonen 包。 kohonen R 包允许用户运行 Kohonen Networks(也称为 SOM - 自组织地图),这是一种用于数据可视化的无监督机器学习算法。
我运行了以下代码并生成了以下图表:
#load libraries
library(kohonen) #fitting SOMs
library(RColorBrewer) #colors, using predefined palettes
#process data
iris_complete <-iris[complete.cases(iris),] #only complete cases... the iris dataset floats around in the sky with diamonds.
iris_unique <- unique(iris_complete) # Remove duplicates
iris.sc = scale(iris_unique[, 1:4])
#run the SOM
iris.grid = somgrid(xdim = 10, ydim=10, topo="hexagonal", toroidal = TRUE)
set.seed(33) #for reproducability
iris.som <- som(iris.sc, grid=iris.grid, rlen=700, alpha=c(0.05,0.01), keep.data = TRUE)
#make plots (3 different plots)
plot(iris.som, type="count")
plot(iris.som, type="dist.neighbours",
palette.name=grey.colors, shape = "straight")
var <- 1 #define the variable to plot
plot(iris.som,
type = "property",
property = getCodes(iris.som)[,var],
main=colnames(getCodes(iris.som))[var],
palette.name=terrain.colors)
从这里开始,我正在尝试修改这些图,使它们更易于识别。我正在尝试为每个圆圈添加一个“标签”(1-100 的数字),以便更容易识别每个圆圈:
我不确定是否有一种直接的方法可以在每个对应的圆圈上放置一个数字。查看 kohonen 包 (https://www.rdocumentation.org/packages/kohonen/versions/2.0.19/topics/som) 中的 som() 函数,似乎可以确定哪个观察属于哪个圆圈:
#determine which circle each observation belongs to
a = iris.som$unit.classif
#pull the original data
b = iris.som$data
#combine both of them into one frame
c = rbind(a,b)
但我不确定是否可以将这些数字“叠加”到相应的圆圈上。有谁知道这样可以吗?
更新:
我尝试了以下代码:
iris_unique$ID <- seq_along(iris_unique[,1])
plot(iris.som, type="mapping", bg = rgb(colour4), shape = "straight",
border = "grey", labels = iris_unique[,6])
或:
library(plotly)
plot1 = plot(iris.som, type="mapping", bg = rgb(colour4), shape = "straight",
border = "grey", labels = iris_unique[,6])
plotly_plot = ggplotly(plot1)
但我不认为这是正确的。
【问题讨论】:
-
@Z.Lin:感谢您的编辑!
标签: r plot data-visualization