绘图中的3D Biplot-R [英] 3D Biplot in plotly - R

查看:103
本文介绍了绘图中的3D Biplot-R的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用plotly包构建3D PCA双图,因为该图非常漂亮并且可以html格式交互(这是我需要的东西).

I want to build a 3D PCA bi-plot using plotly package because the graph is nice and interactive in html format (something that I need).

我的困难是添加负载.我希望载荷以从(0,0,0)点开始的直线表示(即等价于2D双线图)

My difficulty is to add the loading. I want the loading to be presented as straight lines from the point (0,0,0) (i.e. the equivalent to 2D biplots)

总而言之,我不知道如何从3D图形的中心开始添加直线.

So all in all I don't know how to add straight lines starting from the centre of the 3D graph.

我已经使用PCA函数计算了得分和负荷;

I have calculated the scores and loading using the PCA function;

pca1 <- PCA (dat1, graph = F)

得分:

ind1 <- pca1$ind$coord[,1:3]
x <- ind1[,1] ; y <- ind1[,2] ; z <- ind1[,3]

用于加载:

var1 <- pca1$var$coord[,1:3]
xl <- var1[,1] ; yl <- var1[,2] ; zl <- var1[,3]

并使用下面的代码生成3D得分图;

and using the code bellow the 3D score plot is generated;

p <- plot_ly( x=x, y=y, z=z, 
 marker = list(opacity = 0.7, color=y  , colorscale = c('#FFE1A1', '#683531'), showscale = F)) %>% 
  layout(title = "3D Prefmap",
         scene = list(
           xaxis = list(title = "PC 1"), 
           yaxis = list(title = "PC 2"), 
           zaxis = list(title = "PC 3")))

推荐答案

以下是一些可能对3D双工图的开发有用的想法.

Here are some ideas that could be useful for the development of a 3D biplot.

# Data generating process
library(MASS)
set.seed(6543)
n <- 500
mu <- c(1,-2,3,-1,3,4)
Sigma <- diag(rep(1,length(mu)))
Sigma[3,1] <- Sigma[1,3] <- 0.1
Sigma[4,6] <- Sigma[6,4] <- 0.1
X <- as.data.frame(mvrnorm(n, mu=mu, Sigma=Sigma))

# PCA
pca <- princomp(X, scores=T, cor=T)

# Scores
scores <- pca$scores
x <- scores[,1]
y <- scores[,2]
z <- scores[,3]

# Loadings
loads <- pca$loadings

# Scale factor for loadings
scale.loads <- 5

# 3D plot
library(plotly)
p <- plot_ly() %>%
  add_trace(x=x, y=y, z=z,
            type="scatter3d", mode="markers",
            marker = list(color=y, 
               colorscale = c("#FFE1A1", "#683531"), 
               opacity = 0.7)) 

for (k in 1:nrow(loads)) {
   x <- c(0, loads[k,1])*scale.loads
   y <- c(0, loads[k,2])*scale.loads
   z <- c(0, loads[k,3])*scale.loads
   p <- p %>% add_trace(x=x, y=y, z=z,
            type="scatter3d", mode="lines",
            line = list(width=8),
            opacity = 1) 
}
print(p)

这篇关于绘图中的3D Biplot-R的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆