今天是2020年的最后一天,这一年多灾多难,让我们唏嘘不已。不论2020如何,生活仍要继续,希望即将到来的2021年,可以‘牛’转乾坤。
这也是2020年的最后一篇博客了,在这里给大家介绍一下。目前R语言的几种降维方式。
首先需要配置数据
data<-matrix(rnorm(3000),ncol=6)
colnames(data)=paste0('gene',1:6)
rownames(data)=c(paste0(rep('C',nrow(data)/2),1:(nrow(data)/2)),
paste0(rep('P',nrow(data)/2),1:(nrow(data)/2)))
head(data)
1、PCA降维
data.pca <- prcomp(t(data), scale. = TRUE)
head(data.pca$rotation)
pca_data=data.pca$rotation[,1:2]
pca_data=data.frame(sample=rownames(pca_data),
pca_data,
group=c(rep('C',nrow(data)/2),rep('P',nrow(data)/2)))
head(pca_data)
#绘图
library(ggplot2)
ggplot(data=pca_data,aes(x=PC1,y=PC2))+
geom_point(aes(colour=group,shape=group),size=2)+
theme(text=element_text(size=10),legend.title=element_blank(),
panel.background = element_rect(fill = "white", colour = "black",size = 0.2),
legend.key = element_rect(fill = "white", colour = "white"),
legend.background = (element_rect(colour= "white",fill = "white")))
2、非度量MDS降维
dis_data = dist(data,'euclidean',p=2)
mds_x = cmdscale(dis_data,k=2)
mds_x = data.frame(mds_x)
colnames(mds_x)=c('MDS.1','MDS.2')
head(mds_x)
mds_data=data.frame(sample=rownames(mds_x),
mds_x,
group=c(rep('C',nrow(data)/2),rep('P',nrow(data)/2)))
head(mds_data)
library(ggplot2)
ggplot(data=mds_data,aes(x=MDS.1,y=MDS.2))+
geom_point(aes(colour=group,shape=group),size=2)+
theme(text=element_text(size=10),legend.title=element_blank(),
panel.background = element_rect(fill = "white", colour = "black",size = 0.2),
legend.key = element_rect(fill = "white", colour = "white"),
legend.background = (element_rect(colour= "white",fill = "white")))
3、umap降维
library(umap)
data.umap = umap(data)
umap_data=data.umap$layout
colnames(umap_data)=c('umap_1','umap_2')
head(umap_data)
umap_data=data.frame(sample=rownames(umap_data),
umap_data,
group=c(rep('C',nrow(data)/2),rep('P',nrow(data)/2)))
head(umap_data)
library(ggplot2)
ggplot(data=umap_data,aes(x=umap_1,y=umap_2))+
geom_point(aes(colour=group,shape=group),size=2)+
theme(text=element_text(size=10),legend.title=element_blank(),
panel.background = element_rect(fill = "white", colour = "black",size = 0.2),
legend.key = element_rect(fill = "white", colour = "white"),
legend.background = (element_rect(colour= "white",fill = "white")))
4、tsne降维
#迭代次数 max_iter
#困惑度perplexity
#权衡速度与准确度,越小越精确,越大速度越快 theta
library(Rtsne)
#设置随机种子
set.seed(1500)
tsne_result <- Rtsne(data,
initial_dims = ncol(data),
pca = FALSE,
dims = 2,
check_duplicates = FALSE,
perplexity=30,
max_iter=2500,
theta=0.5)$Y
colnames(tsne_result)=c('tsne_1','tsne_2')
head(tsne_result)
tsne_data=data.frame(sample=rownames(data),
tsne_result,
group=c(rep('C',nrow(data)/2),rep('P',nrow(data)/2)))
head(tsne_data)
library(ggplot2)
ggplot(data=tsne_data,aes(x=tsne_1,y=tsne_2))+
geom_point(aes(colour=group,shape=group),size=2)+
theme(text=element_text(size=10),legend.title=element_blank(),
panel.background = element_rect(fill = "white", colour = "black",size = 0.2),
legend.key = element_rect(fill = "white", colour = "white"),
legend.background = (element_rect(colour= "white",fill = "white")))