ixxmu / mp_duty

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

ggraph 绘制基因关系网络图 #3427

Closed ixxmu closed 1 year ago

ixxmu commented 1 year ago

https://mp.weixin.qq.com/s/NTl5EMcjgpSh_afMMHfJ4g

ixxmu commented 1 year ago

ggraph 绘制基因关系网络图 by 老俊俊的生信笔记


点击上方关注我们


1、故事发生



关于之前我在知识星球发布的作业,还是上次那个小朋友积极的交了作业,在这表示夸奖鼓励

复现的图:

这是群里一个小姐姐粉丝提出的问题,文章里用类似于 网络图 的形式展示基因间的相互关系以及相关性。可视化网络关系的软件有 igraphggraphcytoscape 等。

提交作业的小朋友用的是 ggraph 这个包,ggraph 结合了 ggplot 的语法,来对网络数据进行可视化。使得对于图形处理会更加方便。


2、交作业



以下是 ’竹鼠商‘ 的代码及图,这次名字应该对了:

library(igraph)
library(dplyr)
library(ggraph)

# 创建数据框
set.seed(23)
df <- data.frame(
  source = rep('HIF1A'17),
  target = c('HIF1A',
             'S100A2''PPARD''IGFL2''PACS1',
             'GRIN2D''KDM6A''ELAC1''TXLNG',
             'WDSUB1''PDIK1L''BCMO1''DNAJC28',
             'PIN4''LRRC31''INPP5J''NR0B2'),
  Cor =  c(1, runif(16, min = -0.4, max = 0.4))
)

# 原图中点的顺序是按照相关性红色过度到蓝色
df <- df %>%
  arrange(desc(Cor))


# 这里用到igraph构建的node的位置和ggraph构建的edge

# 使用igraph包构建点的位置矩阵,
network <- graph_from_data_frame(d = df, directed = F)
layout_matrix <- layout_in_circle(network)

# 将点的左右方向翻转
layout_matrix[,1] <- layout_matrix[,1] * -1

# 使用ggraph包构建位置矩阵,并替换为igraph的位置
df$source <- as.factor(df$source)
df$target <- as.factor(df$target)

edges <- data.frame(form = as.numeric(df$source),
                    to = as.numeric(df$target))
nodes <- data.frame(name = df$target)
g <- tbl_graph(nodes = nodes, edges = edges)
network_df <- create_layout(g, layout = 'linear', circular = TRUE)
network_df$x <- layout_matrix[,1]
network_df$y <- layout_matrix[,2]


ggraph(network_df) +
  geom_edge_arc(colour = 'black') +
  geom_node_text(aes(label = df$target),
                 size = 3,
                 vjust = c(0.5, ifelse(df$Cor[-1] < 01, -1) *2),
                 hjust = c(1.5, rep(0.516)))+
  geom_node_point(aes(fill = df$Cor),
                  shape = 21,
                  size = c(8, rep(616))) +
  scale_fill_gradient2(low = 'blue', mid = 'white', high = 'red',
                       limits = c(-0.50.5),
                       breaks = c(-0.4,0,0.4)) +
  xlim(-1.51.5) +
  ylim(-1.51.5) +
  theme_graph() +
  theme(legend.position = c(0.90.8),
        legend.text = element_text(family = 'serif'),
        legend.title = element_text(family = 'serif'),
        legend.key = element_rect(fill = 'red', color = 'red')) +
  guides(fill = guide_colourbar(title = 'Cor'))

图:

可以看到复现的已经是很像了。

那么我就借着代码给大家简要介绍一下。


3、代码优化及介绍



首先我们创建我们的数据,第三列是基因间的相关性:

# 加载R包
library(igraph)
library(dplyr)
library(ggraph)
library(tidygraph)

# 创建数据
df <- data.frame(
  from = rep('HIF1A'17),
  to = c('HIF1A',
         'S100A2''PPARD''IGFL2''PACS1',
         'GRIN2D''KDM6A''ELAC1''TXLNG',
         'WDSUB1''PDIK1L''BCMO1''DNAJC28',
         'PIN4''LRRC31''INPP5J''NR0B2'),
  Cor =  c(1, runif(16, min = -0.4, max = 0.4))
)

# 查看内容
head(df,3)

   from     to        Cor
1 HIF1A  HIF1A  1.0000000
2 HIF1A S100A2 -0.1793522
3 HIF1A  PPARD -0.2224022

按相关性排序:

# 按相关性排序
df <- df %>% arrange(desc(Cor))

然后构建节点的属性,再转为 graph 对象:

# 边
edges <- data.frame(from = df$from,to = df$to)

# 节点
nodes <- data.frame(name = df$to)

# 转换
g <- tbl_graph(nodes = nodes, edges = edges)

我们简单绘制一个:

# 简单绘制
ggraph(g,layout = 'linear', circular = T) +
  # 添加边
  geom_edge_link(width = 1) +
  # 添加节点
  geom_node_point(size = 10,color = '#CAFF70') +
  # 添加文字标签
  geom_node_text(aes(label = df$to),size = 5) +
  # 背景优化
  theme_void()

映射一下相关性:

# 把节点改成相关性来映射颜色
ggraph(g,layout = 'linear', circular = T) +
  # 添加边
  geom_edge_link(width = 1) +
  # 添加节点
  geom_node_point(size = 10,aes(color = df$Cor)) +
  # 添加文字标签
  geom_node_text(aes(label = df$to),size = 4.5) +
  # 修改颜色
  scale_color_gradient2(low = 'blue', mid = 'white', high = 'red',
                       limits = c(-0.50.5),
                       breaks = c(-0.4,0,0.4),
                       name = 'Cor') +
  # 背景优化
  theme_void() +
  # 设置图例位置
  theme(legend.position = c(0.9,0.8)) +
  # 设置绘图边界
  expand_limits(x = c(-1.51.5), y = c(-1.51.5))

是不是有点昨天 igraph 的味道了。

那么怎么把线变弯呢?我们使用 geom_edge_arc 函数绘制曲线:

# 绘制曲线
p <- ggraph(g,layout = 'linear', circular = T) +
  # 绘制曲线
  geom_edge_arc(color = 'black',edge_width = 0.7) +
  # 添加节点
  geom_node_point(size = 10,aes(color = df$Cor)) +
  # 添加文字标签
  geom_node_text(aes(label = df$to),size = 4.5) +
  # 修改颜色
  scale_color_gradient2(low = 'blue', mid = 'white', high = 'red',
                        limits = c(-0.50.5),
                        breaks = c(-0.4,0,0.4),
                        name = 'Cor') +
  # 背景优化
  theme_void() +
  # 设置图例位置
  theme(legend.position = c(0.9,0.8)) +
  # 设置绘图边界
  expand_limits(x = c(-1.51.5), y = c(-1.51.5))
p

可以使用 coord_flipscale_y_reverse 旋转图形,因为本质上这些图形都是位置坐标,转换一下即可:

# 旋转
p +
  coord_flip() +
  scale_y_reverse()

接下来我们改变点形状,填充颜色,调整文字标签的位置:

# 改变点形状、大小,填充颜色,调整标签位置
ggraph(g,layout = 'linear', circular = T) +
  # 绘制曲线
  geom_edge_arc(color = 'black',edge_width = 0.7) +
  # 添加节点
  geom_node_point(aes(fill = df$Cor),shape= 21,
                  # 点不同大小
                  size = c(12,rep(8,16))) +
  # 添加文字标签
  geom_node_text(aes(label = df$to),size = 4.5,
                 vjust = c(0.5, ifelse(df$Cor[-1] < 01, -1)*2.5),
                 hjust = c(1.5, rep(0.516))) +
  # 修改颜色
  scale_fill_gradient2(low = 'blue', mid = 'white', high = 'red',
                        limits = c(-0.50.5),
                        breaks = c(-0.4,0,0.4),
                        name = 'Cor') +
  # 背景优化
  theme_void() +
  # 设置图例位置
  theme(legend.position = c(0.9,0.8)) +
  # 设置绘图边界
  expand_limits(x = c(-1.51.5), y = c(-1.51.5)) +
  coord_flip() +
  scale_y_reverse()

还可以把相关性映射给边,如果我们需要修改颜色,使用 scale_colour_manual 是没有用的,粉丝找到了 scale_edge_color_manual 函数对修改对应属性的颜色:

# 修改边颜色
ggraph(g,layout = 'linear', circular = T) +
  # 绘制曲线
  geom_edge_arc(aes(color = df$to),edge_width = 0.7,
                # 不显示图例
                show.legend = F) +
  # 添加节点
  geom_node_point(aes(fill = df$Cor),shape= 21,
                  # 点不同大小
                  size = c(12,rep(8,16))) +
  # 添加文字标签
  geom_node_text(aes(label = df$to),size = 4.5,
                 vjust = c(0.5, ifelse(df$Cor[-1] < 01, -1)*2.5),
                 hjust = c(1.5, rep(0.516))) +
  # 修改颜色
  scale_fill_gradient2(low = 'blue', mid = 'white', high = 'red',
                       limits = c(-0.50.5),
                       breaks = c(-0.4,0,0.4),
                       name = 'Cor') +
  # 修改边颜色
  scale_edge_color_manual(values = rainbow(17)) +
  # 背景优化
  theme_void() +
  # 设置图例位置
  theme(legend.position = c(0.9,0.8)) +
  # 设置绘图边界
  expand_limits(x = c(-1.51.5), y = c(-1.51.5)) +
  coord_flip() +
  scale_y_reverse()

以上都是圆形的布局格式,我们还可以换成线性的:

# 线性布局
ggraph(g,layout = 'linear', circular = F) +
  # 绘制曲线
  geom_edge_arc(aes(color = df$to),edge_width = 0.7,
                # 不显示图例
                show.legend = F) +
  # 添加节点
  geom_node_point(aes(fill = df$Cor),shape= 21,
                  # 点不同大小
                  size = c(12,rep(8,16))) +
  # 添加文字标签
  geom_node_text(aes(label = df$to),angle = 65,hjust = 1,nudge_y = -0.4) +
  # 修改颜色
  scale_fill_gradient2(low = 'blue', mid = 'white', high = 'red',
                       limits = c(-0.50.5),
                       breaks = c(-0.4,0,0.4),
                       name = 'Cor') +
  # 修改边颜色
  scale_edge_color_manual(values = rainbow(17)) +
  # 背景优化
  theme_void() +
  # 设置图例位置
  theme(legend.position = 'top') +
  # 设置绘图边界
  expand_limits(x = c(-1.51.5), y = c(-1.51.5))

换个黑色:



收官!


代码 我上传到 QQ 群 老俊俊生信交流群 文件夹里。欢迎加入。加我微信我也拉你进 微信群聊 老俊俊生信交流群 哦。

群二维码:


老俊俊微信:




知识星球:



所以今天你学习了吗?

欢迎小伙伴留言评论!

点击我留言!

今天的分享就到这里了,敬请期待下一篇!

最后欢迎大家分享转发,您的点赞是对我的鼓励肯定

如果觉得对您帮助很大,赏杯快乐水喝喝吧!

推 荐 阅 读




ixxmu commented 1 year ago

比较完整的一个操作, 相关性的一对多都可以这样尝试

ixxmu commented 1 year ago

对应更高级的版本 https://mp.weixin.qq.com/s/DGuoIkHgeVIFy5mVHh5-xA