我有一个10
行7
列的数据框。每一行,列都是一个字符串。
我想知道是否有一个包可以对列进行分层聚类/着色?
例如,假设它是三列五行:
V1 V2 V3 V4 V5
a a c d e
b b d f b
c c e a c
d d g b d
e f h c e
是否有一个包装会将V1 / V2显示为高度相关并绘制它?让我们说如果成对元素匹配则严格相关。
答案 0 :(得分:1)
> d<-data.frame(V1=c('a','b','c','d','e'),V2=c('a','b','c','d','f'),V3=c('c','d','e','g','h'),V4=c('d','f','a','b','c'),V5=c('e','b','c','d','e'), stringsAsFactors=F)
> res<-outer(1:5,1:5, FUN=Vectorize(function(i,j) sum(d[,i]==d[,j]) ))
> res
[,1] [,2] [,3] [,4] [,5]
[1,] 5 4 0 0 4
[2,] 4 5 0 0 3
[3,] 0 0 5 0 0
[4,] 0 0 0 5 0
[5,] 4 3 0 0 5
> library(corrplot)
> corrplot(res/5)
请参阅https://cran.r-project.org/web/packages/corrplot/vignettes/corrplot-intro.html了解更多绘图选项,包括群集。 注意:V1 / V2和V1 / V5与您的示例同样“高度相关”。