跟着Nature学绘图(6) PCA分析图表可视化

2022-09-21 14:34:48 浏览数 (1)

❝本节来继续进行论文图表复现,主要还是基于「ggplot2」来进行数据可视化 ❞

❝近期系统整理了一下公众号所写过的一些经典文档,如果需要获取全部代码的欢迎加入小编的VIP交流群,「付费99元,周期一年」群内同步上传代码, 公众号右下角添加微信咨询即可 ❞

以往案例

本节来复现文章中的Fig3-h

安装并加载R包

代码语言:javascript复制
package.list=c("tidyverse","ggrepel","factoextra","RColorBrewer")

for (package in package.list) {
  if (!require(package,character.only=T, quietly=T)) {
    install.packages(package)
    library(package, character.only=T)
  }
}

导入数据

代码语言:javascript复制
df <- read_tsv("F3.xls")

PCA分析

代码语言:javascript复制
pca <- df %>% column_to_rownames(var="Sample_id") %>% 
  select(-Subtype) %>% prcomp(.,scale. = TRUE)
每个主成分解释的总方差
代码语言:javascript复制
var_explained <- pca$sdev^2/sum(pca$sdev^2)

数据可视化

代码语言:javascript复制
fviz_pca_biplot(pca, axes = c(1, 2),geom.ind = c("point"),geom.var = c("arrow", "text"),
                pointshape = 20,pointsize=4,
                label ="var",repel = TRUE,col.var = "grey50",
                labelsize=0.5,
                col.ind = df$Subtype)  
  scale_color_manual(values = colorRampPalette(brewer.pal(12,"Paired"))(4)) 
  labs(x=paste0("(PC1: ",round(var_explained[1]*100,2),"%)"),
       y=paste0("(PC2: ",round(var_explained[2]*100,2),"%)"),
       title="PCA-Biplot") 
  theme(panel.background = element_rect(fill = 'white', colour = 'black'),
        axis.title.x = element_text(colour="black",size = 12,margin = margin(t=12)),
        axis.title.y = element_text(colour="black",size = 12,margin = margin(r=12)),
        axis.text=element_text(color="black"),
        plot.title = element_text(size=12,colour = "black",hjust=0.5,face = "bold"),
        legend.title = element_blank(),
        legend.key=element_blank(),   # 图例键为空
        legend.text = element_text(color="black",size=9), # 定义图例文本
        legend.spacing.x=unit(0.1,'cm'), # 定义文本书平距离
        legend.key.width=unit(0.2,'cm'), # 定义图例水平大小
        legend.key.height=unit(0.2,'cm'), # 定义图例垂直大小
        legend.background=element_blank(), # 设置背景为空
        legend.box.background=element_rect(colour="black"), # 图例绘制边框
        legend.position=c(1,0),legend.justification=c(1,0))

0 人点赞