Box plot for column of a table with categorical variables

79 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
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
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!

Translated by