Displaying error bars on scatter plot (Bland-Altman plot)

49 views (last 30 days)
Hello all,
I have a scatter plot (Bland-Altman plot) showing the agreement between two sensors measuring knee angle for 1 gait cycle. I would like to plot the error bars in the y direction but I am having issues with this.
I want the initial HS one colour and then the final index which is HS another colour. I have plotted the mean and +-2SD to indicate the spread of the data.
I have then tried to plot the error bars which have not worked, please would you be able to advise how to do this.
Please see code below:
%% Average Bland-Altman plot
% Plot mean difference line
figure;
first_index = 1;
last_index = numel(mean_data);
scatter(mean_data(first_index), diff_data(first_index), 'filled', 'MarkerFaceColor', 'green');
hold on
scatter(mean_data(last_index), diff_data(last_index), 'filled', 'MarkerFaceColor', 'red');
hold on
scatter(mean_data(2:100), diff_data(2:100), 'filled','MarkerFaceColor', 'black');
hold on;
%+- 2SD
plot([-10 70], [mean_diff + 2*std_diff, mean_diff + 2*std_diff], 'r--');
plot([-10 70], [mean_diff - 2*std_diff, mean_diff - 2*std_diff], 'r--');
%mean diff line
plot([-10 70], [mean_diff, mean_diff], 'k-', 'LineWidth', 1);
%calculate error bars
error_bar_y = errorbar(mean_data,diff_data, 'vertical', 'LineStyle', 'none');
set(error_bar_y, 'color', 'k', 'LineWidth', 2)
legend({'HS_1', 'HS_2', 'Average gait cycle Data', '+ 2 SD', '- 2 SD', 'Mean Difference'});
hold on
xlim([-10 70])
ylim([-10 10])
xlabel('Mean($\o - \bar{\o}$) (deg)', 'Interpreter','Latex')
ylabel('Difference (deg)')

Accepted Answer

Star Strider
Star Strider on 3 Apr 2024 at 11:49
We are missing ‘mean_diff’ and ‘std_diff’ . Using rand to correct for that for now.
The errorbar plot needs 2 to 4 arguments, those being the ‘x’ (and optionally ‘y’ coordinates of the data), and the values for the error bars themselves (or the upper and lower errors if they are different). You have supplied only the ‘x’ and ‘y’ coordinates. I do not see any specific error s to plot. Supplying a vector for the errors solves that, although it would be better to have the correct data.
Try this —
matfiles = dir('*.mat');
for k = 1:numel(matfiles)
filename = matfiles(k).name
load(filename)
end
filename = 'diff_data.mat'
filename = 'mean_data.mat'
% whos
mean_diff = rand*10;
std_diff = rand;
errors = rand(size(mean_data)); % Create 'errors' Vector For 'errorbar'
figure;
first_index = 1;
last_index = numel(mean_data);
scatter(mean_data(first_index), diff_data(first_index), 'filled', 'MarkerFaceColor', 'green');
hold on
scatter(mean_data(last_index), diff_data(last_index), 'filled', 'MarkerFaceColor', 'red');
hold on
scatter(mean_data(2:100), diff_data(2:100), 'filled','MarkerFaceColor', 'black');
hold on;
%+- 2SD
plot([-10 70], [(mean_diff + 2*std_diff), (mean_diff + 2*std_diff)], 'r--');
plot([-10 70], [mean_diff - 2*std_diff, mean_diff - 2*std_diff], 'r--');
%mean diff line
plot([-10 70], [mean_diff, mean_diff], 'k-', 'LineWidth', 1);
%calculate error bars
error_bar_y = errorbar(mean_data,diff_data,errors, 'vertical', 'LineStyle', 'none');
set(error_bar_y, 'color', 'k', 'LineWidth', 2)
legend({'HS_1', 'HS_2', 'Average gait cycle Data', '+ 2 SD', '- 2 SD', 'Mean Difference'});
hold on
xlim([-10 70])
ylim([-10 10])
xlabel('Mean($\o - \bar{\o}$) (deg)', 'Interpreter','Latex')
ylabel('Difference (deg)')
.
  4 Comments
alexandra ligeti
alexandra ligeti on 3 Apr 2024 at 15:10
Hi, sorry me again.
I have now plotted the data but with the correct errors from the data set.
I have now noticed a very strange feature of the plot, would you be able to say why this may be? Seen in yellow, the upper limit of the error bars all seem to be sitting at 0. This does not seem correct?
Thanks again
Star Strider
Star Strider on 3 Apr 2024 at 16:34
As always, my pleasure!
I noticed that as well when I plotted it. The error bars appear to be symmetric with respect to their centres (the averages), so I believe they are correct. (I do not know how they were calculated.)

Sign in to comment.

More Answers (0)

Categories

Find more on Discrete Data Plots in Help Center and File Exchange

Products

Community Treasure Hunt

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

Start Hunting!