Plot Mean over Box Charts for two groups

18 views (last 30 days)
I am running a piece of example code and trying to plot mean value over box chart for two groups.
For example, in 2015 case, it must plot mean values with trend line (yellow). Similarly in 2016 case it must plot another mean values with trend line (red).
I tried in the script but gettting problem in the positioning of mean values with trend line for both 2015 and 2016 group.
Here is the MATLAB example code:
%%%%%% Example Code
tbl = readtable('TemperatureData.csv');
monthOrder = {'January','February','March','April','May','June','July', ...
'August','September','October','November','December'};
tbl.Month = categorical(tbl.Month,monthOrder);
meanvalue = groupsummary(tbl.TemperatureF,tbl.Month,'mean');
boxchart(tbl.Month,tbl.TemperatureF,'GroupByColor',tbl.Year)
hold on
plot(meanvalue,'-o')
hold off
ylabel('Temperature (F)')
legend
Please help in the correction of script and required result.
Thank you in the anticipation.

Accepted Answer

Cris LaPierre
Cris LaPierre on 24 Jan 2022
See the 'Plot Mean Over Box Charts' example on the boxchart documentation page. For help with your specific code, please share your data file. You can attach it using the paperclip icon.
load patients
healthOrder = {'Poor','Fair','Good','Excellent'};
SelfAssessedHealthStatus = categorical(SelfAssessedHealthStatus, ...
healthOrder,'Ordinal',true);
meanWeight = groupsummary(Weight,SelfAssessedHealthStatus,'mean');
boxchart(SelfAssessedHealthStatus,Weight)
hold on
plot(meanWeight,'-o')
hold off
legend(["Weight Data","Weight Mean"])
  7 Comments
Cris LaPierre
Cris LaPierre on 25 Jan 2022
Edited: Cris LaPierre on 25 Jan 2022
Nice job, though the purple line seems to go beyond your actual data.
There is almost always a way to do something, it is usually just a matter of being creative with the information you have. Here, the actual XData is the month, so you need to shift off the true XData to align with the boxes (where there is more than one group). Here is one way to do that using the boxwidth property.
tbl = readtable('TemperatureData.csv');
monthOrder = {'January','February','March','April','May','June','July', ...
'August','September','October','November','December'};
tbl.Month = categorical(tbl.Month,monthOrder);
b = boxchart(tbl.Month,tbl.TemperatureF,'GroupByColor',tbl.Year);
meanvalue = groupsummary(tbl,["Month","Year"],{'mean','mean'},"TemperatureF");
% Because there is a different amount of data in each group
X2015 = 1:sum(meanvalue.Year==2015);
X2016 = 1:sum(meanvalue.Year==2016);
hold on
plot(X2015-b(1).BoxWidth/2, meanvalue.mean_TemperatureF(meanvalue.Year==2015),'-o')
plot(X2016+b(2).BoxWidth/2, meanvalue.mean_TemperatureF(meanvalue.Year==2016),'-^')
hold off
ylabel('Temperature (F)')
legend("2015","2016","mean 2015","mean 2016")
One page I have found helpful when working with tables is the Access Data in Tables page.
Sarfaraz Ahmed
Sarfaraz Ahmed on 25 Jan 2022
Hi Sir Cris,
So nice of you. you made my work easy. Now I can work on specific data by applying above concepts and if I find any trouble then hopefully will re-post or will ask in this thread.
Yes you are right, there is always a way to solve problem with the information what we have so far.
I am glad to work with you on solving my problem.
Thank you so much !!

Sign in to comment.

More Answers (0)

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!