
How can I change the color of individual boxes in my boxchart?
18 views (last 30 days)
Show older comments
Hello,
I currently have a code that generates a tile of boxcharts based on similar groups of data. Each boxchart has the same 9 x-axis labels. In each plot of 9 boxplots, I was the first 3 of those box's face color to be one color, the next 3 another color, and the last 3 a third color (see example png picture for what I mean).
Currently, my code reads in an excel table of data, and then splits the table into the different sets of data based on the site (the 9 x-axis labels are for each sampling site I got my data from). Then it prompts the user to select which group of data they want boxcharts for. Then the code generates a group of boxcharts all tiled in one figure.
When these box charts are all generated they are all the same color. I want each individual boxchart to have the 3 different colors as shown in the .png. The sites with a 'B' in them (B1, B2, B3) would be one color, the sites with an 'M' are another color, and finally the sites with an 'I' are the third color.
Here is my code:
clear all; close all; clc; format shortG
warning off
%% Import Data
filename = 'Algae Data for Matlab Boxplots.xlsx';
T = readtable(filename);
%% Raw Data
% Pull all columns of data for the rows that correspond to each site
B1 = table2array(T(1:8,2:end));
B2 = table2array(T(9:16,2:end));
B3 = table2array(T(17:24,2:end));
I1 = table2array(T(25:32,2:end));
I2 = table2array(T(33:40,2:end));
I3 = table2array(T(41:48,2:end));
M1 = table2array(T(49:56,2:end));
M2 = table2array(T(57:64,2:end));
M3 = table2array(T(65:72,2:end));
% table column names and label names
ptitles = {'cyano','green','diatom','total_alg','cyano_percent','temp','pH','cond','turb','DO','NH3','NO3','PO4','TSS','TOC','ChlA','C_percent','N_percent','d13C','d15N','geosmin','MIB','microcystin','anatoxin','saxitoxin','phormidium','geo_qpcr','micro_qpcr','ana_qpcr','saxi_qpcr','TP','TN','mat','light'};
ptitles = string(ptitles);
ylab_param = {'Cyanobacteria (\mug/cm^2)', 'Green Algae (\mug/cm^2)','Diatoms (\mug/cm^2)', 'Total Algae (\mug/cm^2)', 'Cyanobacteria (% composition)', ['Temperature (' char(176) 'C)'], 'pH (-)' 'Conductivity (\muS/cm)', 'Turbidity (NTU)', 'Dissolved Oxygen (mg/L)', 'Ammonia (NH3, mg/L)', 'Nitrate (NO_3^-, mg/L)', 'Orthophosphate (PO_4^2-, mg/L)', 'TSS (mg/L)', 'TOC (mg/L)', 'Chlorophyll-\alpha (mg/L)', 'Carbon (%)', 'Nitrogen (%)', ['\delta13C (' char(8240) ')'], ['\delta15N (' char(8240) ')'], 'Geosmin (ng/L)', 'MIB (ng/L)', 'Microcystin (\mug/L)', 'Anatoxin-a (\mug/L)', 'Saxitoxin (\mug/L)', 'Phormidium (copies/mL)', 'Geosmin (copies/mL)', 'Microcystin (copies/mL)', 'Anatoxin-a (copies/mL)', 'Saxitoxin (copies/mL)', 'Total Phosphorus (mg/L)', 'Total Nitrogen (mg/L)', 'Mat Thickness (mm)', 'Light Penetration (%)'};
%% Prompt and BoxChart Generator
% Prompt to ask for which group of data you want generated into a tile of
% boxcharts
prop1 = 'Please enter the family of variables you want box plots made of.\nPlease choose between: \n1 - Algae \n2 - Physiochemical \n3 - Nutrients \n4 - Toxins \nPlease type the number.\n';
var = input(prop1,'s');
% Check for typos in the prompt
for j = 1:4
if strcmp(var,'1') || strcmp(var,'2') || strcmp(var,'3') || strcmp(var,'4')
j = 4;
else
prop2 = 'That is either a typo or not an available variable. Try again.\n';
var = input(prop2,'s');
j = j + 1;
end
end
% Loops to generate each tile of the boxcharts, which them looping through
% the different variables asked for
if strcmp(var, '1')
idx1 = [1 2 3 4 5 16 26 33]; %which parameters (columns of table) to plot that are related to the chosen variable group.
FigHandle = figure;
FigHandle.Position = [10 50 850 720];
t = tiledlayout(4,2);
t.TileSpacing = 'loose';
t.Padding = 'compact';
title(t,'Algae Data Points')
for k = 1:length(idx1)
nexttile
box1(k) = boxchart([B1(:,idx1(k)), B2(:,idx1(k)), B3(:,idx1(k)), M1(:,idx1(k)), M2(:,idx1(k)), M3(:,idx1(k)), I1(:,idx1(k)), I2(:,idx1(k)), I3(:,idx1(k))]);
xticklabels({'B1', 'B2', 'B3', 'M1', 'M2', 'M3', 'I1', 'I2', 'I3'})
ylabel(ylab_param(idx1(k)))
k = k+1;
end
end
I've removed the other 3 loops of the boxchart generator, so if you want to run this code, always select '1' for the prompt.
0 Comments
Answers (1)
See Also
Categories
Find more on Data Distribution 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!