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.
0 件のコメント
採用された回答
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
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")
その他の回答 (0 件)
参考
カテゴリ
Help Center および File Exchange で Data Distribution Plots についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!