求R代码绘制昼夜节律数据环形箱线图
环形昼夜节律相位箱线图复刻方案
需求概述
- 复刻目标:某PubMed文献中的环形箱线图(原Python绘制,无公开代码)
- 数据结构:包含
sample(BAT、WAT、in vitro三类)、process(昼夜节律调控检测指标)、ct(24小时时间点,0/24点无缝衔接) - 可视化要求:为每个
sample生成独立图表,展示各process的相位分布,需包含中位数与IQR(四分位距)
数据示例
sample process ct BAT CellCycle 1.140128762 BAT CellCycle 3.106516768 BAT CellCycle 3.738113578 BAT CellCycle 23.44912305 BAT CellCycle 23.49744379 BAT CellCycle 23.51422441 BAT CellCycle 23.33041125 BAT CellCycle 22.19656322 BAT CellCycle 22.84183477 BAT ECM 22.04149961 BAT ECM 23.45526633 BAT ECM 23.36802574 BAT ECM 23.40688727 BAT ECM 1.732071528 BAT ECM 1.529777828 BAT ECM 20.29377917 BAT Energy 2.19647636 BAT Energy 4.177766894 BAT Energy 2.624942744 BAT Energy 1.633080723 BAT Energy 2.25815373 BAT Energy 22.52527935 BAT Energy 21.844178 BAT Energy 1.44484992 BAT Energy 1.710318176 BAT Energy 1.721398481 BAT Energy 5.0700794 BAT Energy 3.252122282 BAT Energy 19.69284419 BAT Energy 2.521538024 BAT Energy 2.309621407 BAT Energy 1.979057875 BAT Energy 1.747326389 BAT Energy 1.620003554 BAT Energy 4.512535826 BAT Energy 19.48505391 BAT Energy 3.96613478
R实现代码
基于tidyverse和ggplot2,处理0/24点衔接问题,生成环形箱线图:
# 加载依赖包 library(tidyverse) # 替换为你的完整数据集 df <- tibble( sample = rep("BAT", 38), process = c(rep("CellCycle",9), rep("ECM",7), rep("Energy",22)), ct = c(1.140128762,3.106516768,3.738113578,23.44912305,23.49744379,23.51422441,23.33041125,22.19656322,22.84183477, 22.04149961,23.45526633,23.36802574,23.40688727,1.732071528,1.529777828,20.29377917, 2.19647636,4.177766894,2.624942744,1.633080723,2.25815373,22.52527935,21.844178,1.44484992,1.710318176,1.721398481,5.0700794,3.252122282,19.69284419,2.521538024,2.309621407,1.979057875,1.747326389,1.620003554,4.512535826,19.48505391,3.96613478) ) # 处理时间点:转换为极坐标角度,同时解决0/24衔接问题 df <- df %>% mutate(ct_wrap = ifelse(ct > 22, ct - 24, ct)) # 将22点后的数据转换为负数值,实现环形衔接 # 按sample分组生成并保存图表 df %>% group_by(sample) %>% group_walk(function(data, group) { plot <- ggplot(data, aes(x = process, y = ct_wrap)) + geom_boxplot(width = 0.8, fill = "#69b3a2", alpha = 0.7) + coord_polar(start = -pi/2, direction = 1) + # 极坐标:0点对应顶部,顺时针旋转 scale_y_continuous( breaks = c(-2, 0, 2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 22), labels = c("22", "0", "2", "4", "6", "8", "10", "12", "14", "16", "18", "20", "22") ) + theme_minimal() + theme( panel.grid.major.y = element_line(color = "gray80"), panel.grid.major.x = element_blank(), axis.title = element_blank(), plot.title = element_text(hjust = 0.5, size = 16), axis.text.y = element_text(size = 10), axis.text.x = element_text(size = 12, face = "bold") ) + ggtitle(paste("相位分布 -", group$sample)) ggsave(paste0("环形箱线图_", group$sample, ".png"), plot, width = 8, height = 8, dpi = 300) print(plot) })
Python实现代码
基于matplotlib和seaborn,实现同效果的环形箱线图:
import pandas as pd import matplotlib.pyplot as plt import seaborn as sns import numpy as np # 替换为你的完整数据集 data = { 'sample': ['BAT']*38, 'process': ['CellCycle']*9 + ['ECM']*7 + ['Energy']*22, 'ct': [1.140128762,3.106516768,3.738113578,23.44912305,23.49744379,23.51422441,23.33041125,22.19656322,22.84183477, 22.04149961,23.45526633,23.36802574,23.40688727,1.732071528,1.529777828,20.29377917, 2.19647636,4.177766894,2.624942744,1.633080723,2.25815373,22.52527935,21.844178,1.44484992,1.710318176,1.721398481,5.0700794,3.252122282,19.69284419,2.521538024,2.309621407,1.979057875,1.747326389,1.620003554,4.512535826,19.48505391,3.96613478] } df = pd.DataFrame(data) # 处理时间点,解决0/24衔接问题 df['ct_wrap'] = df['ct'].apply(lambda x: x - 24 if x > 22 else x) # 按sample分组生成图表 for sample_name, group_data in df.groupby('sample'): fig, ax = plt.subplots(figsize=(8,8), subplot_kw={'projection': 'polar'}) # 映射process到极坐标位置 process_list = group_data['process'].unique() process_indices = np.arange(len(process_list)) process_to_idx = {p:i for i,p in enumerate(process_list)} group_data['pos'] = group_data['process'].map(process_to_idx) # 绘制箱线图 sns.boxplot( data=group_data, x='pos', y='ct_wrap', ax=ax, width=0.8, palette='viridis', alpha=0.7 ) # 极坐标配置:0点对应顶部,顺时针旋转 ax.set_theta_zero_location('N') ax.set_theta_direction(1) # 设置y轴刻度与标签(24小时制) ax.set_yticks([-2,0,2,4,6,8,10,12,14,16,18,20,22]) ax.set_yticklabels(['22','0','2','4','6','8','10','12','14','16','18','20','22']) # 设置x轴刻度为process名称 ax.set_xticks(process_indices * 2 * np.pi / len(process_list)) ax.set_xticklabels(process_list, fontsize=12, fontweight='bold') # 调整样式 ax.grid(True, axis='y', color='gray') ax.set_title(f'相位分布 - {sample_name}', y=1.1, fontsize=16) # 保存并展示 plt.savefig(f'环形箱线图_{sample_name}.png', dpi=300, bbox_inches='tight') plt.show()
内容的提问来源于stack exchange,提问作者Jen1984
相关产品推荐
相关产品推荐

