Adding sigstar to grouped bar graph

42 次查看(过去 30 天)
Hanna Armstrong
Hanna Armstrong 2024-7-26
回答: Umar 2024-8-7,11:12
Hello,
I have sets of data that is being compared within each group. However, I need to display all of the groups on one graph so I can show how differences compare across various participants. Right now, this is the graph that I have. I would like to add sigmas between various blue, red, yellow bars depending of the results of the individual participant. Does anyone know how I could possible do that?
Thank you for your time!

回答(2 个)

Rushikesh
Rushikesh 2024-8-7,10:42
Hello Hanna,
Recently for one of my projects, I also wanted to add statistical significance indicators (sigmas) between bars to highlight significant differences. When reading problem you were facing, I understood that you want to add sigmas between bars of different groups (methods under which force measurements taken) for each participant using sigstar function.
I found out that to use sigstar method you need to download repository from MATLAB File Exchange since it is not built in function in MATLAB. Here is the repository link that can be useful
Once you download the repository, extract the downloaded files and move "sigstar.m" file to your MATLAB working directory.
we can import it using "addpath" function as shown below
addpath("sigstar.m")
Here I assume that you are using p-values from your statistical tests to determine the significance levels (e.g., p < 0.05, p < 0.01, etc.). Also I am using one asterisk (*) between the bars to indicate p < 0.05 and two asterisks (**) between the red and yellow bars to indicate p < 0.01.
Here is an example using sample data to add significance indicators to the plot
% Sample data for seven participants, each with three measurements (FP, NW, SW)
data = [
1500, 1400, 1300; % Participant 1
1600, 1550, 1500; % Participant 2
1700, 1650, 1600; % Participant 3
1800, 1750, 1700; % Participant 4
1900, 1850, 1800; % Participant 5
2000, 1950, 1900; % Participant 6
2100, 2050, 2000; % Participant 7
];
% Colors for the bars
colors = [0 0 1; 1 0 0; 1 1 0]; % Blue, Red, Yellow
% Create the bar chart
figure;
b = bar(data, 'grouped');
for k = 1:size(data, 2)
b(k).FaceColor = colors(k, :);
end
% Add legends
legend({'FP', 'NW based Calibration', 'SW based Calibration'}, 'Location', 'northwest');
% Example significance levels (assuming them as results from statistical tests)
% These are hypothetical p-values for comparisons between different bars for each participant
sigLevels = [
0.03, 0.02; % Participant 1
0.04, 0.01; % Participant 2
0.05, 0.03; % Participant 3
0.02, 0.01; % Participant 4
0.03, 0.04; % Participant 5
0.01, 0.02; % Participant 6
0.05, 0.03; % Participant 7
];
% Define pairs for significance indicators
sigPairs = {};
pValues = [];
for i = 1:size(data, 1)
if sigLevels(i, 1) < 0.05
sigPairs{end+1} = [i-0.2, i]; % Pair for FP vs NW
pValues(end+1) = sigLevels(i, 1);
end
if sigLevels(i, 2) < 0.05
sigPairs{end+1} = [i, i+0.2]; % Pair for NW vs SW
pValues(end+1) = sigLevels(i, 2);
end
end
% Add significance indicators using sigstar
sigstar(sigPairs, pValues);
% Customize the plot
ylim([0, 2200]); % Adjust y-axis limit to fit significance indicators
xticks(1:7);
xticklabels({'Participant 1', 'Participant 2', 'Participant 3', 'Participant 4', 'Participant 5', 'Participant 6', 'Participant 7'});
ylabel('Force (N)');
xlabel('Participants');
title('Force Measurements for Participants with Significance Indicators');
hold off;
I have tested above solution on MATLAB R2024a version.
Please let me know if this helps in your problem. Thanks.

Umar
Umar 2024-8-7,11:12

Hi @Hanna Armstrong ,

I do completely understand the reason of not sharing humongous amount of data . However, I have generated generic sample data to demonstrate the process. You can replace this with your actual data later.

% Sample data for three groups (blue, red, yellow) and four participants

data = [10 15 20; 12 18 22; 8 14 18; 11 16 21]; % Define the data matrix with values for each group and participant

Next, I plot the grouped bar graph using the bar function.

bar(data, 'grouped'); % Create a grouped bar graph using the data matrix legend('Blue', 'Red', 'Yellow'); % Add a legend to the graph for each group xlabel('Participants'); % Set the label for the x-axis ylabel('Values'); % Set the label for the y-axis title('Grouped Bar Graph'); % Set the title of the graph hold on; % Hold the current graph to add more elements

To add sigmas between the bars of different groups, I used the errorbar function which allows you to display error bars representing the variability or uncertainty in the data.

numGroups = size(data, 2); % Calculate the number of groups from the data matrix

numBars = size(data, 1); % Calculate the number of bars based on the data matrix

groupWidth = min(0.8, numBars/(numBars + 1.5)); % Calculate the width of each group in the bar graph

for i = 1:numBars

    x = (1:numGroups) - groupWidth/2 + (2*i-1) * groupWidth / (2*numBars); % Calculate the x positions for each group
    errorbar(x, data(i,:), zeros(1,numGroups), 'k', 'linestyle', 'none'); % Add error bars to the graph for each group

end

hold off; % Release the current graph to end the plotting

Please see attached plot.

Hope this helps. Please let me know if you have any further questions.

类别

Help CenterFile Exchange 中查找有关 Discrete Data Plots 的更多信息

产品

Community Treasure Hunt

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

Start Hunting!

Translated by