请求绘制暴露-中介-结局通路网络关系可视化图
中介效应路径可视化实现方案
需求说明
我有一组展示暴露→中介→结局关系的数据,包含1个暴露因素(体力活动)、多个炎症相关中介因素及1个结局(乳腺癌)。需要绘制路径可视化图,满足以下要求:
- 箭头按
direction变量(正相关/负相关/无关联)着色 - 箭头粗细随
exposure_int_es的绝对值调整 - 暴露节点标注为“Exposure”,结局节点标注为“Outcome”,中介节点保留原名称
- 箭头上标注效应量及95%CI文本
可复现数据集与关联方向判定代码
# 可复现数据集 data <- read.table( text = "Author Exposure Outcome Pathway exp_in_es_measure exposure_int_es exposure_int_lower exposure_it_higher exp_int_grade exp_int_n_studies Swain_CT_2023 Physical_activity CRP Inflammation SMD -0.27 -0.62 0.08 Low 12 Swain_CT_2023 Physical_activity TNF_alpha Inflammation SMD -0.63 -1.04 -0.22 Moderate 8 Swain_CT_2023 Physical_activity IL-6 Inflammation SMD -0.55 -0.97 -0.13 Moderate 11 Swain_CT_2023 Physical_activity Adiponectin Inflammation SMD 0.01 -0.14 0.17 High 5 Swain_CT_2023 Physical_activity Leptin Inflammation SMD -0.5 -1.1 0.09 Low 4 Lou_MWC_2023 CRP Breast_Cancer Inflammation RR 1.13 1.01 1.26 Moderate 16 Lou_MWC_2023 TNF_alpha Breast_Cancer Inflammation RR 0.84 0.57 1.1 Moderate 4 Lou_MWC_2023 IL-6 Breast_Cancer Inflammation RR 1.04 0.66 1.42 Very_low 3 Lou_MWC_2023 Adiponectin Breast_Cancer Inflammation RR 0.76 0.61 0.91 Moderate 8 Lou_MWC_2023 Leptin Breast_Cancer Inflammation RR 0.9 0.63 1.18 Low 8", header = TRUE ) # 定义关联方向判定函数 get_direction <- function(es_measure, lower, upper, effect_type) { if (effect_type == "SMD") { if (es_measure > 0 & lower > 0 & upper > 0) { return("Positive") } else if (es_measure < 0 & lower < 0 & upper < 0) { return("Negative") } else { return("Null") } } else if (effect_type == "RR") { if (es_measure > 1 & lower > 1 & upper > 1) { return("Positive") } else if (es_measure < 1 & lower < 1 & upper < 1) { return("Negative") } else { return("Null") } } } # 计算每条路径的关联方向 data$direction <- with(data, mapply(get_direction, exposure_int_es, exposure_int_lower, exposure_it_higher, effect_type = exp_in_es_measure))
可视化实现代码
使用tidygraph构建图数据,ggraph绘制路径图:
# 加载所需包 library(tidygraph) library(ggraph) library(dplyr) # 1. 整理节点数据:提取所有节点,设置标注 nodes <- data %>% select(Exposure, Outcome) %>% pivot_longer(cols = everything(), values_to = "name") %>% distinct(name) %>% mutate(label = case_when( name == "Physical_activity" ~ "Exposure", name == "Breast_Cancer" ~ "Outcome", TRUE ~ name )) # 2. 整理边数据:设置箭头属性,生成标注文本 edges <- data %>% mutate( # 计算箭头粗细:取效应量绝对值,做标准化处理 edge_width = abs(exposure_int_es) / max(abs(exposure_int_es)) * 3, # 生成效应量+95%CI标注文本 edge_label = paste0( ifelse(exp_in_es_measure == "SMD", round(exposure_int_es, 2), exposure_int_es), " (", round(exposure_int_lower, 2), "–", round(exposure_it_higher, 2), ")" ) ) %>% select(from = Exposure, to = Outcome, direction, edge_width, edge_label) # 3. 构建图对象 graph <- tbl_graph(nodes = nodes, edges = edges, directed = TRUE) # 4. 绘制路径图 ggraph(graph, layout = "linear", circular = FALSE) + # 绘制箭头 geom_edge_link(aes(color = direction, width = edge_width), arrow = arrow(type = "closed", length = unit(4, "mm")), end_cap = circle(5, "mm")) + # 绘制节点 geom_node_label(aes(label = label), size = 4, fill = "white", color = "black") + # 设置箭头颜色映射 scale_color_manual(values = c("Positive" = "red", "Negative" = "blue", "Null" = "gray")) + # 设置箭头粗细范围 scale_width_continuous(range = c(0.5, 3)) + # 调整主题:去掉坐标轴和背景 theme_graph() + labs(color = "关联方向") + # 调整布局,让节点水平排列 coord_cartesian(xlim = c(-0.5, nrow(nodes)-0.5))
代码说明
- 节点处理:将暴露和结局节点替换为要求的标注,中介节点保留原名称
- 箭头处理:用效应量绝对值的标准化值控制粗细,根据
direction变量设置颜色(红=正相关,蓝=负相关,灰=无关联) - 标注处理:自动生成包含效应量和95%CI的文本,添加在箭头上
- 布局:采用线性水平布局,清晰展示暴露→中介→结局的路径关系
内容的提问来源于stack exchange,提问作者user115916
相关产品推荐
相关产品推荐

