是否有任何解决方法可将混合数据类型聚类并在R中呈现3D散点图?

时间:2019-07-11 21:32:44

标签: r ggplot2 dplyr cluster-analysis

我试图查看3D图中标记组内的数据点分布,因为我想查看数据点的分布,并希望查看3D空间中每组数据点的相似程度。为此,我使用了来自CRAN的scatterplot3d程序包来获取3D散点图,但没有获得正确的数据图。

可复制的数据

这是我使用的可复制数据。

    > dput(head(phenDat,30))
structure(list(SampleID = c("Tarca_001_P1A01", "Tarca_013_P1B01", 
"Tarca_025_P1C01", "Tarca_037_P1D01", "Tarca_049_P1E01", "Tarca_061_P1F01", 
"Tarca_051_P1E03", "Tarca_063_P1F03", "Tarca_075_P1G03", "Tarca_087_P1H03", 
"Tarca_004_P1A04", "Tarca_064_P1F04", "Tarca_076_P1G04", "Tarca_088_P1H04", 
"Tarca_005_P1A05", "Tarca_017_P1B05", "Tarca_054_P1E06", "Tarca_066_P1F06", 
"Tarca_078_P1G06", "Tarca_090_P1H06", "Tarca_007_P1A07", "Tarca_019_P1B07", 
"Tarca_031_P1C07", "Tarca_079_P1G07", "Tarca_091_P1H07", "Tarca_008_P1A08", 
"Tarca_020_P1B08", "Tarca_022_P1B10", "Tarca_034_P1C10", "Tarca_046_P1D10"
), GA = c(11, 15.3, 21.7, 26.7, 31.3, 32.1, 19.7, 23.6, 27.6, 
30.6, 32.6, 12.6, 18.6, 25.6, 30.6, 36.4, 24.9, 28.9, 36.6, 19.9, 
26.1, 30.1, 36.7, 13.6, 17.6, 22.6, 24.7, 13.3, 19.7, 24.7), 
    Batch = c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 
    2L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 4L, 4L, 5L, 5L, 6L, 
    6L, 6L, 6L), Set = c("PRB_HTA", "PRB_HTA", "PRB_HTA", "PRB_HTA", 
    "PRB_HTA", "PRB_HTA", "PRB_HTA", "PRB_HTA", "PRB_HTA", "PRB_HTA", 
    "PRB_HTA", "PRB_HTA", "PRB_HTA", "PRB_HTA", "PRB_HTA", "PRB_HTA", 
    "PRB_HTA", "PRB_HTA", "PRB_HTA", "PRB_HTA", "PRB_HTA", "PRB_HTA", 
    "PRB_HTA", "PRB_HTA", "PRB_HTA", "PRB_HTA", "PRB_HTA", "PRB_HTA", 
    "PRB_HTA", "PRB_HTA"), Train = c(1L, 1L, 1L, 1L, 1L, 1L, 
    1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
    1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), Platform = c("HTA20", 
    "HTA20", "HTA20", "HTA20", "HTA20", "HTA20", "HTA20", "HTA20", 
    "HTA20", "HTA20", "HTA20", "HTA20", "HTA20", "HTA20", "HTA20", 
    "HTA20", "HTA20", "HTA20", "HTA20", "HTA20", "GSE113966", "GSE113966", 
    "GSE113966", "GSE113966", "GSE113966", "GSE113966", "GSE113966", "GSE113966", "GSE113966", 
    "GSE113966")), row.names = c(NA, 30L), class = "data.frame")

我的尝试

hclustfunc <- function(x) hclust(x, method="complete")
distfunc <- function(x) as.dist((1-cor(t(x)))/2)
d <- distfunc(persons_df)
fit <- hclustfunc(d)

我更新的尝试

library(rgl)
library(car)
scatter3d(x = PC1, y = PC2, z = PC3, surface = FALSE, groups = as.factor(clusters),  surface.col = cluster.colors, col = cluster.colors, xlab="PC1",ylab="PC2",zlab="PC3")

基本上,我想查看属于不同批次(或组)的数据点(又称行),并希望通过“组”属性为它们着色。我只是想看看如果按照不同的年龄类别,不同的批次和不同的平台对数据点进行分组,它们之间的相似之处

我正在考虑使用kmeans,PCA,其他方法可以给我提供可以在3D图中可视化的不同组件,但这对我来说不是很直观,如何在R中做到这一点?

所需的情节

我想获得3D图,像这样:

enter image description here

谁能指出我该怎么做呢?有什么方法可以聚类我的数据并在R中的3D图中将其可视化?有什么想法吗?谢谢

更新:可能最简单的事情

首先,我不想获得过于复杂的解决方案,我只是想对属于不同批次,平台和年龄类别的数据点(又称每一行)进行分组(我使用findInterval(persons_df$ages, c(10,20,30,40,50)) )。有什么办法可以在R中做到这一点?

1 个答案:

答案 0 :(得分:4)

编辑-为混合数据聚类添加了k模式方法。

您也可以考虑使用plotly进行3d绘图。这是您的数据示例,其中我已为批次,平台和10岁年龄段的每个现有组合定义了组。在绘图中,为它们分配了不同的颜色,您可以双击组图例以切换外观。您需要进行修改以获取更大的数据,例如,您可以从分组中删除平台,因为该平台已经映射到z。

library(plotly); library(dplyr); library(RColorBrewer)
age_group = 10
phenDat %>% 
  mutate(group = paste(Batch, Platform, "age", 
                       floor(GA/age_group)*age_group, "-", 
                       floor(GA/age_group)*age_group + age_group - 1)) %>%
plot_ly(x = ~GA, y = ~Set, z = ~Platform, color = ~group) %>%
  add_markers(marker = list(size = 2,
                            color = colorRampPalette(brewer.pal(11,"Spectral"))(10))) %>%
  layout(scene = list(xaxis = list(title = "GA"),
                      yaxis = list(title = "Set"),
                      zaxis = list(title = "Platform")))

enter image description here

对于给定混合数据的聚类,这是使用klaR包的kmodes函数的一种方法,该方法似乎在这里产生了合理的结果:

phenDat %>%
  bind_cols(cluster = klaR::kmodes(phenDat, 6)[["cluster"]] %>% as.character) %>%
  plot_ly(x = ~GA, y = ~Set, z = ~Platform, color = ~cluster) %>%
  add_markers(marker = list(size = 5)) %>%
  layout(scene = list(xaxis = list(title = "GA"),
                      yaxis = list(title = "Set"),
                      zaxis = list(title = "Platform")))

enter image description here