cover_image

跟着 Cell 学作图 | 主坐标分析(PCoA)及其可视化(vegan)

小小木舟 木舟笔记
2022年09月27日 02:30
图片
pcoa

「Title:」Targeted suppression of human IBD-associated gut microbiota commensals by phage consortia for treatment of intestinal inflammation

「DOI:」10.1016/j.cell.2022.07.003

本期图片

图片
fig

Principal coordinate analyses (PCoA), Bray-Curtis dissimilarity, colored according to (E) disease or (F) Kp abundance

示例数据和代码领取

点赞在看 本文,分享至朋友圈集赞20个保留30分钟,截图发至微信mzbj0002领取。

「木舟笔记2022年度VIP可免费领取」

木舟笔记2022年度VIP企划

「权益:」

  1. 「2022」年度木舟笔记所有推文示例数据及代码(「在VIP群里实时更新」)。

    图片
    data+code
  2. 木舟笔记「科研交流群」

  3. 「半价」购买跟着Cell学作图系列合集(免费教程+代码领取)|跟着Cell学作图系列合集

「收费:」

「99¥/人」。可添加微信:mzbj0002 转账,或直接在文末打赏。

图片

绘制

setwd('D:\\MZBJ\\Note\\NOTE\\55.PCOA')
# Load package
library(vegan)
library(ggplot2)
library(ggthemes)
# Load data
otu <- read.table('otu.txt',row.names = 1,header = T)
group <- read.table('group.txt',header = T)
# creat data
group$bacteria <- runif(55,0,20)
#pcoa
# vegdist函数,计算距离;method参数,选择距离类型
distance <- vegdist(otu, method = 'bray')
# 对加权距离进行PCoA分析
pcoa <- cmdscale(distance, k = (nrow(otu) - 1), eig = TRUE)

## plot data
# 提取样本点坐标
plot_data <- data.frame({pcoa$point})[1:2]

# 提取列名,便于后面操作。
plot_data$ID <- rownames(plot_data)
names(plot_data)[1:2] <- c('PCoA1''PCoA2')

# eig记录了PCoA排序结果中,主要排序轴的特征值(再除以特征值总和就是各轴的解释量)
eig = pcoa$eig

#为样本点坐标添加分组信息
plot_data <- merge(plot_data, group, by = 'ID', all.x = TRUE)
head(plot_data)

# figure1
ggplot(data = plot_data, aes(x=PCoA1, y=PCoA2, fill=group)) +
  geom_point(shape = 21,color = 'black',size=4) +
  scale_fill_manual(values = c('#73bbaf','#d15b64','#592c93'))+
  labs(x=paste("PCoA 1 (", format(100 * eig[1] / sum(eig), digits=4), "%)", sep=""),
       y=paste("PCoA 2 (", format(100 * eig[2] / sum(eig), digits=4), "%)", sep=""))+
  geom_hline(yintercept=0, linetype=4) +    
  geom_vline(xintercept=0 ,linetype=4)+          
  theme_few()+
  theme(legend.position = c(0.90.2),
        legend.title = element_blank(),
        legend.background = element_rect(colour ="black"))
ggsave('pcoa1.pdf',width = 4,height = 4)

# figure2
ggplot(data = plot_data, aes(x=PCoA1, y=PCoA2, fill=bacteria)) +
  geom_point(shape = 21,color = 'black',size=4) +
  scale_fill_gradient(low = '#f2fe32',high = '#180f7c')+
  labs(x=paste("PCoA 1 (", format(100 * eig[1] / sum(eig), digits=4), "%)", sep=""),
       y=paste("PCoA 2 (", format(100 * eig[2] / sum(eig), digits=4), "%)", sep=""))+
  geom_hline(yintercept=0, linetype=4) +    
  geom_vline(xintercept=0 ,linetype=4)+          
  theme_few()+
  theme(legend.title = element_blank(),
        legend.position = c(0.80.15),
        legend.direction = "horizontal")
ggsave('pcoa2.pdf',width = 4.5,height = 4)
        
图片
RESULT1
图片
result2

往期内容

  1. 即将满员!CNS图表复现|生信分析|R绘图 资源分享&讨论群!(内附推文合集)
  2. 跟着 Nature Communication 学作图 | 热图+格子注释(通路富集相关)
  3. 跟着 Nature Communication 学作图 | 百分比堆积柱状图+卡方检验
  4. ggbiplot | 带箭头的主成分分析(PCA)图绘制

图片
木舟笔记矩阵


跟着Cell学作图 · 目录
上一篇这图怎么画| 微生物网络图(microbial networks)下一篇国庆特惠 !| CNS图表复现|生信分析|R绘图 资源分享&讨论群!
继续滑动看下一个
木舟笔记
向上滑动看下一个