heatMap = (
study_data,
heatX,
heatY,
scaling,
{
title=null,
height=null,
width=null,
}
) => {
const xDesc = colDescMap.has(heatX) ? colDescMap.get(heatX) : heatX;
const yDesc = colDescMap.has(heatY) ? colDescMap.get(heatY) : heatY;
const x = {
label: xDesc,
};
const y = {
label: yDesc,
reverse: true,
};
const pearsonCorrelation = (data, x, y) => {
let n = 0;
let xSum = 0;
let ySum = 0;
let xySum = 0;
let x2Sum = 0;
let y2Sum = 0;
data.forEach(d => {
const xVal = +d[x];
const yVal = +d[y];
if (!isNaN(xVal) && !isNaN(yVal)) {
n++;
xSum += xVal;
ySum += yVal;
xySum += xVal * yVal;
x2Sum += xVal * xVal;
y2Sum += yVal * yVal;
}
});
if (n === 0) return NaN;
const numerator = (n * xySum) - (xSum * ySum);
const denominator = Math.sqrt((n * x2Sum - xSum * xSum) * (n * y2Sum - ySum * ySum));
return numerator / denominator;
};
const correlationCoefficient = pearsonCorrelation(study_data, heatX, heatY);
return Plot.plot({
grid: true,
height: height || 400,
width: width || 600,
padding: 0,
marginLeft: 50,
x: x,
y: y,
color: {type: scaling, scheme: "Warm", legend: true},
title: title || `Correlation (${xDesc}, ${yDesc}): ${correlationCoefficient.toFixed(2)}`,
marks: [
Plot.cell(
study_data,
Plot.bin(
{fill: "count"},
{x: heatX, y: heatY}
)
),
]
});
}