Plot Mean over Box Charts for two groups

조회 수: 20 (최근 30일)
Sarfaraz Ahmed
Sarfaraz Ahmed 2022년 1월 24일
댓글: Sarfaraz Ahmed 2022년 1월 25일
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