其他
ggraph包优雅的绘制网络图
欢迎关注R语言数据分析指南
❝本节来介绍如何基于微生物测序得到的OTU表绘制一个物种间网络图,下面通过一个小栗子来进行展示
❞
加载并安装R包
package.list=c("tidyverse","ggraph","tidygraph","magrittr","tidytext","widyr")
for (package in package.list) {
if (!require(package,character.only=T, quietly=T)) {
install.packages(package)
library(package, character.only=T)
}
}
导入数据
otu <- read_tsv("otu_taxa_table.xls") %>%
separate(taxonomy,
into=c("domain","phylum","class","order","family","genus","species"),sep=";") %>%
mutate_at(vars(c(`domain`:`species`)),~str_split(.,"__",simplify=TRUE)[,2]) %>%
column_to_rownames("OTU")
数据拆分
table <- otu %>% select_if(~is.numeric(.)) %>% rownames_to_column("ID")
tax <- otu %>% select_if(~!is.numeric(.)) %>% rownames_to_column("ID")
数据清洗
titles_node <- table %>% left_join(.,tax %>% select(1,phylum),by="ID") %>%
select(-ID) %>%
group_by(phylum) %>%
summarise(across(where(is.numeric), ~ sum(.x, na.rm=TRUE))) %>% select(1) %>%
distinct(phylum) %>%
filter(phylum!="") %>%
rowid_to_column("id")
title_description_tf_idf <- table %>% left_join(.,tax %>% select(1,phylum),by="ID") %>%
select(-ID) %>%
group_by(phylum) %>%
summarise(across(where(is.numeric), ~ sum(.x, na.rm=TRUE))) %>%
pivot_longer(-phylum) %>%
filter(phylum!="",value!=0) %>%
set_colnames(c("title","word","n")) %>%
bind_tf_idf(word, title , n)
计算相似度
title_similarity <- title_description_tf_idf %>%
pairwise_similarity(title, word, tf_idf, sort = TRUE)
整合边文件与点文件
titles_edges <- title_similarity %>%
left_join(titles_node, by = c("item1" = "phylum")) %>%
rename(from = id)
titles_edges %<>%
left_join(titles_node, by = c("item2" = "phylum")) %>%
rename(to = id)
titles_edges %<>% select(from, to, similarity)
数据格式转换
titles_graph <- tbl_graph(nodes = titles_node,
edges = titles_edges,directed = TRUE)
# A tbl_graph: 26 nodes and 466 edges
#
# A directed simple graph with 1 component
#
# Node Data: 26 x 2 (active)
id phylum
<int> <chr>
1 1 Acidobacteria
2 2 Actinobacteria
3 3 Armatimonadetes
4 4 Ascomycota
5 5 Bacteroidetes
6 6 Calditrichaeota
# ... with 20 more rows
#
# Edge Data: 466 x 3
from to similarity
<int> <int> <dbl>
1 24 12 0.996
2 12 24 0.996
3 20 12 0.935
# ... with 463 more rows
绘制网络图
set.seed(123)
ggraph(titles_graph,layout = "fr") +
geom_edge_fan0(aes(color = similarity))+
geom_node_point(shape=21,fill = "green",color="white",size=3) +
geom_node_text(aes(label = phylum), vjust = -1,hjust=0.5,size=3.5,color="black") +
scale_edge_color_gradientn(colours=colorRampPalette(c("#5eaaf5","#f4d963","red"))(10),
na.value="grey80")+
theme_graph()+
theme(legend.title = element_blank())
❝可以看到要绘制此图,涉及到的细节还是蛮多的;感兴趣的小伙伴可以对此文进行打赏后台将发送数据给您;或者也可以加入我的「付费绘图交流群」,付费99元将得到「一整年的200余篇文档的数据及代码」,同时还会配套精美的「Markdown文档 & 交互式绘图文档」方便各位学习,公众号右下角可添加小编微信
❞
欢迎大家扫描下方二位码加入「QQ交流群」
「关注下方公众号下回更新不迷路」,如需要加入微信交流群,请在菜单栏处添加作者微信,备注单位+方向+姓名即可邀您
2021年个人年度小总结
跟着PNAS学绘图-ggplot2绘制散点图添加渐变拟合曲线
ggplot2绘制渐变色散点图与折线图
ggplot2优雅的绘制点图
[热图-华夫图-瀑布图]用ggplot2一次性解决
ggplot2优雅的带你绘制中国地图
ggplot2绘制基因作用元件图