Plot Mean over Box Charts for two groups

18 次查看(过去 30 天)
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.

采纳的回答

Cris LaPierre
Cris LaPierre 2022-1-24
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 个评论
Cris LaPierre
Cris LaPierre 2022-1-25
编辑:Cris LaPierre 2022-1-25
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 2022-1-25
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 !!

请先登录,再进行评论。

更多回答(0 个)

类别

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

Community Treasure Hunt

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

Start Hunting!

Translated by