2
$\begingroup$

I have a data frame (df) which has correlations calculated for different genes with respect to different ID combinations. I want to get separate histogram plot based on the gene name (separate plot for separate gene). Also after plotting I want to know which genes have more deviations in their correlation values (Like more deviated from the median in each plot).

Example: df

Gene    ID_1    ID_2    Correlation
TNF 1175077258  1175075956  0.626115
TNF 1175077261  1175075956  0.542499
TNF 1175077261  1175077258  0.50113
TNF 1175077263  1175075956  0.431587
TNF 1175077263  1175077258  0.734858
TNF 1175077263  1175077261  0.622206
CCL2    952399885   952399878   0.632703
CCL2    952399894   952399878   0.622244
CCL2    952399894   952399885   0.697313
CCL2    952399923   952399878   0.687826
CCL2    952399923   952399885   0.612089
CCL2    952399923   952399894   0.603834
IL10    1068768000  1068767927  0.118955
IL10    1068768147  1068767927  0.32038
IL10    1068768147  1068768000  0.430287
IL10    1068768409  1068767927  0.335264
IL10    1068768409  1068768000  0.406426
IL10    1068768409  1068768147  0.546452

I tried the below code which only works when combining all genes together.

import matplotlib.pyplot as plt
import seaborn as sns
plt.rcParams["figure.figsize"] = (8,6)

g1 = sns.histplot(df.Correlation)
g1.set(
    xlim=(-1,1),
    title="correlation"
)
g1.axvline(0.0, c='black', linestyle="--")
g1.axvline(df.Correlation.median(), c='red', linestyle="--")

The result/plot I am expecting is this (I have created this example plot in excel)

enter image description here

$\endgroup$

1 Answer 1

2
$\begingroup$

You could use the groupby() pandas function to group the dataframe by gene name. And then just loop through each group to plot the histogram of correlation values. I think you can use the mean of the ratio (deviation / median) to compare deviation for different genes:

import matplotlib.pyplot as plt

for name, group in df.groupby('Gene'):

    median = group['Correlation'].median()
    deviation = abs(group['Correlation'] - median)
    ratio = deviation / median
    ratio_mean = ratio.mean()

    id_1 = group['ID_1'].astype(str)
    id_2 = group['ID_2'].astype(str)

    group['Combination'] = id_1 + '/' + id_2

    plt.figure(figsize=(8, 6))
    plt.bar(group['Combination'], group['Correlation'])

    plt.xlabel('Combination', fontsize=12, fontweight='bold')
    plt.ylabel('Correlation', fontsize=12, fontweight='bold')

    plt.xticks(rotation=45, ha="right") 
    plt.title(f'{name} (deviation ratio: {ratio_mean.round(3)})')
    plt.tight_layout()

    plt.gca().spines['top'].set_visible(False)
    plt.gca().spines['right'].set_visible(False)
    
    plt.show()

CCL2

IL10

TNF

$\endgroup$
3
  • $\begingroup$ Is there a way to also get correlation matrix based on the above df data frame for each gene separately? Thank you $\endgroup$
    – Riya
    Jan 16, 2023 at 11:28
  • 1
    $\begingroup$ @Riya I think what you want is the corr() function for this: corr_matrix = group[['Combination', 'Correlation']].corr() $\endgroup$
    – Steve
    Jan 16, 2023 at 11:55
  • 1
    $\begingroup$ Thank you Steve $\endgroup$
    – Riya
    Jan 16, 2023 at 12:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.