is it possible to mark significance levels using heatmap?
17 views (last 30 days)
Show older comments
I have a matrix with my data and a matrix with pvalues, is it possible to overlay pvalues under a specified threshold (i.e. .05) onto the heatmap, using a symble (e.g. *), or even the text value.
0 Comments
Accepted Answer
Drew
on 30 Oct 2024
Text cannot be a child of HeatmapChart, so as an alternative, could use imagesc.
If this answer helps you, please remember to accept the answer.
% Example data matrix and p-value matrix
dataMatrix = rand(10); % Replace with your data matrix
pValueMatrix = rand(10); % Replace with your p-value matrix
% Threshold for p-values
pValueThreshold = 0.1;
% Create a figure and axes
figure;
imagesc(dataMatrix); % Use imagesc to display the data matrix as a heatmap
colormap(sky);
colorbar;
% Get the size of the data matrix
[numRows, numCols] = size(dataMatrix);
% Loop through each element in the matrix
for row = 1:numRows
for col = 1:numCols
% Display the data value
dataValueStr = num2str(dataMatrix(row, col), '%.2f');
text(col, row, dataValueStr, 'HorizontalAlignment', 'center', ...
'VerticalAlignment', 'bottom', 'Color', 'black', 'FontSize', 10);
% Check if the p-value is below the threshold
if pValueMatrix(row, col) < pValueThreshold
% Overlay a symbol or p-value text on the heatmap
% Use '*' for symbol or num2str(pValueMatrix(row, col)) for text
pValueStr = '*'; % Change to num2str(pValueMatrix(row, col)) to show p-value
text(col, row, pValueStr, 'HorizontalAlignment', 'center', ...
'VerticalAlignment', 'top', 'Color', 'red', 'FontSize', 12);
end
end
end
% Set axis labels and title
xlabel('Columns');
ylabel('Rows');
title('Heatmap with Data Values and Significant P-values');
% Adjust the axis to display correctly
set(gca, 'XTick', 1:numCols, 'YTick', 1:numRows);
axis equal tight;
More Answers (1)
Voss
on 30 Oct 2024
As far as I know, there is no built-in option to conditionally alter the heatmap data labels, but what you can do is create a heatmap with no data labels and create an additional invisible axes on top containing text objects that you can manipulate at will.
Example:
data = 100*rand(5)
p = 0.5*rand(5)
h = heatmap(data,'CellLabelColor','none');
str = compose('%0.4g\np=%0.2g',data(:),p(:));
idx = p(:) < 0.05;
str(idx) = strcat(str(idx),'*');
ax = axes( ...
'Visible','off', ...
'Units',h.Units, ...
'Position',h.Position, ...
'YDir','reverse');
[x,y] = meshgrid(categorical(h.XData),categorical(h.YData));
text(ax,x(:),y(:),str,'HorizontalAlignment','center')
0 Comments
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!