is it possible to mark significance levels using heatmap?

57 ビュー (過去 30 日間)
Emu
Emu 2024 年 10 月 30 日 16:58
コメント済み: Emu 2024 年 11 月 7 日 19:36
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.

採用された回答

Drew
Drew 2024 年 10 月 30 日 18:10
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;
  1 件のコメント
Emu
Emu 2024 年 11 月 7 日 19:36
Thank you ! both of these worked but this answer provided the format I wanted.

サインインしてコメントする。

その他の回答 (1 件)

Voss
Voss 2024 年 10 月 30 日 17:59
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)
data = 5×5
91.7670 66.2194 9.8986 83.5310 96.9522 63.0690 13.5828 47.6867 40.9138 7.2952 48.4783 92.8572 35.7252 97.9216 81.5112 10.8300 96.9977 41.8516 32.0662 9.2124 7.4585 28.4586 80.6638 16.3520 14.3313
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
p = 0.5*rand(5)
p = 5×5
0.3685 0.0560 0.2255 0.0459 0.0255 0.0094 0.0308 0.3124 0.0829 0.2498 0.3161 0.4574 0.4442 0.3291 0.2433 0.0540 0.3828 0.2185 0.2170 0.2821 0.1825 0.0940 0.0910 0.4831 0.2659
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
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')

製品


リリース

R2024b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by