简体   繁体   English

如何从 fviz_dist 中生成的有序相异图像中获取矩阵?

[英]How to obtain the matrix from the ordered dissimilarity image generated in fviz_dist?

I'm trying to obtain the matrix (Ordered dissimilarity matrix) from the ggplot that is generated with the function fviz_dist from factoextra package.我试图获得与该函数产生的ggplot基质(有序相异矩阵) fviz_distfactoextra包。

From my original data I generated a distance correlation matrix using dis.cor (I need to use spearman correlation coefficient), as follows:从我的原始数据中,我使用 dis.cor 生成了一个距离相关矩阵(我需要使用 spearman 相关系数),如下所示:

dist.cor<-get_dist(b, method = "spearman")

c<-round(as.matrix(dist.cor)[1:nrow(b),1:nrow(b)],1)

But this matrix is not ordered so you can't visualize the clusters.但是这个矩阵没有排序,所以你不能可视化集群。

Then, with function fviz_dist I can generate the ggplot of the Distance Correlation Matrix (ordered):然后,使用函数 fviz_dist 我可以生成距离相关矩阵的 ggplot(有序):

fviz_dist(dist.cor)

But I don't need the image, I need that ordered matrix in a dataframe or matrix format so I can export it using write.csv and open it in excel to work with it.但我不需要图像,我需要数据框或矩阵格式的有序矩阵,以便我可以使用 write.csv 导出它并在 excel 中打开它以使用它。

Thanks!谢谢!

You can extract the data from the ggplot object:您可以从 ggplot 对象中提取数据:

library(factoextra)
library(tidyr)
dist.cor = get_dist(mtcars,method="spearman")
g = fviz_dist(dist.cor)

在此处输入图片说明

The variable names are already factored and ordered:变量名称已经分解和排序:

head(g$data)
             Var1        Var2       value
1     Volvo 142E- Volvo 142E- 0.000000000
2      Fiat X1-9- Volvo 142E- 0.006904570
3       Fiat 128- Volvo 142E- 0.006904570
4 Toyota Corolla- Volvo 142E- 0.006904570
5    Honda Civic- Volvo 142E- 0.009174312
6  Porsche 914-2- Volvo 142E- 0.018254605

head(levels(g$data$Var1))
[1] "Volvo 142E-"     "Fiat X1-9-"      "Fiat 128-"       "Toyota Corolla-"
[5] "Honda Civic-"    "Porsche 914-2-" 

head(levels(g$data$Var2))
[1] "Volvo 142E-"     "Fiat X1-9-"      "Fiat 128-"       "Toyota Corolla-"
[5] "Honda Civic-"    "Porsche 914-2-"

So it's a matter of pivoting it:所以这是一个旋转它的问题:

head(mat)
mat = pivot_wider(g$data,values_from="value",names_from="Var2")

# A tibble: 6 x 33
  Var1    `Volvo 142E-` `Fiat X1-9-` `Fiat 128-` `Toyota Corolla… `Honda Civic-`
  <fct>           <dbl>        <dbl>       <dbl>            <dbl>          <dbl>
1 Volvo …       0            0.00690     0.00690          0.00690        0.00917
2 Fiat X…       0.00690      0           0                0              0.0254 
3 Fiat 1…       0.00690      0           0                0              0.0254 
4 Toyota…       0.00690      0           0                0              0.0254 
5 Honda …       0.00917      0.0254      0.0254           0.0254         0      
6 Porsch…       0.0183       0.0252      0.0252           0.0252         0.0274 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM