%% New in r2020b: Two New Marker Symbols
% Author: Adam Danz
% Sept 30, 2020
% https://www.mathworks.com/matlabcentral/highlights
%
% The Matlab r2020b release introduces the new horizontal ('_') and vertical ('|') line marker symbols that are centered around the coordinate similarly to the plus marker ('+').
%% Show new markers

% Set up marker symbols & properties
syms = 'ox|_';
markerSz = [120 160 200 400 800];
colors = [
    .55 .55 .55; 
    .55 .55 .55;
    .00 .00 1.0;
    1.0 .00 .00];
alphas = [.5 .5 1 1];
markerNames = ["Circle","Cross","Vertical line","Horizontal line"];

% Plot symbols
figure()
ax = axes();
hold(ax,'on')
h = gobjects(1, numel(syms)+1);
h(1) = scatter(ax, 1:numel(markerSz), zeros(size(markerSz))+.6, markerSz, [.5 0 .5], '+', ...
    'LineWidth', 2, 'MarkerEdgeAlpha', alphas(1), 'DisplayName', 'Plus sign');
scatter(ax, 1:numel(markerSz), zeros(size(markerSz))+.6, markerSz, colors(1,:), 'o', ...
    'LineWidth', 2, 'MarkerEdgeAlpha', alphas(1))
for i = 1:numel(syms)
    h(i+1) = scatter(ax, 1:numel(markerSz), zeros(size(markerSz)), markerSz, colors(i,:), syms(i),...
        'LineWidth', 2, 'MarkerEdgeAlpha', alphas(i), 'DisplayName', markerNames(i));
end
axis(ax,'equal')
xlim(ax,[0.5,numel(markerSz)+.5])
ylim(ax,[-.35,.95])
set(ax, 'XTick', 1:numel(markerSz), 'XTickLabel', compose('%d',markerSz),'YTick',[])
ax.YAxis.Color = 'none';
xlabel(ax, 'Marker size (points)')
title(ax, 'Marker symbol comparison')
legend(ax,h,'Location', 'EastOutside','Box','off');

%% Use the horizontal line marker in a bar plot

% Select year (2002-2014)
yr = 2005; 
% Read in built-in data set as time table
TT = table2timetable(readtable('outages.csv'));
% Select chosen year and add quarter
TT(year(TT.OutageTime) ~= yr,:) = []; 
TT.Quarter = quarter(TT.OutageTime);
% Compute number of power outages in each region for selected year and each quarter
[regionID, regions] = findgroups(TT.Region); 
nOutages = accumarray(regionID,1); 
nOutages(:,2:5) = cumsum(cell2mat(splitapply(@(q){accumarray(q,1,[4,1])},TT.Quarter,regionID)'),1)'; 
% Plot results
fig = figure(); 
ax = axes(fig); 
hold(ax, 'on')
bar(ax,categorical(regions),nOutages(:,1),'DisplayName',num2str(yr));
hh = plot(ax,categorical(regions),nOutages(:,2:end), '_', 'MarkerSize', 50, 'LineWidth',3);
set(hh, {'DisplayName'}, compose('Q%d',1:4)')
legend(ax,'LineWidth',0.7);
xlabel(ax, 'US Regions')
ylabel(ax, 'Number of outages')
title(ax, 'Annual power outages in USA by region')

%
%% Compare the increase in COVID-19 case with the increase in testing

% Read in dataset
% Source: https://github.com/owid/covid-19-data/tree/master/public/data
T = readtable('owid-covid-data.csv');

% Isolate the month of August
T(T.date < datetime(2020,08,01) | T.date >= datetime(2020,09,01), :) = [];

% Remove international & world data
T(strcmpi(T.location, 'international'),:) = [];
T(strcmpi(T.location, 'world'),:) = [];

% Remove countries with >100M people
T(T.population < 1e+08,:) = [];

% Remove countries that haven't reported number of tests
[countryGroup0, countryISOcode0] = findgroups(T.iso_code);
countryRmIdx = splitapply(@(x)all(isnan(x)),T.new_tests,countryGroup0);
T(ismember(T.iso_code, countryISOcode0(countryRmIdx)),:) = [];
fprintf(['Countries that meet population criteria but were eliminated for ',...
    'not having test-numbers: %s\n'],strjoin(countryISOcode0(countryRmIdx)))
% Create timetables of number of new infections for each country
[countryGroup, countryISOcode] = findgroups(T.iso_code);
newCasesbyCountry = splitapply(@(a,b){timetable(a,b)},T.date,T.new_cases,countryGroup);
TTnewCases = synchronize(newCasesbyCountry{:});
TTnewCases.Properties.VariableNames = countryISOcode;
TTnewCases.Properties.DimensionNames{1} = 'date';

% Find days where number of new cases increased from previous report
casesIncreased = diff(fillmissing(TTnewCases{:,:},'previous'))>0;

% Create timetables of number of tests for each country
newTestsbyCountry = splitapply(@(a,b){timetable(a,b)},T.date,T.new_tests,countryGroup);
TTnewTests = synchronize(newTestsbyCountry{:});
TTnewTests.Properties.VariableNames = countryISOcode;
TTnewTests.Properties.DimensionNames{1} = 'date';

% Find days where number of tests increased from previous report
testsIncreased = diff(fillmissing(TTnewTests{:,:},'previous'))>0;

% Plot days where cases increased
[dayIdx_cases, countryIdx_cases] = find(casesIncreased);
[dayIdx_tests, countryIdx_tests] = find(testsIncreased);
ISOs = categorical(countryISOcode);

fig = figure();
ax = axes(fig); 
hold(ax, 'on')
h = gobjects(1,2); 
h(1) = plot(ax, ISOs(countryIdx_cases), TTnewCases.date(dayIdx_cases+1), 'k|','DisplayName','Increase in cases');
h(2) = plot(ax, ISOs(countryIdx_tests), TTnewCases.date(dayIdx_tests+1), 'k_','DisplayName','Increase in tests');
xlabel(ax, 'Country (ISO code)')
ylim(ax,[datetime(2020,08,01),datetime(2020,09,01)])
set(ax,'TickDir', 'out', 'YTick', min(ylim):days(3):max(ylim),'YMinorTick','on')
ax.YAxis.MinorTickValues = min(ylim):max(ylim);
title(ax,'COVID-19 Data for August 2020', 'Countries with population >100M ')
% Add daily ref lines
yl = arrayfun(@(y)yline(ax,y,'-','color',[.15 .15 .15],'Alpha',.15),(min(ylim):max(ylim))+hours(12));
% Add legend
lg = legend(ax,h,'Location', 'SouthOutside','Orientation','horizontal','LineWidth',0.601);
title(lg, 'Change from previous day')
% Indicate source of data next to legend & lock with legend position
ax2 = axes('Visible','off');
fig.ResizeFcn = @(~,~)set(ax2,'Position',[sum(lg.Position([1,3])),lg.Position(2),.1,lg.Position(4)]);
fig.ResizeFcn()
th(1) = text(ax2, .05, 1, 'Data source:');
th(2) = text(ax2, .05, .65, 'Our World in Data'); 
th(3) = text(ax2, .05, .3, '$\mathrm{<github>}$','color',[0 0 .8],'Interpreter','latex');
set(th, 'VerticalAlignment', 'Top', 'HorizontalAlignment','left','FontSize',9)
th(3).ButtonDownFcn = @(~,~)web('https://github.com/owid/covid-19-data/tree/master/public/data'); % Countopen website

%% Create Community Highlight thumbnail image

syms = 'o+*.x_|sd^v><pho'; % 'o' twice to fill square
[x,y] = ndgrid(1:4,1:4);
fig = figure('Color', 'W');
fig.Position([3:4]) = [300,300];
hold on
h = arrayfun(@(m)scatter(x(m), y(m), 500, syms(m)),1:numel(syms));
set(h, {'MarkerFaceColor'}, arrayfun(@(h){h.CData},h)','LineWidth',3)
axis equal
xlim([.5,4.5])
ylim([.5,4.5])
title({'r2020b','Two new markers'},'FontSize',18)
a = annotation('textbox',[.33 .295 .35 .15],'LineWidth',3, ...
    'HorizontalAlignment', 'Center', 'FontSize', 16);
axis off