【问题标题】:R package Kohonen - how to plot hexagons instead of circles as in Matlab SOM toolbox?R package Kohonen - 如何在 Matlab SOM 工具箱中绘制六边形而不是圆形?
【发布时间】:2013-11-20 10:38:46
【问题描述】:

是否可以用六边形绘制 SOM 地图(包 Kohonen) 作为基本形式而不是圆形?现在不同的绘图 (计数、变化等)被绘制为带有白色环境的圆圈。

目的是创建类似于 Matlab SOM Toolbox 的 SOM 视觉效果。

discussion 表明 2010 年不可能进行六边形绘图。

【问题讨论】:

    标签: r plot som


    【解决方案1】:

    您可以使用 Voronoi 图来获得填充空间的表示(六边形)。

    • 十六进制网格坐标存储在kohonen对象的$grid$pts中,
    • 代码在$codes

    将其与lattice 情节放在一起:

    require ("kohonen")
    require ("latticeExtra")
    require ("deldir")
    som.wines <- som (scale (wines), grid = somgrid(5, 5, "hexagonal"))
    
    df <- as.data.frame (cbind (som.wines$grid$pts, som.wines$codes))
    levelplot (alcohol ~ x * y, data = df, panel = panel.voronoi, aspect = "iso)
    

    产量:

    (我不确定,但我想这在 2010 年就已经可以实现了)。

    【讨论】:

    • 谢谢!我能够使用您的代码来改进 SOM 的视觉效果。
    【解决方案2】:

    即使已经有了答案,也许这种不同的方法会是一个很好的补充。使用代码,您可以创建类似于下面的热图

    我从围绕 kohonen 包编写的更广泛的程序中获取了这段代码。你可以在这里找到我对程序的完整解释:http://nbremer.blogspot.nl/2013/07/on-creation-of-extended-self-organizing.html

    这是一个位编码,您需要从一种特定类型的变量开始:输入&lt;&lt;Heatmap matrix variable&gt;&gt; 是一个矩阵,它将是您的热图的数字表示。 这里 [1,1] 将成为左下节点(第 1 行第 1 列),[1,2] 将成为右侧节点,[2,1] 将成为第二行左侧第一个节点, 等等。 因此,在视觉上,您在热图中从左下到右上工作,而在矩阵中,您从左上到右下工作

    library(RColorBrewer) #to use brewer.pal
    library(fields) #to use designer.colors
    
    #Function to create the polygon for each hexagon
    Hexagon <- function (x, y, unitcell = 1, col = col) {
      polygon(c(x, x, x + unitcell/2, x + unitcell, x + unitcell, 
                x + unitcell/2), c(y + unitcell * 0.125, y + unitcell * 
                                   0.875, y + unitcell * 1.125, y + unitcell * 0.875, 
                                   y + unitcell * 0.125, y - unitcell * 0.125), 
              col = col, border=NA)
    }#function
    
    #Start with a matrix that would be the numerical representation of you heatmap
    #Here [1,1] will become the lower left node (1st row, 1st column), 
    #[1,2] will become the node to the right
    #[2,1] will be the first node to the left in the second row
    #So visually you work your way from bottom left to top right
    x <- as.vector(<<Heatmap matrix variable>>)
    
    #Number of rows and columns of your SOM
    SOM_Rows <- dim(<<Heatmap matrix variable>>)[1]
    SOM_Columns <- dim(<<Heatmap matrix variable>>)[2]
    
    #To make room for the legend
    par(mar = c(0.4, 2, 2, 7))
    
    #Initiate the plot window but do show any axes or points on the plot
    plot(0, 0, type = "n", axes = FALSE, xlim=c(0, SOM_Columns), 
         ylim=c(0, SOM_Rows), xlab="", ylab= "", asp=1)
    
    #Create the color palette 
    #I use designer.colors to interpolate 50 colors between 
    #the maxmimum number of allowed values in Brewer 
    ColRamp <- rev(designer.colors(n=50, col=brewer.pal(9, "Spectral")))
    
    #Make a vector with length(ColRamp) number of bins between the minimum and 
    #maximum value of x. 
    #Next match each point from x with one of the colors in ColorRamp
    ColorCode <- rep("#FFFFFF", length(x)) #default is all white
    Bins <- seq(min(x, na.rm=T), max(x, na.rm=T), length=length(ColRamp))
    for (i in 1:length(x))
        if (!is.na(x[i])) ColorCode[i] <- ColRamp[which.min(abs(Bins-x[i]))] 
    
    #Actual plotting of hexagonal polygons on map
    offset <- 0.5 #offset for the hexagons when moving up a row
    for (row in 1:SOM_Rows) {
      for (column in 0:(SOM_Columns - 1)) 
         Hexagon(column + offset, row - 1, col = ColorCode[row + SOM_Rows * column])
      offset <- ifelse(offset, 0, 0.5)
    }
    
    #Add legend to the right if you want to
    image.plot(legend.only=TRUE, col=ColRamp, zlim=c(min(x, na.rm=T), max(x, na.rm=T)))
    

    【讨论】:

    • 非常感谢您这样做@NBremer。你的代码不能更全面地发布,这仍然是一个很大的遗憾。
    • 将 soms 可视化为六边形与圆形之间有什么根本区别吗?
    【解决方案3】:

    我没有足够的声誉来评论@NBremer 的回答。我已经扩展了上面的代码,以允许可视化大型组件平面和 u 矩阵,以便从 R“kohonen”库中输出。此处提供代码和工作示例:

    R SOM Visualization Functions

    【讨论】:

    • 太棒了!这很容易使用并且看起来不错。是否可以创建一个 kohonenViz 包,例如用于可视化关联规则的 arulesViz?我认为很多人都会从您的解决方案中受益。
    • 在可视化 SOMS 时,圆形和六边形之间有什么根本区别吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-03-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多