使用plotly包绘制3D群集

时间:2018-07-24 14:01:12

标签: r

我需要使用R中的plotly包以3D形式呈现3个群集。这些群集是使用R中包含的k-means函数生成的。我进行了搜索,但发现仅使用ggplot包。

请问我该怎么做?

这是我的数据集的一部分,提供了可重复的示例。

> dput(DATAFINALE[1:50,])
structure(list(YEAR_SALES = c(2L, 1L, 2L, 2L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 1L, 2L, 1L, 2L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 1L, 1L), CREATION_YEAR_SALES = c(2L, 
1L, 2L, 2L, 2L, 1L, 1L, 2L, 1L, 1L, 1L, 1L, 2L, 1L, 1L, 2L, 2L, 
1L, 2L, 1L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 1L, 1L, 1L, 2L, 2L, 
2L, 2L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 1L, 1L, 2L, 1L, 2L, 1L, 
1L), TYPE_PEAU = c(2L, 3L, 4L, 2L, 2L, 3L, 2L, 2L, 2L, 2L, 2L, 
2L, 2L, 2L, 4L, 4L, 1L, 4L, 4L, 2L, 2L, 2L, 2L, 2L, 2L, 4L, 2L, 
3L, 4L, 2L, 2L, 4L, 2L, 2L, 2L, 2L, 2L, 3L, 2L, 4L, 2L, 2L, 3L, 
3L, 2L, 2L, 2L, 2L, 2L, 4L), SENSIBILITE = c(3L, 3L, 3L, 2L, 
1L, 3L, 3L, 2L, 2L, 2L, 3L, 1L, 3L, 1L, 2L, 3L, 3L, 2L, 3L, 3L, 
3L, 3L, 3L, 2L, 1L, 3L, 2L, 3L, 2L, 3L, 2L, 3L, 3L, 2L, 1L, 3L, 
3L, 3L, 3L, 1L, 2L, 2L, 3L, 2L, 3L, 3L, 3L, 1L, 2L, 3L), IMPERFEC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, 3L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 1L, 1L, 1L, 1L, 2L, 
2L, 1L, 2L, 1L, 1L, 1L, 1L, 1L, 1L), INTERET_ENVIRONNEMENT = c(1L, 
3L, 3L, 3L, 3L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 3L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 3L, 1L, 3L, 1L, 3L, 1L, 3L, 1L, 1L, 
1L, 1L, 1L, 3L, 1L, 1L, 3L, 3L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L), INTERET_COMPOSITION = c(1L, 1L, 1L, 3L, 3L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 3L, 3L, 1L, 1L, 1L, 1L, 3L, 1L, 
3L, 1L, 3L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 3L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), MONTH_SALES = c(9, 9, 
2, 9, 3, 3, 11, 12, 3, 6, 3, 3, 8, 9, 5, 1, 10, 5, 4, 9, 2, 3, 
4, 5, 6, 7, 7, 9, 7, 7, 11, 6, 4, 4, 4, 8, 9, 8, 9, 12, 4, 4, 
3, 11, 5, 12, 11, 2, 6, 3), DAY_SALES = c(13, 3, 10, 23, 12, 
10, 26, 4, 18, 9, 9, 9, 4, 10, 17, 28, 22, 4, 14, 22, 2, 10, 
1, 20, 7, 12, 1, 3, 13, 3, 9, 5, 13, 27, 1, 28, 18, 10, 3, 2, 
15, 6, 25, 4, 8, 23, 16, 19, 21, 14), HOURS_INS = c(17, 14, 18, 
16, 23, 18, 16, 12, 17, 16, 21, 18, 22, 14, 10, 15, 13, 13, 21, 
16, 23, 22, 17, 12, 15, 23, 17, 14, 8, 10, 12, 14, 13, 10, 17, 
3, 19, 22, 17, 18, 23, 18, 8, 16, 12, 19, 21, 14, 11, 22), CREATION_MONTH_SALES = c(9, 
9, 2, 10, 12, 3, 11, 2, 3, 6, 10, 3, 3, 9, 7, 11, 11, 5, 4, 9, 
2, 3, 4, 8, 6, 7, 10, 5, 7, 8, 11, 6, 4, 4, 11, 8, 9, 8, 12, 
12, 4, 8, 2, 11, 11, 1, 11, 10, 8, 3), CREATION_DAY_SALES = c(13, 
11, 15, 31, 5, 10, 27, 7, 18, 9, 8, 18, 6, 26, 4, 24, 16, 12, 
15, 22, 10, 10, 25, 5, 28, 20, 10, 18, 14, 31, 9, 5, 22, 27, 
6, 29, 18, 11, 6, 2, 16, 17, 1, 4, 23, 23, 16, 1, 25, 16), VALIDATION_YEAR_SALES = c(2, 
1, 2, 2, 2, 1, 1, 2, 1, 1, 1, 1, 2, 1, 1, 2, 2, 1, 2, 1, 2, 1, 
1, 1, 1, 1, 1, 2, 1, 1, 1, 2, 2, 2, 2, 2, 1, 1, 1, 1, 1, 1, 2, 
1, 1, 2, 1, 2, 1, 1), VALIDATION_MONTH_SALES = c(9, 9, 2, 11, 
12, 3, 12, 2, 3, 6, 10, 3, 3, 10, 7, 11, 11, 5, 4, 9, 2, 3, 4, 
8, 6, 7, 10, 5, 7, 9, 11, 6, 4, 4, 11, 8, 9, 8, 12, 12, 4, 8, 
2, 11, 11, 1, 11, 10, 8, 3), VALIDATION_DAY_SALES = c(15, 14, 
16, 3, 6, 19, 1, 8, 21, 10, 9, 21, 7, 1, 6, 25, 17, 13, 20, 29, 
11, 20, 29, 6, 30, 22, 12, 20, 16, 1, 10, 7, 25, 28, 14, 30, 
19, 13, 8, 4, 28, 24, 2, 7, 25, 25, 19, 3, 27, 21), AGE_CUSTUMER = c(32, 
37, 24, 32, 44, 33, 29, 30, 56, 48, 44, 43, 37, 43, 35, 62, 60, 
33, 51, 32, 35, 33, 28, 24, 32, 38, 33, 36, 54, 45, 39, 41, 55, 
34, 54, 51, 45, 57, 24, 47, 35, 51, 45, 39, 31, 40, 42, 42, 39, 
58), MEAN_Sales = c(0, 71.75, 50.7142857142857, 0, 0.666666666666667, 
83.3333333333333, 0.333333333333333, 25.7777777777778, 23.3846153846154, 
35.5294117647059, 21.6363636363636, 46.8461538461538, 18.4, 15.0666666666667, 
110.25, 8.85714285714286, 0, 21.5, 18.5714285714286, 28.125, 
101.333333333333, 69.1428571428571, 48.25, 20.5833333333333, 
12, 20.3333333333333, 23, 15.1428571428571, 12.3913043478261, 
30.3076923076923, 24.625, 23.375, 20.0833333333333, 32.75, 0, 
1.5, 0, 50.6, 32.3846153846154, 33, 28.6818181818182, 19.8076923076923, 
25.6666666666667, 9.83333333333333, 33, 55.3333333333333, 42.7, 
0, 31.375, 11.625), NBR_GIFTS = c(1, 1, 1, 1, 1, 1, 1, 1, 4, 
3, 4, 2, 1, 4, 1, 1, 1, 1, 3, 2, 1, 2, 2, 1, 3, 5, 4, 1, 9, 2, 
5, 1, 2, 1, 1, 1, 1, 1, 2, 2, 2, 3, 3, 1, 3, 2, 1, 1, 4, 4), 
    OUTCOME = c(3, 4, 7, 3, 3, 6, 3, 9, 26, 17, 22, 13, 10, 30, 
    4, 7, 7, 6, 14, 16, 3, 7, 12, 12, 15, 24, 21, 7, 46, 13, 
    16, 8, 12, 8, 3, 8, 3, 10, 13, 13, 22, 26, 12, 6, 13, 6, 
    10, 4, 16, 24)), .Names = c("YEAR_SALES", "CREATION_YEAR_SALES", 
"TYPE_PEAU", "SENSIBILITE", "IMPERFECTIONS", "BRILLANCE", "GRAIN_PEAU", 
"RIDES_VISAGE", "ALLERGIES", "MAINS", "PEAU_CORPS", "INTERET_ALIM_NATURELLE", 
"INTERET_ORIGINE_GEO", "INTERET_VACANCES", "INTERET_ENVIRONNEMENT", 
"INTERET_COMPOSITION", "MONTH_SALES", "DAY_SALES", "HOURS_INS", 
"CREATION_MONTH_SALES", "CREATION_DAY_SALES", "VALIDATION_YEAR_SALES", 
"VALIDATION_MONTH_SALES", "VALIDATION_DAY_SALES", "AGE_CUSTUMER", 
"MEAN_Sales", "NBR_GIFTS", "OUTCOME"), row.names = c(1L, 2L, 
3L, 5L, 9L, 13L, 14L, 16L, 18L, 19L, 20L, 24L, 27L, 29L, 30L, 
32L, 33L, 35L, 36L, 37L, 39L, 44L, 49L, 51L, 52L, 53L, 55L, 56L, 
61L, 62L, 63L, 65L, 66L, 67L, 71L, 74L, 75L, 80L, 81L, 84L, 86L, 
90L, 92L, 95L, 96L, 99L, 100L, 103L, 104L, 107L), class = "data.frame")

我的集群模型由以下代码给出:

Model<-kmeans(DATAFINALE,centers = 3,nstart=20)

然后我需要获得标题为Basic 3D Scatter Plot的链接https://plot.ly/r/3d-scatter-plots/中给出的图。

提前谢谢

1 个答案:

答案 0 :(得分:0)

首先,必须将聚类向量添加到数据集中。

<div class="container-fluid">

然后,您必须确定将哪个变量绘制为x,y和z(我随机抽取了三个):

  # convert them as factor to plot them right
  DATAFINALE$cluster <- as.factor(Model$cluster)

最后,您可以使用群集作为颜色来绘制它:

x <-'MONTH_SALES'
y <-'DAY_SALES'
z <- 'HOURS_INS'

结果在这里:

enter image description here