heatmap help - how to display grouped data across 3 levels (LO, MED, HI) ?
4 views (last 30 days)
Show older comments
How can I show 3 levels in a heatmap (LO MED, HI) and not just two (FALSE, TRUE for example) across n=4 GROUPS for a single outcome variable (continuous variable)? And is it possbile to (also) show % of total for the OUTCOME variable by GROUP? and not just mean or median.
Here is some working code and a dataset, but displays only two levels (LO, HI) for each GROUP:
load CHRX.mat % loads as CH = Chelsea; RX = Roxbury
P = prctile(CH.VLFN, 75);
isVHI = CH.VLFN>=P==1;
VHI = isVHI; % this is the HIGH level
P1 = prctile(CH.VLFN, 25);
isVLO = CH.VLFN<=P1==1;
VLO = isVLO; % thius is the LOW level
isIQR = ~VHI&~VLO==1;
IQR = isIQR; % this is the MEDIUM level
CH1 = addvars(CH, VLO); CH2 = addvars(CH1 ,IQR); CH3 = addvars(CH2, VHI)
%% HEATMAPS
% note: ImpactWind is the GROUP categorical variable (n=4)
% note: UFPConc is the OUTCOME, continuous variable or ColorVariable
format shortG
data = CH3; % Chelsea
figure()
h = heatmap(data,'ImpactWind','VHI','ColorVariable','UFPConc')
h.YDisplayLabels = {'LO','HI'}
0 Comments
Accepted Answer
Voss
on 10 Jan 2024
You can make a variable that has three distinct values, say 0 for LO, 1 for MEDIUM, and 2 for HI, which can be constructed as follows:
level = 2*VHI + IQR;
and then add that variable to your table and create the heatmap based on that.
load CHRX.mat % loads as CH = Chelsea; RX = Roxbury
VHI = CH.VLFN >= prctile(CH.VLFN, 75); % this is the HIGH level
VLO = CH.VLFN <= prctile(CH.VLFN, 25); % this is the LOW level
IQR = ~VHI & ~VLO; % this is the MEDIUM level
level = VHI*2+IQR;
CH = addvars(CH,level);
%% HEATMAPS
% note: ImpactWind is the GROUP categorical variable (n=4)
% note: UFPConc is the OUTCOME, continuous variable or ColorVariable
format shortG
data = CH; % Chelsea
figure()
h = heatmap(data,'ImpactWind','level','ColorVariable','UFPConc');
h.YDisplayLabels = {'LO','MED','HI'};
More Answers (1)
William Rose
on 10 Jan 2024
Make a single variable Vstat with three categories: LOW, MED, HIGH, rather than three separate variables.
load CHRX.mat % loads as CH = Chelsea; RX = Roxbury
Phi = prctile(CH.VLFN, 75);
Plo = prctile(CH.VLFN, 25);
[Vstat{1:height(CH),1}]=deal('MED'); % initialize Vstat
for i=1:height(CH)
if CH.VLFN(i)<Plo, Vstat{i}='LOW';
elseif CH.VLFN(i)>Phi, Vstat{i}='HIGH';
end
end
data=addvars(CH,Vstat); % Chelsea
%% HEATMAPS
% note: ImpactWind is the GROUP categorical variable (n=4)
% note: UFPConc is the OUTCOME, continuous variable or ColorVariable
format shortG
figure()
h = heatmap(data,'ImpactWind','Vstat','ColorVariable','UFPConc');
h.YDisplayLabels = {'HIGH','MED','LOW'};
There's probably a more elegant way to do it than the for loop I use below, but my way seems to work. Good luck.
See Also
Categories
Find more on Data Distribution Plots in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!