Box plot for column of a table with categorical variables
28 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
Achuan Chen
il 13 Dic 2021
Commentato: Walter Roberson
il 29 Gen 2022
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 Commenti
Walter Roberson
il 14 Dic 2021
Are you sure you want your posts to be considered Spam ? That could result in Mathworks closing your account.
Risposta accettata
Cris LaPierre
il 14 Dic 2021
Modificato: Cris LaPierre
il 14 Dic 2021
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 Commenti
Rebecca Stone
il 29 Gen 2022
If one of the categories is empty, how can we leave a space for the category that is missing/empty?
Walter Roberson
il 29 Gen 2022
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.
Più risposte (1)
Vedere anche
Categorie
Scopri di più su Categorical Arrays in Help Center e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!

