美文网首页R语言学习基因ID转换
R语言可视化(十七):tSNE图绘制

R语言可视化(十七):tSNE图绘制

作者: Davey1220 | 来源:发表于2020-10-02 22:47 被阅读0次

    17. tSNE图绘制

    清除当前环境中的变量

    rm(list=ls())
    

    设置工作目录

    setwd("C:/Users/Dell/Desktop/R_Plots/17tsne/")
    

    查看示例数据

    head(iris)
    ##   Sepal.Length Sepal.Width Petal.Length Petal.Width Species
    ## 1          5.1         3.5          1.4         0.2  setosa
    ## 2          4.9         3.0          1.4         0.2  setosa
    ## 3          4.7         3.2          1.3         0.2  setosa
    ## 4          4.6         3.1          1.5         0.2  setosa
    ## 5          5.0         3.6          1.4         0.2  setosa
    ## 6          5.4         3.9          1.7         0.4  setosa
    

    使用tsne包进行tSNE降维可视化分析

    # 加载tsne包
    library(tsne)
    
    colors = rainbow(length(unique(iris$Species)))
    names(colors) = unique(iris$Species)
    head(colors)
    ##      setosa  versicolor   virginica 
    ## "#FF0000FF" "#00FF00FF" "#0000FFFF"
    
    # 使用tsne函数进行tSNE降维分析
    tsne_iris = tsne(iris[,1:4],k=2,perplexity=50)
    ## sigma summary: Min. : 0.565012665854053 |1st Qu. : 0.681985646004023 |Median : 0.713004330336136 |Mean : 0.716213420895748 |3rd Qu. : 0.74581655363904 |Max. : 0.874979764925049 |
    ## Epoch: Iteration #100 error is: 12.4789319339631
    ## Epoch: Iteration #200 error is: 0.228964032001554
    ## Epoch: Iteration #300 error is: 0.225993664440037
    ## Epoch: Iteration #400 error is: 0.181661747509326
    ## Epoch: Iteration #500 error is: 0.181628530267247
    ## Epoch: Iteration #600 error is: 0.181628527460257
    ## Epoch: Iteration #700 error is: 0.181628527460191
    ## Epoch: Iteration #800 error is: 0.181628527460191
    ## Epoch: Iteration #900 error is: 0.181628527460191
    ## Epoch: Iteration #1000 error is: 0.181628527460191
    
    # 查看tSNE降维后的结果
    head(tsne_iris)
    ##           [,1]     [,2]
    ## [1,] 3.2389639 5.779896
    ## [2,] 3.1307192 3.026691
    ## [3,] 1.9806959 4.528267
    ## [4,] 0.6876072 4.567498
    ## [5,] 2.5127031 6.712823
    ## [6,] 3.5102694 7.830132
    
    # 使用基础plot函数可视化tSNE降维后的结果
    plot(tsne_iris,col=colors[iris$Species],pch=16,
         xlab = "tSNE_1",ylab = "tSNE_2",main = "tSNE plot")
    # 添加分隔线
    abline(h=0,v=0,lty=2,col="gray")
    # 添加图例
    legend("topright",title = "Species",inset = 0.01,
           legend = unique(iris$Species),pch=16,
           col = unique(colors[iris$Species]))
    
    image.png

    使用Rtsne包进行tSNE降维可视化分析

    # 加载Rtsne包
    library(Rtsne)
    
    iris_unique <- unique(iris) # Remove duplicates
    iris_matrix <- as.matrix(iris_unique[,1:4])
    head(iris_matrix)
    ##   Sepal.Length Sepal.Width Petal.Length Petal.Width
    ## 1          5.1         3.5          1.4         0.2
    ## 2          4.9         3.0          1.4         0.2
    ## 3          4.7         3.2          1.3         0.2
    ## 4          4.6         3.1          1.5         0.2
    ## 5          5.0         3.6          1.4         0.2
    ## 6          5.4         3.9          1.7         0.4
    
    # Set a seed if you want reproducible results
    set.seed(42)
    # 使用Rtsne函数进行tSNE降维分析
    tsne_out <- Rtsne(iris_matrix,pca=FALSE,dims=2,
                      perplexity=30,theta=0.0) # Run TSNE
    head(tsne_out)
    ## $N
    ## [1] 149
    ## 
    ## $Y
    ##               [,1]       [,2]
    ##   [1,]   5.5067790  24.486797
    ##   [2,]   7.6737152  23.079876
    ##   [3,]   8.1913796  23.761097
    ##   [4,]   8.4254467  23.808179
    ##   [5,]   5.5960403  24.921692
    ##   [6,]   3.4308904  24.959199
    ##   [7,]   8.1381015  24.557058
    ##   [8,]   6.1900369  24.173668
    ##   [9,]   9.0993792  23.285372
    ##  [10,]   7.4001506  23.500007
    ##  [11,]   3.9869935  24.736220
    ##  [12,]   7.0156526  24.518013
    ##  [13,]   8.0765386  23.166514
    ##  [14,]   9.4908994  23.556526
    ##  [15,]   2.8235240  25.573620
    ##  [16,]   2.9130954  25.935506
    ##  [17,]   3.6599753  25.426126
    ##  [18,]   5.4125340  24.349586
    ##  [19,]   3.0210424  24.869039
    ##  [20,]   4.6411079  25.257085
    ##  [21,]   4.2922549  23.641746
    ##  [22,]   4.8038395  24.901788
    ##  [23,]   7.8765660  21.926248
    ##  [24,]   5.4954619  23.046120
    ##  [25,]   7.1852574  25.092759
    ##  [26,]   7.2300950  22.976782
    ##  [27,]   5.7752939  23.532759
    ##  [28,]   4.9904034  24.266867
    ##  [29,]   5.3041970  24.031600
    ##  [30,]   7.7778841  24.221787
    ##  [31,]   7.6319477  23.823039
    ##  [32,]   4.4463848  23.708368
    ##  [33,]   3.9290986  25.893639
    ##  [34,]   3.2745094  25.857022
    ##  [35,]   7.3529093  23.516745
    ##  [36,]   6.7761239  23.210195
    ##  [37,]   4.0294992  24.106827
    ##  [38,]   6.0324266  25.059326
    ##  [39,]   9.1217786  23.534852
    ##  [40,]   5.7711840  24.044706
    ##  [41,]   5.9092367  24.569308
    ##  [42,]   9.3613002  22.569342
    ##  [43,]   9.0282188  24.001022
    ##  [44,]   5.2225531  23.159773
    ##  [45,]   3.4211933  24.162079
    ##  [46,]   8.0525016  23.160908
    ##  [47,]   4.6038886  25.326149
    ##  [48,]   8.4113423  24.034475
    ##  [49,]   4.2748166  24.781044
    ##  [50,]   6.4893044  23.869299
    ##  [51,]  -3.4834923  -9.965512
    ##  [52,]  -1.9690760 -10.664461
    ##  [53,]  -3.8191676 -10.383389
    ##  [54,]   3.1376088 -10.703924
    ##  [55,]  -2.4532383 -10.770603
    ##  [56,]   0.9900592 -11.432847
    ##  [57,]  -2.2628047 -11.362811
    ##  [58,]   4.5040714  -9.968530
    ##  [59,]  -2.5192007 -10.292370
    ##  [60,]   3.2323610 -11.120233
    ##  [61,]   4.4280067 -10.256122
    ##  [62,]   0.5648119 -10.757219
    ##  [63,]   2.3575941  -9.270542
    ##  [64,]  -1.1990073 -11.613762
    ##  [65,]   2.9143236  -9.675958
    ##  [66,]  -2.4854425  -9.950649
    ##  [67,]   0.9030404 -11.725632
    ##  [68,]   1.9350463 -10.053143
    ##  [69,]  -0.5092066 -13.138326
    ##  [70,]   2.8690573 -10.213814
    ##  [71,]  -2.2482343 -12.499771
    ##  [72,]   0.9718897  -9.969406
    ##  [73,]  -1.9907939 -13.095569
    ##  [74,]  -0.8307111 -11.551066
    ##  [75,]  -1.2508515 -10.232772
    ##  [76,]  -2.1673565 -10.101118
    ##  [77,]  -3.3901546 -10.456176
    ##  [78,]  -4.2762412 -11.213459
    ##  [79,]  -0.4942210 -11.332514
    ##  [80,]   3.3653166  -9.570109
    ##  [81,]   3.3298577 -10.238496
    ##  [82,]   3.5141631 -10.036563
    ##  [83,]   2.2275955 -10.048487
    ##  [84,]  -3.1599068 -13.331195
    ##  [85,]   1.3926380 -11.951350
    ##  [86,]  -1.5899534 -11.566936
    ##  [87,]  -3.1257502 -10.387121
    ##  [88,]  -0.3192176 -12.900888
    ##  [89,]   1.6793000 -10.887428
    ##  [90,]   2.8248768 -10.725630
    ##  [91,]   2.1670620 -11.425018
    ##  [92,]  -1.0644772 -11.285936
    ##  [93,]   2.2230720 -10.159786
    ##  [94,]   4.4493154  -9.976223
    ##  [95,]   2.0909366 -11.002940
    ##  [96,]   1.3450678 -10.767548
    ##  [97,]   1.4286380 -10.861575
    ##  [98,]  -0.4859747 -10.529980
    ##  [99,]   4.5531760  -9.766885
    ## [100,]   1.8256231 -10.622254
    ## [101,]  -8.1681708 -15.141821
    ## [102,]  -3.0821655 -14.054047
    ## [103,]  -9.0429357 -13.408360
    ## [104,]  -6.2913266 -12.856362
    ## [105,]  -7.7669167 -13.505570
    ## [106,] -10.4974274 -13.294026
    ## [107,]   2.6560764 -12.368364
    ## [108,]  -9.8999107 -12.946700
    ## [109,]  -7.6408393 -12.392806
    ## [110,]  -9.6394676 -14.323147
    ## [111,]  -5.9640846 -14.026406
    ## [112,]  -5.4826524 -13.139493
    ## [113,]  -7.5860668 -13.644660
    ## [114,]  -2.8719220 -14.392450
    ## [115,]  -3.5419594 -14.708240
    ## [116,]  -6.7399088 -14.578607
    ## [117,]  -6.4682393 -13.069037
    ## [118,] -10.6462356 -14.040398
    ## [119,] -10.8093339 -13.034725
    ## [120,]  -1.6239970 -13.770771
    ## [121,]  -8.4482533 -14.022786
    ## [122,]  -2.7673819 -14.345725
    ## [123,] -10.6205472 -13.101836
    ## [124,]  -3.4751382 -12.775843
    ## [125,]  -8.1144685 -13.985993
    ## [126,]  -9.3980637 -13.136582
    ## [127,]  -3.0073132 -12.651019
    ## [128,]  -2.8873036 -12.760297
    ## [129,]  -6.9566725 -13.128339
    ## [130,]  -9.1876614 -12.729380
    ## [131,]  -9.8001932 -12.948082
    ## [132,] -10.6301905 -14.035993
    ## [133,]  -7.0797403 -13.260803
    ## [134,]  -3.7329453 -12.449749
    ## [135,]  -4.9349389 -12.667315
    ## [136,] -10.1578052 -13.475241
    ## [137,]  -7.5054287 -15.084694
    ## [138,]  -6.3581608 -13.135133
    ## [139,]  -2.5507459 -12.699941
    ## [140,]  -7.5576427 -13.885089
    ## [141,]  -7.8477888 -14.288949
    ## [142,]  -6.9311661 -14.331769
    ## [143,]  -8.6350440 -14.108707
    ## [144,]  -8.2340074 -14.614955
    ## [145,]  -6.8427060 -14.201949
    ## [146,]  -3.8694359 -13.229102
    ## [147,]  -6.0352962 -13.720744
    ## [148,]  -6.9420702 -15.132527
    ## [149,]  -3.0791891 -13.529181
    ## 
    ## $costs
    ##   [1]  1.179324e-04 -3.689469e-04 -1.472921e-04 -4.273294e-04  7.624380e-04
    ##   [6] -1.313372e-04  1.126943e-03  7.574498e-04 -1.913122e-04 -6.932105e-04
    ##  [11] -8.492889e-05  1.024120e-03 -4.320760e-04  9.484793e-05  1.034310e-04
    ##  [16] -1.842527e-05 -2.769062e-04  3.582073e-05  9.299024e-05  3.510226e-04
    ##  [21]  1.186597e-04  9.862502e-05  2.806501e-03  7.798399e-04  7.986599e-04
    ##  [26] -1.339566e-04  9.950297e-04  1.161829e-04 -7.778344e-05 -2.067769e-05
    ##  [31] -6.404696e-04 -5.211731e-05  6.500397e-05  1.041786e-05 -3.733109e-04
    ##  [36]  2.337047e-04 -3.017156e-04  9.792827e-04 -1.755743e-04  3.190899e-04
    ##  [41]  2.431256e-04  1.020450e-04  2.787914e-04  7.182304e-04  1.113900e-03
    ##  [46] -3.749718e-04  3.334247e-04  8.082786e-05 -5.599858e-05  7.585540e-04
    ##  [51]  7.531620e-04  7.173614e-04  2.078562e-03 -1.084888e-05  1.999274e-03
    ##  [56]  1.693053e-03  7.959443e-04  4.374940e-04  1.132554e-03  7.866948e-04
    ##  [61]  3.883855e-05  1.778366e-03  1.568548e-03  2.732406e-03  4.332908e-05
    ##  [66]  6.904889e-04  2.165244e-03  2.403076e-04  2.726784e-03 -3.148891e-05
    ##  [71]  1.613799e-03  2.174618e-03  3.656731e-03  1.439881e-03  2.410020e-03
    ##  [76]  1.061569e-03  1.028394e-03  5.288050e-03  3.202629e-03  2.366342e-04
    ##  [81]  8.124443e-05  1.351984e-04  3.548746e-04  1.674996e-03  1.447612e-03
    ##  [86]  4.387094e-04  1.335889e-03  3.588848e-03 -1.542536e-04  1.726962e-04
    ##  [91]  5.801623e-04  2.254048e-03  4.292584e-04  3.809488e-04  6.045353e-04
    ##  [96]  1.324231e-04  6.585140e-04  3.054234e-03  8.623991e-04  4.056875e-04
    ## [101]  4.624438e-04  1.366814e-03  1.009017e-03  1.713772e-03  6.288805e-04
    ## [106]  1.608018e-04  1.268004e-03  3.481348e-04  1.375940e-03  5.179650e-04
    ## [111]  3.086146e-03  3.631408e-03  8.518228e-04  9.834214e-04  1.469104e-03
    ## [116]  1.224737e-03  1.777474e-03  1.681800e-04  5.902857e-05  2.021059e-03
    ## [121]  4.853900e-04  2.000830e-03  1.299285e-04  2.299773e-03  3.225243e-04
    ## [126]  4.490196e-04  1.750803e-03  1.941641e-03  1.081639e-03  8.451969e-04
    ## [131]  4.045783e-04  1.189270e-04  6.152785e-04  2.858092e-03  1.208743e-03
    ## [136] -1.943961e-04  8.259299e-04  1.318010e-03  1.952109e-03  5.434035e-04
    ## [141]  1.365092e-04 -1.536645e-04  6.192905e-04  2.872638e-04  2.285592e-04
    ## [146]  1.959709e-03  3.139510e-03  1.286250e-03  1.493989e-03
    ## 
    ## $itercosts
    ##  [1] 45.0879058 43.7584671 45.1243240 44.7692647 43.7384615  0.3317077
    ##  [7]  0.1473289  0.1354032  0.1325056  0.1308674  0.1299235  0.1293005
    ## [13]  0.1288420  0.1284922  0.1282276  0.1280100  0.1278221  0.1276655
    ## [19]  0.1275252  0.1274040
    ## 
    ## $origD
    ## [1] 4
    ## 
    ## $perplexity
    ## [1] 30
    
    # Show the objects in the 2D tsne representation
    plot(tsne_out$Y,col=iris_unique$Species, asp=1,pch=20,
         xlab = "tSNE_1",ylab = "tSNE_2",main = "tSNE plot")
    # 添加分隔线
    abline(h=0,v=0,lty=2,col="gray")
    # 添加图例
    legend("topright",title = "Species",inset = 0.01,
           legend = unique(iris_unique$Species),pch=16,
           col = unique(iris_unique$Species))
    
    image.png
    # data.frame as input
    set.seed(42)
    # 更改perplexity和theta值
    tsne_out <- Rtsne(iris_unique,pca=FALSE,
                      perplexity = 20,theta=0.5)
    head(tsne_out)
    ## $N
    ## [1] 149
    ## 
    ## $Y
    ##              [,1]       [,2]
    ##   [1,]   7.715389  17.705174
    ##   [2,]   6.714282  13.769817
    ##   [3,]   8.157742  14.124667
    ##   [4,]   7.809143  13.697934
    ##   [5,]   7.987368  17.528455
    ##   [6,]   6.791518  20.153909
    ##   [7,]   8.710824  14.416845
    ##   [8,]   7.376314  16.792163
    ##   [9,]   8.189728  12.791145
    ##  [10,]   6.717610  14.309527
    ##  [11,]   7.618207  19.807624
    ##  [12,]   6.979341  15.679252
    ##  [13,]   7.061598  13.611695
    ##  [14,]   8.703919  12.799065
    ##  [15,]   7.369231  21.314902
    ##  [16,]   6.842204  21.427915
    ##  [17,]   7.166539  20.435873
    ##  [18,]   7.465803  17.763868
    ##  [19,]   7.676051  20.613974
    ##  [20,]   6.710571  19.156099
    ##  [21,]   8.443834  19.008168
    ##  [22,]   6.742755  18.731957
    ##  [23,]   9.501581  15.070496
    ##  [24,]   5.980155  17.128316
    ##  [25,]   6.139254  15.733237
    ##  [26,]   6.217819  14.178264
    ##  [27,]   6.428262  17.065069
    ##  [28,]   7.724756  18.336525
    ##  [29,]   7.976838  18.004713
    ##  [30,]   7.424925  14.529141
    ##  [31,]   6.971005  14.358575
    ##  [32,]   8.369564  18.977941
    ##  [33,]   6.326850  20.407753
    ##  [34,]   6.816088  21.083007
    ##  [35,]   6.780623  14.360317
    ##  [36,]   8.060725  15.576010
    ##  [37,]   8.395989  19.619546
    ##  [38,]   8.443099  17.040896
    ##  [39,]   8.452467  12.996652
    ##  [40,]   7.395879  17.298077
    ##  [41,]   8.014606  17.144911
    ##  [42,]   7.481623  12.267588
    ##  [43,]   8.733881  13.378562
    ##  [44,]   5.947488  17.507990
    ##  [45,]   5.787446  19.212730
    ##  [46,]   7.087497  13.571666
    ##  [47,]   6.582526  19.272593
    ##  [48,]   8.207260  13.926048
    ##  [49,]   7.399908  19.469612
    ##  [50,]   7.571307  16.307071
    ##  [51,]   4.394639 -19.289305
    ##  [52,]   4.166886 -20.851062
    ##  [53,]   4.793280 -19.198982
    ##  [54,]   5.602155 -26.671096
    ##  [55,]   5.245131 -20.511082
    ##  [56,]   5.646036 -24.120716
    ##  [57,]   3.872715 -21.206414
    ##  [58,]   5.519235 -28.451264
    ##  [59,]   4.926860 -20.266936
    ##  [60,]   6.062190 -26.537496
    ##  [61,]   5.759777 -28.268192
    ##  [62,]   4.701436 -23.749113
    ##  [63,]   3.679397 -26.426654
    ##  [64,]   5.351755 -21.905942
    ##  [65,]   4.314637 -26.952424
    ##  [66,]   4.202548 -20.080409
    ##  [67,]   5.695679 -23.868457
    ##  [68,]   4.431456 -25.724963
    ##  [69,]   7.270078 -21.688010
    ##  [70,]   4.974752 -26.640891
    ##  [71,]   3.830695 -22.192594
    ##  [72,]   3.994333 -24.489692
    ##  [73,]   6.430228 -21.040284
    ##  [74,]   5.630178 -21.954776
    ##  [75,]   4.817189 -21.140262
    ##  [76,]   4.506897 -20.389368
    ##  [77,]   5.195707 -19.683392
    ##  [78,]   5.153288 -19.294883
    ##  [79,]   5.072630 -22.577081
    ##  [80,]   4.597493 -27.433435
    ##  [81,]   5.193637 -27.056603
    ##  [82,]   5.100150 -27.339082
    ##  [83,]   4.540650 -26.050193
    ##  [84,]   6.254610 -21.319542
    ##  [85,]   6.071711 -24.223658
    ##  [86,]   3.745414 -22.074190
    ##  [87,]   4.578353 -19.769608
    ##  [88,]   7.252855 -21.728829
    ##  [89,]   5.034584 -24.916893
    ##  [90,]   5.501645 -26.377054
    ##  [91,]   5.984879 -25.268674
    ##  [92,]   5.017774 -22.007440
    ##  [93,]   4.644536 -26.005433
    ##  [94,]   5.515568 -28.395657
    ##  [95,]   5.434407 -25.395652
    ##  [96,]   4.942550 -24.676220
    ##  [97,]   5.057557 -24.744289
    ##  [98,]   4.794154 -22.178158
    ##  [99,]   5.288612 -28.548037
    ## [100,]   4.924180 -25.288019
    ## [101,] -14.720190   8.120475
    ## [102,] -11.835778   3.096062
    ## [103,] -12.778642   9.733934
    ## [104,] -13.188997   5.671674
    ## [105,] -13.631634   7.611333
    ## [106,] -12.901432  11.506110
    ## [107,] -11.673101   1.879873
    ## [108,] -12.440571  10.837229
    ## [109,] -11.274141   7.140769
    ## [110,] -13.912300  10.122575
    ## [111,] -12.147243   6.270212
    ## [112,] -12.546087   5.270368
    ## [113,] -12.749814   7.888978
    ## [114,] -11.901891   2.760847
    ## [115,] -11.288509   2.772746
    ## [116,] -13.928568   6.939219
    ## [117,] -12.943740   6.008282
    ## [118,] -13.810883  11.574257
    ## [119,] -12.670568  11.882708
    ## [120,] -12.875627   3.311425
    ## [121,] -13.261832   8.806721
    ## [122,] -11.580872   2.758515
    ## [123,] -12.723562  11.649209
    ## [124,] -12.087709   4.196538
    ## [125,] -13.394957   8.403919
    ## [126,] -12.508938  10.198705
    ## [127,] -11.790559   4.043863
    ## [128,] -11.473917   4.019978
    ## [129,] -13.212842   6.489570
    ## [130,] -12.149792  10.078319
    ## [131,] -12.451790  10.727172
    ## [132,] -13.817733  11.547700
    ## [133,] -13.307234   6.693915
    ## [134,] -12.403537   4.451978
    ## [135,] -13.428587   4.452829
    ## [136,] -13.025346  11.051335
    ## [137,] -14.547148   7.493264
    ## [138,] -13.066118   5.920086
    ## [139,] -11.374131   3.779877
    ## [140,] -12.558457   8.064209
    ## [141,] -13.482160   8.119613
    ## [142,] -12.168193   7.623663
    ## [143,] -13.578190   8.961591
    ## [144,] -14.001458   8.494630
    ## [145,] -12.462676   7.298402
    ## [146,] -12.411605   4.054358
    ## [147,] -12.344027   6.129648
    ## [148,] -14.553464   7.009208
    ## [149,] -11.499951   3.587173
    ## 
    ## $costs
    ##   [1]  6.242687e-04  6.293003e-04  7.580414e-04  1.967870e-04  1.029160e-03
    ##   [6]  3.812285e-04  1.284010e-03  1.947316e-03  5.602282e-05  2.609845e-04
    ##  [11]  1.131349e-03  2.355685e-03  1.496672e-04 -2.725965e-04  8.615276e-05
    ##  [16] -9.103645e-05 -1.026418e-04  7.471934e-04 -4.820882e-05  1.225569e-03
    ##  [21]  1.401053e-04  1.238423e-03  1.083713e-03  1.013981e-03  4.054493e-04
    ##  [26]  4.235251e-04  1.866602e-03  1.260274e-03  8.423138e-04  8.466582e-04
    ##  [31]  1.028833e-05  4.868350e-04  4.055505e-04  4.395368e-04  7.272476e-04
    ##  [36]  1.526546e-03  4.291155e-04  1.168867e-03  2.936939e-05  1.256397e-03
    ##  [41]  3.652797e-04 -2.487728e-04  4.364336e-04  4.936336e-04  6.284857e-04
    ##  [46]  1.242948e-04  9.720837e-04  7.300661e-04  1.276410e-03  2.595590e-03
    ##  [51] -1.638248e-05  9.963329e-04  2.082263e-04 -1.539286e-04  1.250050e-03
    ##  [56]  1.445096e-03  1.478114e-03  5.981239e-06  3.058988e-04  4.631793e-04
    ##  [61] -1.839881e-04  1.833061e-03  2.231296e-03  1.756529e-03  2.905429e-04
    ##  [66]  2.668615e-04  1.168726e-03  1.302897e-04  2.269873e-03  3.376048e-04
    ##  [71]  1.177970e-03  3.345636e-03  1.446172e-03  6.378471e-04  1.522122e-03
    ##  [76]  2.028485e-04  8.296849e-05  4.347997e-04  2.332496e-03 -5.056151e-05
    ##  [81]  1.733452e-04  1.506508e-04  8.317497e-04  4.218776e-04  3.149415e-04
    ##  [86]  9.216793e-04  3.308710e-04  3.635962e-03 -1.450625e-04  4.038861e-04
    ##  [91]  5.577626e-04  1.774802e-03  8.720726e-04  4.305382e-05  8.922485e-04
    ##  [96] -6.292440e-05  8.183549e-04  2.754186e-03 -8.058446e-05  8.772215e-04
    ## [101]  1.208109e-04  7.315875e-04  2.038605e-03  1.421054e-03  1.149629e-03
    ## [106]  5.391937e-04  2.029174e-03  7.924317e-04  3.640921e-03  1.011342e-03
    ## [111]  1.994893e-03  2.035141e-03  1.417985e-03  6.375140e-04  6.554719e-04
    ## [116]  2.277190e-03  1.273799e-03  5.490530e-04  8.494791e-05  2.960040e-04
    ## [121]  1.175569e-03  7.489057e-04  3.251306e-04  2.266579e-04  5.750585e-04
    ## [126]  9.387132e-04  4.593413e-06  7.492131e-04  1.387842e-03  9.528573e-04
    ## [131]  8.688057e-04  4.153938e-04  7.817248e-04  1.251807e-05  7.458750e-04
    ## [136] -9.347800e-06  8.179079e-04  6.716049e-04  2.656127e-04  8.893953e-04
    ## [141]  4.275921e-04 -1.101141e-04  1.378859e-03  6.152032e-04  6.984994e-04
    ## [146]  1.646995e-04  2.326887e-03  1.450425e-03  4.802579e-04
    ## 
    ## $itercosts
    ##  [1] 47.1165208 45.6090433 45.3971807 45.3865836 44.1171993  0.4610884
    ##  [7]  0.1465717  0.1383095  0.1329804  0.1320085  0.1276541  0.1273532
    ## [13]  0.1252604  0.1228743  0.1209324  0.1210714  0.1220934  0.1225643
    ## [19]  0.1223175  0.1226974
    ## 
    ## $origD
    ## [1] 7
    ## 
    ## $perplexity
    ## [1] 20
    
    # 使用ggplot2包可视化tSNE降维的结果
    library(ggplot2)
    
    tsne_res <- as.data.frame(tsne_out$Y)
    colnames(tsne_res) <- c("tSNE1","tSNE2")
    head(tsne_res)
    ##      tSNE1    tSNE2
    ## 1 7.715389 17.70517
    ## 2 6.714282 13.76982
    ## 3 8.157742 14.12467
    ## 4 7.809143 13.69793
    ## 5 7.987368 17.52846
    ## 6 6.791518 20.15391
    
    # 使用ggplot2可视化tSNE降维的结果
    ggplot(tsne_res,aes(tSNE1,tSNE2,color=iris_unique$Species)) + 
      geom_point() + theme_bw() + 
      geom_hline(yintercept = 0,lty=2,col="red") + 
      geom_vline(xintercept = 0,lty=2,col="blue",lwd=1) +
      theme(plot.title = element_text(hjust = 0.5)) + 
      labs(title = "tSNE plot",color="Species")
    
    image.png
    sessionInfo()
    R version 3.6.0 (2019-04-26)
    Platform: x86_64-w64-mingw32/x64 (64-bit)
    Running under: Windows 10 x64 (build 18363)
    
    Matrix products: default
    
    locale:
    [1] LC_COLLATE=Chinese (Simplified)_China.936 
    [2] LC_CTYPE=Chinese (Simplified)_China.936   
    [3] LC_MONETARY=Chinese (Simplified)_China.936
    [4] LC_NUMERIC=C                              
    [5] LC_TIME=Chinese (Simplified)_China.936    
    
    attached base packages:
    [1] stats     graphics  grDevices utils     datasets  methods   base     
    
    other attached packages:
    [1] scatterplot3d_0.3-41 ggplot2_3.2.0        Rtsne_0.15          
    [4] tsne_0.1-3          
    
    loaded via a namespace (and not attached):
     [1] Rcpp_1.0.5       pillar_1.4.2     compiler_3.6.0   tools_3.6.0     
     [5] digest_0.6.20    evaluate_0.14    tibble_2.1.3     gtable_0.3.0    
     [9] lattice_0.20-38  pkgconfig_2.0.2  rlang_0.4.7      Matrix_1.2-17   
    [13] rstudioapi_0.10  yaml_2.2.0       xfun_0.8         withr_2.1.2     
    [17] dplyr_0.8.3      knitr_1.23       grid_3.6.0       tidyselect_0.2.5
    [21] glue_1.3.1       R6_2.4.0         rmarkdown_1.13   irlba_2.3.3     
    [25] purrr_0.3.2      magrittr_1.5     scales_1.0.0     htmltools_0.3.6 
    [29] rsconnect_0.8.16 assertthat_0.2.1 colorspace_1.4-1 labeling_0.3    
    [33] lazyeval_0.2.2   munsell_0.5.0    crayon_1.3.4    
    

    相关文章

      网友评论

        本文标题:R语言可视化(十七):tSNE图绘制

        本文链接:https://www.haomeiwen.com/subject/ofyiuktx.html