is it possible to mark significance levels using heatmap?
조회 수: 57 (최근 30일)
이전 댓글 표시
채택된 답변
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개)
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)
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
참고 항목
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!