Box plot for column of a table with categorical variables
28 ビュー (過去 30 日間)
古いコメントを表示
I have a table of different varieties of apples and their prices.
I'm trying to create a box plot for each variety, with the variety names as the categorical variables and show them on the same chart.
Here's my attempt:
T=readtable('fruitvegprices2.csv');
items={'apples','pears','carrots','cabbage'};
%apples
[idx,ia] = ismember(T.(2),items{1});
T_apples = T(idx,{'item','variety'});% this gives table of apples alone
S_apples=unique(T_apples,'stable') %distinct varieties of apples
A=sortrows(T(idx,{'item','variety','price'})) %grouping the varieties together
var_order=S_apples.variety;
namedvar=categorical(A.price,1:8,var_order);
plot=boxchart(namedvar,A.price)
xlabel('Variety')
ylabel('Price')
The output I get is:

I don't know how to fix this, also I can't seem to turn off the Latex interpreter for the x-axis labels.
Could I get some help please?
Thank you!
6 件のコメント
Walter Roberson
2021 年 12 月 14 日
Are you sure you want your posts to be considered Spam ? That could result in Mathworks closing your account.
採用された回答
Cris LaPierre
2021 年 12 月 14 日
編集済み: Cris LaPierre
2021 年 12 月 14 日
Not sure what your solution was, but this is how I would create the boxchart
% Load data
opts = detectImportOptions('fruitvegprices.csv');
opts = setvartype(opts,["category","item","variety","unit"],"categorical");
data = readtable('fruitvegprices.csv',opts)
% create table of just the apples
apples = data(data.item=="apples",:)
% Create boxchart
b = boxchart(removecats(apples.variety),apples.price);
b.Parent.TickLabelInterpreter = "none";
3 件のコメント
Rebecca Stone
2022 年 1 月 29 日
If one of the categories is empty, how can we leave a space for the category that is missing/empty?
Walter Roberson
2022 年 1 月 29 日
There are two possibilities I see:
- you can use setcats() https://www.mathworks.com/help/matlab/ref/categorical.setcats.html . You would do this if you know ahead of time all of the valid categories, and want to discard anything not on the list and provide space for anything on the list that does not happen to have an entry in the data
- If you want to make sure that specific categories are represented without removing any categories you did not know about in advance, then you can use addcats() https://www.mathworks.com/help/matlab/ref/categorical.addcats.html . It is not documented, but my tests show that adding in an existing category is permitted and does not change the values -- so you do not need to filter out the categories already present before addcats()
Once you have done one of the above in order to add in missing categories, then reordercats() https://www.mathworks.com/help/matlab/ref/categorical.reordercats.html to get the categories in the order you want . If you want unexpected categories to be handled (present in the data) then you will need to create some logic to decide where they should go relative to the categories you do expect.
その他の回答 (1 件)
参考
カテゴリ
Help Center および File Exchange で Categorical Arrays についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!

