Box plot for column of a table with categorical variables
Show older comments
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 Comments
Walter Roberson
on 13 Dec 2021
A=sortrows(T(idx,{'item','variety','price'})) %grouping the varieties together
sortrows() sorts first by the first column, which in this case would be 'item', not 'variety' ?
But you already know that T(idx,:) is only for apples, so it is not clear why you bother extracting and sorting on the item field, since the field should be the same for everything in the subset ?
Cris LaPierre
on 13 Dec 2021
Share your data file. You can attach it using the paperclip icon.
Achuan Chen
on 13 Dec 2021
Achuan Chen
on 13 Dec 2021
Walter Roberson
on 14 Dec 2021
Are you sure you want your posts to be considered Spam ? That could result in Mathworks closing your account.
Accepted Answer
More Answers (1)
Achuan Chen
on 13 Dec 2021
0 votes
Categories
Find more on Bar Plots in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
