微生物属与环境因子之间的相关性绘图,Spearman计算方法
数据结构:土壤理化性质和菌属之间有一一对应的site标号
得到的结果(部分展示)
配色方案(RGB以及对应的16进制)代码里用的是RGB
- RGB: (198, 91, 63) Hex: #C65B3F
- RGB: (222, 160, 147) Hex: #DEA093
- RGB: (237, 204, 197) Hex: #EDCCC5
- RGB: (215, 230, 234) Hex: #D7E6EA
- RGB: (119, 165, 179) Hex: #77A5B3
- RGB: (64, 128, 148) Hex: #408094
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt
import matplotlib.colors as mcolors
from scipy.stats import spearmanr
# Load the Excel files
genusbasic_df = pd.read_excel('path_to/Genusbasic.xlsx')
sampleinformation_df = pd.read_excel('path_to/Sampleinformation.xlsx')
# Merge the two dataframes on 'site' column
merged_df = pd.merge(genusbasic_df, sampleinformation_df, on='site')
# Drop the 'site' column as it is not needed for correlation analysis
merged_df = merged_df.drop(columns=['site'])
# Calculate the correlation matrix
correlation_matrix = merged_df.corr()
# Separate the correlation matrix into two parts: one for Genus vs soil properties
genus_columns = genusbasic_df.columns[1:]
soil_properties_columns = sampleinformation_df.columns[1:]
genus_soil_corr = correlation_matrix.loc[soil_properties_columns, genus_columns]
# Define the inverted custom colormap
inverted_cmap = mcolors.LinearSegmentedColormap.from_list("inverted_cmap",
[(64/255, 128/255, 148/255),
(119/255, 165/255, 179/255),
(215/255, 230/255, 234/255),
(237/255, 204/255, 197/255),
(222/255, 160/255, 145/255),
(198/255, 91/255, 63/255)])
# Plot the heatmap with the inverted colormap
plt.figure(figsize=(14, 10))
# Create the heatmap with the inverted custom colormap and circular cells
heatmap = sns.heatmap(genus_soil_corr, annot=True, cmap=inverted_cmap, vmin=-1, vmax=1, center=0, linewidths=.5, linecolor='grey', cbar_kws={'ticks': [-0.8, -0.6, -0.4, -0.2, 0, 0.2, 0.4, 0.6, 0.8]})
# Calculate p-values and add significance markers in a position to avoid covering values
for i in range(len(soil_properties_columns)):
for j in range(len(genus_columns)):
_, p_value = spearmanr(merged_df[soil_properties_columns[i]], merged_df[genus_columns[j]])
if p_value < 0.05:
if p_value < 0.01:
heatmap.text(j + 0.9, i + 0.7, '**', ha='center', va='center', color='black', fontsize=12)
else:
heatmap.text(j + 0.9, i + 0.7, '*', ha='center', va='center', color='black', fontsize=12)
# Adjust x-axis labels to 45 degrees
plt.xticks(rotation=45, ha='right')
# Adjust the plot to leave a small margin between axes and the plot
plt.title('Spearman Correlation Heatmap')
plt.xlabel('Genus')
plt.ylabel('Soil Properties')
plt.tight_layout(pad=2.0)
plt.show()