美文网首页R语言做图生信绘图
ggraph优雅的绘制环状网络图

ggraph优雅的绘制环状网络图

作者: R语言数据分析指南 | 来源:发表于2022-05-12 09:33 被阅读0次

欢迎关注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")

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

数据获取

本节内容到此结束,喜欢的小伙伴欢迎分享转发,评论区留言交流可获取数据

相关文章

  • ggraph优雅的绘制环状网络图

    欢迎关注R语言数据分析指南 本节来介绍如何使用ggraph绘制圆形网络图,这里使用了一个R包ccgraph,各位观...

  • R语言可视化(四十):网络图绘制

    40. 网络图绘制 清除当前环境中的变量 设置工作目录 使用igraph包绘制网络图 使用ggraph包绘制网络图...

  • ggraph绘制网络图(1)

    最近有小伙伴问如何通过R绘制网络图,自己以前的确没有绘制网络图的需求,正好趁此机会学习一下,这篇文档介绍如何通过g...

  • R语言可视化(三十六):环状条形图绘制

    36. 环状条形图绘制 清除当前环境中的变量 设置工作目录 加载所需的R包 绘制基础环状条形图 绘制分组环状条形图...

  • 图片复现:ggraph互作网络图

    来复现一幅基因互作网络图,这种图一般使用cytoscape可以完成。这里我们使用ggraph简单复现一下。 (图片...

  • 学做NAR图表:ggraph做网络图

    最近在NAR上看到一篇文章: 原文网络图如下: 我们借此机会,通过ggraph作图解析相关参数。详细注释代码和参数...

  • 【网络图】绘制"上流"的网络图

    上篇学习了如何用Gephi绘制网络图,今天学习做个复杂点,一看就让人觉得是高水平文章的“上流”的网络图,学习绘制下...

  • 可视化:环状条形图

    前言   条形图算是很常见的数据展示图形之一。今天我们就来说说条形图相关的事情——绘制环状条形图。顾名思义,环状条...

  • Tableau-网络图、弧线图

    一、网络图 1.绘制简单的五点网络图: 拖动“X”至列,“Y”至行---勾选掉聚合度量---标记为“线”---拖动...

  • 小程序canvas上绘制图片真机不显示

    小程序在画布上绘制图片时真机不显示 分析:小程序的画布不能绘制网络图片,所以需要wx.downloadFile()...

网友评论

    本文标题:ggraph优雅的绘制环状网络图

    本文链接:https://www.haomeiwen.com/subject/yqjsurtx.html