从矩阵制作3D图

时间:2018-05-07 00:37:19

标签: r 3d density-plot

我有2D matrix

cells                  Rpl37a    Itm2c   Atp1b1     Olfm1      Prnp   
1: AAACGGGCAATCTACG-1 1.7876878 2.393453 4.527764 2.0751658 2.3934526 
2: AAGTCTGCAGCGAACA-1 1.9235754 1.683439 4.034793 1.6834394 1.9235754 
3: ACAGCCGAGTACGTTC-1 1.7168081 2.422601 4.021788 2.0605259 2.0605259 
4: ACATCAGCAGGTTTCA-1 3.1849476 2.165849 4.139574 0.0000000 1.8161277 
5: ACCCACTAGTGTACCT-1 0.8960067 2.018112 4.774355 2.0181116 1.3608743 
6: ACCTTTAAGTGGTCCC-1 3.2418217 1.199632 3.185896 1.7294919 0.8718525 
7: ACGATACCAAGAAGAG-1 1.2513362 2.396079 3.977721 1.2513362 2.9158808 
8: ACGGGCTGTACAGTGG-1 3.5215001 1.710004 2.824437 2.0532873 2.5993292

对于每一列,我需要生成一个密度图,显示行值的分布,然后在一个3D图表上显示所有密度图。我怎么能实现这一目标?我知道有plotly库,但我是全新的,并且遇到了麻烦。

  

更新

我的目标是以某种方式同时查看所有发行版。我不知道如何实现这一目标。只有2D叠加的折线图不起作用,因为上面的矩阵我只是作为样本展示,实际上它有更多的行和列。我可能不仅需要密度图,还需要每列的折线图。

1 个答案:

答案 0 :(得分:1)

也许是这样,但我不相信。

dat <- as.data.frame(matrix(rnorm(5000*10), ncol=10))
names(dat) <- LETTERS[1:10]

xvalues <- apply(dat, 2, function(y) density(y)$x)
xmin <- min(xvalues)
xmax <- max(xvalues)
yvalues <- c(apply(dat, 1, function(a) density(a, from=xmin, to=xmax)$y))
dd <- cbind(c(yvalues), xvalues=c(xvalues), a = colnames(dat))

library(plotly)
plot_ly(data.frame(dd), x = ~xvalues, y = ~a, z = ~yvalues, split = ~a, 
        type = "scatter3d", mode = "lines") 

enter image description here

修改

更好,但仍然没有说服力。

dat <- as.data.frame(matrix(rnorm(150*10), ncol=10))
names(dat) <- LETTERS[1:10]

xvalues <- apply(dat, 2, function(y) density(y)$x)
xmin <- min(xvalues)
xmax <- max(xvalues)
yvalues <- c(apply(dat, 2, function(a) density(a, from=xmin, to=xmax)$y))

dd <- cbind(c(yvalues), xvalues=c(xvalues), a = colnames(dat))

library(plotly)

plot_ly(data.frame(dd), x = ~xvalues, y = ~a, z = ~yvalues, split = ~a, 
        type = "scatter3d", mode = "lines") 

enter image description here