Box plot for column of a table with categorical variables

80 views (last 30 days)
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
Achuan Chen
Achuan Chen on 13 Dec 2021
Update:
I've created a cell C, where column i of C = prices of variety i = i-th entry of S_apples.variety, so all I need to do now is to make a boxplot of C, and in theorey it will make a box plot for each column of C, and C has 8 columns for the 8 distinct varieties:
T=readtable('fruitvegprices.csv');
Error using readtable (line 498)
Unable to find or open 'fruitvegprices2.csv'. Check the path and filename or file permissions.
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,{'variety','price'})) %grouping the varieties together
C=cell(length(S_apples.variety),1);%column i=prices of variety i
for i=1:length(S_apples.variety)
[c,d]=ismember(A.variety,S_apples.(2){i});
for k=1:length(A(c,:).price)
C{k,i}=A(c,:).price(k);
end
end
C(find(cellfun(@isempty,C)))={nan};
boxchart(C)
However, it comes up with the message:
;
all the cell entries are integers so I don't see why there's a problem, and I've assigned NaN to the empty cells so this should work. What's gone wrong?
Thank you!
Walter Roberson
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.

Sign in to comment.

Accepted Answer

Cris LaPierre
Cris LaPierre on 14 Dec 2021
Edited: Cris LaPierre on 14 Dec 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)
data = 9148×6 table
category item variety date price unit _________ ________________ __________________ __________ _____ ____ fruit apples bramleys_seedling 2021-11-12 1.49 kg fruit apples coxs_orange_group 2021-11-12 1.09 kg fruit apples egremont_russet 2021-11-12 1.14 kg fruit apples braeburn 2021-11-12 1.01 kg fruit apples gala 2021-11-12 1.01 kg fruit apples other_early_season 2021-11-12 0.95 kg fruit pears conference 2021-11-12 1.04 kg fruit pears doyenne_du_comice 2021-11-12 1.08 kg fruit raspberries raspberries 2021-11-12 4.4 kg fruit strawberries strawberries 2021-11-12 6.24 kg vegetable beetroot beetroot 2021-11-12 0.51 kg vegetable brussels_sprouts brussels_sprouts 2021-11-12 0.98 kg vegetable pak_choi pak_choi 2021-11-12 2.72 kg vegetable curly_kale curly_kale 2021-11-12 3.26 kg vegetable cabbage red 2021-11-12 0.55 kg vegetable cabbage savoy 2021-11-12 0.5 head
% create table of just the apples
apples = data(data.item=="apples",:)
apples = 985×6 table
category item variety date price unit ________ ______ __________________ __________ _____ ____ fruit apples bramleys_seedling 2021-11-12 1.49 kg fruit apples coxs_orange_group 2021-11-12 1.09 kg fruit apples egremont_russet 2021-11-12 1.14 kg fruit apples braeburn 2021-11-12 1.01 kg fruit apples gala 2021-11-12 1.01 kg fruit apples other_early_season 2021-11-12 0.95 kg fruit apples bramleys_seedling 2021-11-05 1.56 kg fruit apples coxs_orange_group 2021-11-05 1.09 kg fruit apples egremont_russet 2021-11-05 1.18 kg fruit apples braeburn 2021-11-05 1.2 kg fruit apples gala 2021-11-05 1 kg fruit apples other_early_season 2021-11-05 0.95 kg fruit apples bramleys_seedling 2021-10-29 1.51 kg fruit apples coxs_orange_group 2021-10-29 1.24 kg fruit apples egremont_russet 2021-10-29 1.27 kg fruit apples braeburn 2021-10-29 1.12 kg
% Create boxchart
b = boxchart(removecats(apples.variety),apples.price);
b.Parent.TickLabelInterpreter = "none";
  3 Comments
Rebecca Stone
Rebecca Stone on 29 Jan 2022
If one of the categories is empty, how can we leave a space for the category that is missing/empty?
Walter Roberson
Walter Roberson on 29 Jan 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.

Sign in to comment.

More Answers (1)

Achuan Chen
Achuan Chen on 13 Dec 2021
Nevermind lol, think I'ved solved it using the cell2mat funciton. Cheers.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!