Plot Mean over Box Charts for two groups

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

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

Hi Cris, Thank you for the reply.
Actually I have gone through this example but this one is for only 1 data set . In the above exapmle there are two data sets (2015 and 2016) data sets. it's required to plot mean trend line in both groups to make comparision. I have attached a generated plot from my script, however that one is not seems correct location of mean plot. can you please have a look ?
Thank you.
The picture makes it look like you have working code, but without your dataset, it's hard for me to conclude anything specific. However, if you have two different groups of data, you should probably have two separate lines plotting the mean.
load patients
healthOrder = {'Poor','Fair','Good','Excellent'};
SelfAssessedHealthStatus = categorical(SelfAssessedHealthStatus, ...
healthOrder,'Ordinal',true);
[meanWeight,BG] = groupsummary(Weight,{SelfAssessedHealthStatus,Gender},'mean');
boxchart(SelfAssessedHealthStatus,Weight,'GroupByColor',Gender)
hold on
plot(meanWeight(1:2:end),'-o')
plot(meanWeight(2:2:end),'-^')
hold off
legend(["Weight Data (F)","Weight Data (M)","Weight Mean (F)","Height Mean (M)"])
Thank you for your reply.
Actually, I am trying to mold a given example for mean plot on the box chart . The given code here is:
%%%%%% 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);
boxchart(tbl.Month,tbl.TemperatureF,'GroupByColor',tbl.Year)
ylabel('Temperature (F)')
legend
'TemperatureData.csv' is a builtin data set provided by MATLAB. Once I succeed plotting mean over box chart (on the proper position) for multiple groups then I will work on my specific data and I can share that file as well. As you showed, a similar mean plot is required in the the example code for both 2015 and 2016 data sets ('TemperatureData.csv').
Thank you.
Ok, so next step is to make an attempt at adding the plots of the means from the 2 examples shared already.
Hi Sir Cris,
I have made an attemp by editing above code for plotting mean values on the box chart and it works fine except deviating mean values (outside the box chart). I mean is it possible that mean value should show inside the box like in the patient data example with 1 data group set. I understand that is one group so mean value can be plotted inside the box. can we do that with multiple groups ? so that mean value can be shown inside the box and contnously ploting line till the end. The modified code is here:
tbl = readtable('TemperatureData.csv');
monthOrder = {'January','February','March','April','May','June','July', ...
'August','September','October','November','December'};
yearOrder = [2015,2016];
tbl.Month = categorical(tbl.Month,monthOrder);
tbl.Year = categorical(tbl.Year,yearOrder);
[meanvalue,BS] = groupsummary(tbl.TemperatureF,{tbl.Month,tbl.Year},'mean');
boxchart(tbl.Month,tbl.TemperatureF,'GroupByColor',tbl.Year)
hold on
plot(meanvalue(1:2:end),'-o')
plot(meanvalue(2:2:end),'-^')
hold off
ylabel('Temperature (F)')
legend
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.
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

Community Treasure Hunt

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

Start Hunting!