graph绘制函数图像(ggraph优雅的绘制环状网络图)

欢迎关注R语言数据分析指南

本节来介绍如何使用「ggraph」绘制圆形网络图,这里使用了一个R包「ccgraph」,各位观众老爷细细体会,下面通过一个小例子来进行展示

加载R包

library(tidygraph) library(ggraph) library(tidyverse) library(magrittr) library(devtools) library(tidytext) library(tidygraph) library(RColorBrewer)

安装R包

install_github("gaospecial/ccgraph") library(ccgraph)

导入数据

otu <- read_tsv("otu_taxa_table.xls") %>% select(OTU:C5,taxonomy) %>% 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")

数据清洗

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_description_tf_idf country_index <- c("title","word") nodes_country <- gather_graph_node(title_description_tf_idf ,index = country_index, value = "n",root="phylum") edges_country <- gather_graph_edge(title_description_tf_idf ,index = country_index,root="phylum")

数据整合

graph_country <- tbl_graph(nodes_country,edges_country)

数据可视化

ggraph(graph_country,layout = 'dendrogram', circular = TRUE) geom_edge_diagonal(aes(color=node1.node.branch),alpha=1/3) geom_node_point(aes(size=node.size,color=node.branch),alpha=1/3) coord_fixed() theme_void() theme(legend.position = "none") scale_size(range = c(0.5,80)) geom_node_text(aes(x = 1.0175 * x,y = 1.0175 * y, label = node.short_name, angle = -((-node_angle(x, y) 90) %% 180) 90, filter = leaf,color = node.branch), size =3, hjust = 'outward') scale_colour_manual(values= rep( brewer.pal(9,"Paired") , 30)) geom_node_text(aes(label=node.short_name,filter = !leaf,color = node.branch), fontface="bold",size=4,family="Times")

graph绘制函数图像(ggraph优雅的绘制环状网络图)(1)

参考:https://github.com/gaospecial/ccgraph

数据获取

本节内容到此结束,喜欢的小伙伴欢迎转发此文档附上一句话到朋友圈「30分钟后后台截图给我」,即可获取对应的数据及代码

,

免责声明:本文仅代表文章作者的个人观点,与本站无关。其原创性、真实性以及文中陈述文字和内容未经本站证实,对本文以及其中全部或者部分内容文字的真实性、完整性和原创性本站不作任何保证或承诺,请读者仅作参考,并自行核实相关内容。文章投诉邮箱:anhduc.ph@yahoo.com

    分享
    投诉
    首页