######### PCA___3D图(ps:画出来就牛了,.......正在试验中..........哦豁................试验成功.........)
library(scatterplot3d)
## 在做完pca后的数据中将一二列删除,加上对应的品种一列,并在第一行中加上行名
dat = read.table("pca-3d.txt",header=T, stringsAsFactors = T )
## stringsAsFactors = T 以factor格式将非数字变量导入
## class(dat$Breed) 查看变量类型
#####(这个是你的数据中包含的品种,顺序和txt里的顺序要一致)
最终导入的数据大概是这样子
QQ截图20220521165840.png
# 颜色
####, "paleturquoise3","blue", "green3", "red1", "cyan", "orange",
### "magenta4", "yellowgreen", "darkorange3", "grey60", "black","red4", "purple3", "lawngreen"
color = c("mediumblue","darkred","green","dodgerblue1")
color <- color[as.numeric(dat$Breed)]
# 形状
shapes = c(1,2,3,4)
shapes <- shapes[as.numeric(dat$Breed)]
# 画图
scatterplot3d(dat[,1:3], main='PCA', type='p',
highlight.3d=F, angle=75, grid=T, box=T, scale.y=1,
cex.symbols=0.8 , col.grid='lightblue',
xlab = "PC1",ylab = "PC2",zlab = "PC3",
pch= shapes,
color = color)
# 图例
## pch和上面的shapes对应,col和 上面color对应即可
legend("topright", legend = levels(dat$Breed) ,
col = c("mediumblue","darkred","green","dodgerblue1"),
cex = 0.8, xpd = TRUE,inset = 0.03,ncol = 2, bty = "n",
pch= c(1,2,3,4))
### cex 修改字体, ### pt.cex 修改点的大小
### inset 调整图注位置, ### xpd 是否允许在作图区域外作图
### bty 图例框 是否画出,o为画出,n为不画出 ### ncol 图例分类的列数
### title 给图例添加标题 ### pch 点的类型
####输出多角度图
###把画图那部分替换了就行了
pdf("pca-3D.pdf", onefile = TRUE, width = 8, height = 8)
diffangle = function(ang){
scatterplot3d(dat[,1:3], main='PCA', type='p',
highlight.3d=F, angle=ang, grid=T, box=T, scale.y=1,
cex.symbols=0.8 , col.grid='lightblue',
xlab = "PC1",ylab = "PC2",zlab = "PC3",
pch= shapes,
color = color)
# 图例
## pch和上面的shapes对应,col和 上面color对应即可
legend("topright", legend = levels(dat$Breed) ,
col = c("mediumblue","darkred","green","dodgerblue1"),
cex = 0.8, xpd = TRUE,inset = 0.03,ncol = 2, bty = "n",
pch= c(1,2,3,4))
}
sapply(seq(-360,260,5),diffangle)
dev.off()
网友评论