clear
clc
close all

load irisdata

%% 2 numerical variables
sl = X(:,1);
sw = X(:,2);

% use scatter function
figure; scatter(sl, sw)
scatter(sl, sw, 'mo', 'filled')
xlabel('sepal length (cm)')
ylabel('sepal width (cm)')
set(gca, 'fontsize', 16)
title('Iris data','fontsize', 18)
box on

% alternatively, use plot function
figure; plot(sl, sw, 'm.', ...
    'markersize', 14)
xlabel('sepal length (cm)')
ylabel('sepal width (cm)')
set(gca, 'fontsize', 16)
title('Iris data','fontsize', 18)

% plot sl and sw separately in one figure
figure
plot(sl, 'm.', 'markersize', 14)
hold on
plot(sw, 'co','markersize', 14)
xlabel('index')
ylabel('value')
set(gca, 'fontsize', 16)
title('Iris data','fontsize', 18)
legend('sepal length', 'sepal width')
legend({'sepal length', 'sepal width'}, 'fontsize', 18)

%% 1 numercal and 1 categorical variable

figure; 
subplot(1,2,1)
boxplot(sl, class) 
%boxplot(sl,class,'orientation', 'horizontal')
set(gca, 'fontsize', 16)
ylabel('sepal length')
title('A side-by-side box plot', 'fontsize', 20)

subplot(1,2,2)
boxplot(sw, class) 
%boxplot(sl,class,'orientation', 'horizontal')
set(gca, 'fontsize', 16)
ylabel('sepal width')
title('Another side-by-side box plot', 'fontsize', 20)

%% 2 numerical and 1 categorical variables

class = categorical(class);
iris_types = categories(class);

figure;
subplot(1,2,1)
hold on
plot(sl(class==iris_types{1}),sw(class==iris_types{1}), 'rp', 'markersize', 16)
plot(sl(class==iris_types{2}),sw(class==iris_types{2}), 'bo', 'markersize', 16)
plot(sl(class==iris_types{3}),sw(class==iris_types{3}), 'm.', 'markersize', 16)
set(gca, 'fontsize', 16)
xlabel('sepal length')
ylabel('sepal width')
title('A 2-D scatterplot with labels', 'fontsize', 20)
legend(iris_types, 'fontsize', 16)
box on

% A much simpler way
subplot(1,2,2)
gscatter(sl, sw, class, 'rbm', 'po.', 16)
set(gca, 'fontsize', 16)
xlabel('sepal length')
ylabel('sepal width')
title('Identical plot created by gscatter', 'fontsize', 20)
legend(iris_types, 'fontsize', 16)
box on

%% scatterplot with 3 numerical variables
pl = X(:,3);
pw = X(:,4);

figure
plot3(sl, sw, pl, 'b.', 'markersize', 12)
set(gca, 'fontsize', 16)
xlabel('sepal length')
ylabel('sepal width')
zlabel('petal length')
title('A 3-D scatterplot', 'fontsize', 20)

%% 3-D scatterplot with labels
figure
hold on
colors = 'rbm';  % character array = string
symbols = 'po.';
for i = 1:numel(iris_types)
    subclass = (class==iris_types{i}); % indicator variable, logical
    plot3(sl(subclass),sw(subclass),pl(subclass), [colors(i) symbols(i)], 'markersize', 12)
end
set(gca, 'fontsize', 16)
xlabel('sepal length')
ylabel('sepal width')
zlabel('petal length')
title('A 3-D scatterplot with labels', 'fontsize', 20)
legend(iris_types, 'fontsize', 16)


% alternatively, one can use the scatter3 function
figure; 
scatter3(sl,sw,pl,40,class,'filled')
set(gca, 'fontsize', 16)
xlabel('sepal length')
ylabel('sepal width')
zlabel('petal length')


figure; 
scatter3(sl,sw,pl,40,pw,'filled') % color ~ pw value 
set(gca, 'fontsize', 16)
xlabel('sepal length')
ylabel('sepal width')
zlabel('petal length')

%% scatterplot matrix among all 4 numerical variabels

figure; plotmatrix(X)
title('scatterplot matrix', 'fontsize', 18)
var_names

figure; gplotmatrix(X,[],class)
title('scatterplot matrix with labels', 'fontsize', 18)
