【问题标题】:Labelling Points on a Plot (R Language)在图上标记点(R 语言)
【发布时间】: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


【解决方案1】:

您可以在iris.som$grid$pts 中找到每个圆或六边形的中心坐标。将标签放在坐标上的一种方法是使用dimnames() 为每个圆圈指定一个名称,然后使用text() 将名称作为相应圆圈坐标上的标签。

iris.som$grid$pts
#          x         y
#  [1,]  1.5 0.8660254
#  [2,]  2.5 0.8660254
#  [3,]  3.5 0.8660254
#  [4,]  4.5 0.8660254
#  [5,]  5.5 0.8660254
#  up to [100,] 10.0 8.6602540

# Assign a name for each coordinate. For example, "1", "2", "3", etc

dimnames(iris.som$grid$pts) = list(as.character(1:100), c("x","y"))  

#iris.som$grid$pts
#       x         y
#1    1.5 0.8660254
#2    2.5 0.8660254
#3    3.5 0.8660254
#4    4.5 0.8660254
#5    5.5 0.8660254

# Put the names on the plot 
text(iris.som$grid$pts, dimnames(iris.som$grid$pts[[1]]))

# Do the same steps for other plots
plot(iris.som, type="dist.neighbours", 
     palette.name=grey.colors, shape = "straight")
text(iris.som$grid$pts, dimnames(iris.som$grid$pts[[1]]))

请注意,如果您使用 RStudio,您可能需要调整绘图窗格的宽度以正确显示所有标签。

【讨论】:

  • 感谢您的回复!太棒了!我唯一想问的是:是否可以替换这一行 "dimnames(iris.som$grid$pts) = list(as.character(1:100), c("x","y"))"用这一行“iris.som$unit.classif”? SOM 算法将每个数据点(来自 iris 文件)分配到这些“圆圈”之一。我试图让“iris.som$unit.classif”中的相应数字与圆圈上的数字相匹配。是否有可能做到这一点?感谢您的所有帮助!
  • 这里有一个相关问题:stackoverflow.com/questions/65864333/…
  • 假设我想查看“91 圈”中的哪些观察结果(基于视觉输出)。你会怎么做?
  • @Noob 抱歉来晚了。很难确定哪些观察属于哪个圈子。我最好的猜测是获取“iris.som$unit.classif”中数字(例如“91”)的索引(位置),然后检查“iris.sc”中的相应索引
猜你喜欢
  • 2012-03-18
  • 1970-01-01
  • 1970-01-01
  • 2016-01-11
  • 1970-01-01
  • 2019-09-30
  • 2021-08-16
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多