ixxmu / mp_duty

抓取网络文章到github issues保存
https://archives.duty-machine.now.sh/
89 stars 24 forks source link

R优雅的自定义绘制代谢通路图 #2987

Closed ixxmu closed 1 year ago

ixxmu commented 1 year ago

https://mp.weixin.qq.com/s/7ERRGmixeYnMl79bvdBQJA

ixxmu commented 1 year ago

R优雅的自定义绘制代谢通路图 by 生信宝库

最近看到一个非常好的案例来分享一下,使用igraph,ggraph等R包来自定义绘制代谢通路pathway,原文文档链接见下方,数据可自行去官网下载。代码过程很是简洁,当然难点也许在于构建边文件与点文件。这需要各位观众老爷细细品味

原文文档

https://github.com/cxli233/ggpathway

有需要学习数据可视化的朋友欢迎加入小编2022年度VIP群,目前群内已经上传公众号文档「数据+代码约270篇」VIP交流群(1)已经500人满员,随着内容不断增多,为了更好的创做内容现在进群需「付费149元」 年底临近「从即日起至12月底无需转发文档到朋友圈积赞,可直接已129元的价格入群」

淘宝店铺个性化绘图服务

最近有部分观众老爷找小编做个性化绘图,为了给观众老爷提供更加优质的服务;小编开通了 「淘宝店铺(R语言数据分析指南)」 ,有特殊绘图需求的欢迎关注咨询(「由于小编时间精力有限有需求的请及时下单」)

加载R包

library(tidyverse)
library(igraph)
library(ggraph)
library(readxl)
library(viridis)
library(RColorBrewer)
library(rcartocolor)

构建边数据

example1_edge_table <- tribble(
  ~from, ~to,  ~label,
  "Glc6P""6P-gluconolactone",  "Glc6PHD",
  "6P-gluconolactone""6P-glucoconate",  "6P-gluconolactonase",
  "6P-glucoconate""Ru5P""6P-gluconateDH"
)
# 构建点数据
example1_nodes_table <- tribble(
  ~name, ~x,  ~y,
  "Glc6P"10,
  "6P-gluconolactone"20,  
  "6P-glucoconate"30,
  "Ru5P"40
)

文件整合

example1_network <- graph_from_data_frame(
  d = example1_edge_table,
  vertices = example1_nodes_table,
  directed = T
)

绘制基础通路图

ggraph(example1_network, layout = "manual"
       x = x, y = y) +
  geom_node_text(aes(label = name), hjust = 0.5) +
  geom_edge_link(aes(label = example1_edge_table$label), 
                 angle_calc = 'along',
                 label_dodge = unit(2'lines'),
                 arrow = arrow(length = unit(0.5'lines')), 
                 start_cap = circle(4'lines'),
                 end_cap = circle(4'lines')) +
  theme_void()  

绘制PPP代谢途径

导入数据

example2_edges <- read_excel("OPPP_edges.xlsx")
example2_nodes <- read_excel("OPPP_nodes.xlsx")

构建新标签

example2_nodes <- example2_nodes %>% 
  mutate(label = str_remove(name, "_\\d"))

文件整合

example2_network <- graph_from_data_frame(
  d = example2_edges,
  vertices = example2_nodes,
  directed = T)

绘制PPP代谢途径图

ggraph(example2_network, layout = "kk") +
  geom_node_point(size = 3, aes(fill = as.factor(carbons)), 
                  alpha = 0.8, shape = 21, color = "grey20") +
  geom_node_text(aes(label = label), hjust = 0.5, repel = T) +
  geom_edge_link(label_dodge = unit(2'lines'),
    arrow = arrow(length = unit(0.4'lines')), 
    start_cap = circle(1'lines'),
    end_cap = circle(2'lines')) +
  scale_fill_manual(values = carto_pal(7"Vivid")) +
  labs(fill = "Carbons") +
  theme_void()  

TCA途径

导入数据

example3_edges <- read_excel("TCA_cycle_edges.xlsx")
example3_nodes <- read_excel("TCA_cycle_nodes.xlsx")

构建标签文本

example3_nodes <- example3_nodes %>% 
  mutate(label = str_remove(name, "_\\d"))

整合数据

example3_network <- graph_from_data_frame(
  d = example3_edges,
  vertices = example3_nodes,
  directed = T)

绘制TCA途径图

ggraph(example3_network, layout = "manual",
       x = x, y = y) +
  geom_node_point(size = 3, aes(fill = as.factor(carbons)), 
                  alpha = 0.8, shape = 21, color = "black") +
  geom_edge_link(arrow = arrow(length = unit(0.4'lines')), 
                 start_cap = circle(0.5'lines'),
                 end_cap = circle(0.5'lines'), 
                 width = 1.1, alpha = 0.5) +
  geom_node_text(aes(label = label), hjust = 0.5, repel = T) +
  annotate(geom = "text", label = "TCA Cycle"
           x = 0, y = 0, size = 5, fontface = "bold") +
  scale_fill_manual(values = carto_pal(7"Vivid")) +
  labs(fill = "Carbons") +
  theme_void() +
  coord_fixed()

好了本节介绍到此结束,整个代码还是非常简洁的,喜欢的观众老爷欢迎分析转发,更多详细内容可以参看作者官方文档,「当然更推荐大家加入我的VIP交流群」一定让你感受到物超所值,「添加小编微信请备注来意,以便高效处理」

小编微信

关注下方公众号下回更新不迷路