gpt4 book ai didi

r - 在图上标记点(R 语言)

转载 作者:行者123 更新时间:2023-12-04 08:14:05 25 4
gpt4 key购买 nike

我正在使用 R 编程语言并正在学习 kohonen按以下方式打包 this tutorial . kohonen R 包允许用户运行 Kohonen Networks(也称为 SOM - 自组织 map ),这是一种用于数据可视化的无监督机器学习算法。
我运行了以下代码并生成了以下图:

#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)
from https://imgur.com/a/fQlv74X
从这里开始,我试图修改这些图,以便它们更容易识别。我正在尝试为每个圆圈添加一个“标签”(1-100 之间的数字),以便更容易识别每个圆圈:
from https://imgur.com/a/GxuPp5J
我不确定是否有一种直接的方法可以在每个相应的圆圈上放置一个数字。看着 som() kohonen 包( https://www.rdocumentation.org/packages/kohonen/versions/2.0.19/topics/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)
但我不认为这是正确的。

最佳答案

您可以在 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,您可能需要调整绘图 Pane 的宽度以正确显示所有标签。
enter image description here
enter image description here

关于r - 在图上标记点(R 语言),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65798482/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com