PCA图叠加密度曲线

该代码示例展示了如何使用R中的ggplot2和其他相关包进行PCA主成分分析图和密度曲线图的绘制。首先,它导入了所需的数据,然后通过ggplot2创建了PCA图,用以展示不同疾病和队列在PC1和PC2上的分布。接着,绘制了PC1和PC2的密度曲线图,进一步解释变量的分布情况。最后,将主图与密度图组合在一起,形成了一个综合的可视化结果。
摘要由CSDN通过智能技术生成
加载R包
library(readxl)
library(tidyverse)
library(aplot)
library(vegan)
library(patchwork)
library(cowplot)
library(grid)
library(ggplotify)
导入数据
dat.taxa <- read_excel("F1.xlsx", sheet = "Fig 1a taxonomy PCA")
pca <- dat.taxa
绘制PCA图
Fig1a.taxa.pca <- ggplot(pca,aes(PC1,PC2))+
  geom_point(size=2,aes(color=Disease,shape=Cohort),show.legend = F)+ 
  scale_color_manual(values=c("#5686C3","#75C500"))+
  scale_shape_manual(values=c(16,15)) +
  stat_ellipse(aes(color = Disease),fill="white",geom = "polygon",
               level=0.95,alpha = 0.01,show.legend = F)+
  labs(x="PC1 (23.3%)",y="PC2 (9.1%)")+
  theme_classic()+
  theme(axis.line=element_line(colour = "black"),
        axis.title=element_text(color="black",face = "bold"),
                   panel.grid.major = element_blank(),
                   panel.grid.minor = element_blank(),
                   panel.background = element_blank(),
                   axis.text = element_text(color="black",size=10,face = "bold"))
绘制密度曲线图
pca$Group = paste(pca$Disease,"|", pca$Cohort,sep = "")

Fig1a.taxa.pc1.density <-
  ggplot(pca) +
  geom_density(aes(x=PC1, group=Group,fill=Disease,linetype=Cohort),
               color="black", alpha=0.6,position = 'identity',
               show.legend = F) +
  scale_fill_manual(values=c("#5686C3","#75C500")) +
  scale_linetype_manual(values = c("solid","dashed"))+
  scale_y_discrete(expand = c(0,0.001))+
  labs(x=NULL,y=NULL)+
  theme_classic() +
  theme(axis.text.x=element_blank(),
        axis.ticks.x = element_blank())

Fig1a.taxa.pc2.density <-
  ggplot(pca) +
  geom_density(aes(x=PC2, group=Group, fill=Disease, linetype=Cohort),
               color="black", alpha=0.6, position = 'identity',show.legend = F) +
  scale_fill_manual(values=c("#5686C3","#75C500")) +
  theme_classic()+
  scale_linetype_manual(values = c("solid","dashed"))+
  scale_y_discrete(expand = c(0,0.001))+
  labs(x=NULL,y=NULL)+
  theme(axis.text.y=element_blank(),
        axis.ticks.y=element_blank())+
  coord_flip()
拼图并转换类型
p1 <- Fig1a.taxa.pca %>% 
  insert_top(Fig1a.taxa.pc1.density,height = 0.3) %>% 
  insert_right(Fig1a.taxa.pc2.density,width=0.3) %>% 
  as.ggplot()

 尝试重新做

导入数据
dat.funct <- read_excel("F1.xlsx", sheet = "Fig 1a metagenome PCA")
pca <- dat.funct
绘制主图
Fig1a.function.pca <- ggplot(pca,aes(PC1,PC2))+
  geom_point(size=2, aes(col=Disease,shape=Cohort))+
  scale_shape_manual(values=c(16,15)) +
  scale_color_manual(values=c("#5686C3","#75C500")) +
  stat_ellipse(aes(color = Disease),fill="white",geom = "polygon",
               level=0.95,alpha = 0.01,show.legend = F)+
  labs(x="PC1 (17.3%)",y="PC2 (7.3%)")+
  theme_classic()+
  theme(axis.line = element_line(colour = "black"),
        axis.title=element_text(color="black",face = "bold"),
        axis.text=element_text(color="black",face = "bold",size=10),
                   panel.grid.major = element_blank(),
                   panel.grid.minor = element_blank(),
                   panel.background = element_blank())
绘制密度曲线图
pca$Group = paste(pca$Disease,"|", pca$Cohort,sep = "")

Fig1a.function.pc1.density <-
  ggplot(pca) +
  geom_density(aes(x=PC1, group=Group, fill=Disease, linetype=Cohort),
               color="black", alpha=0.6, position = 'identity',
               show.legend = F) +
  scale_fill_manual(values=c("#5686C3","#75C500")) +
  theme_classic()+
  scale_linetype_manual(values = c("solid","dashed"))+
  scale_y_discrete(expand = c(0,0.001))+
  labs(x=NULL,y=NULL)+
  theme_classic() +
  theme(axis.text.x=element_blank(),
        axis.ticks.x = element_blank())
        
Fig1a.function.pc2.density <-
  ggplot(pca) +
  geom_density(aes(x=PC2, group=Group,fill=Disease,linetype=Cohort),
               color="black", alpha=0.6,position='identity',
               show.legend = F) +
  scale_fill_manual(values=c("#5686C3","#75C500")) +
  scale_linetype_manual(values = c("solid","dashed"))+
  scale_y_discrete(expand = c(0,0.001))+
  labs(x=NULL,y=NULL)+
  theme_classic()+
  coord_flip()+
  theme(axis.text.y=element_blank(),
        axis.ticks.y=element_blank())
图形格式转换
p2 <- Fig1a.function.pca %>% 
  insert_top(Fig1a.function.pc1.density,height = 0.3) %>% 
  insert_right(Fig1a.function.pc2.density,width=0.3) %>% 
  as.ggplot()
总拼图
(p1|p2)+plot_layout(ncol=2,width = c(0.8,1))

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值