%% Display Bubbles in Different Axes on the Same Scale
% Define two sets of data that show the contamination levels of a certain toxin across different towns on the east and west sides of a certain metropolitan area. Define towns1 and towns2 as the populations across the towns. Define nsites1 and nsites2 as the number of industrial sites in the corresponding towns. Then define levels1 and levels2 as the contamination levels in the towns.

towns1 = randi([25000 500000],[1 30]);
towns2 = towns1/3;
nsites1 = randi(10,1,30);
nsites2 = randi(10,1,30);
levels1 = (5 * nsites2) + (7 * randn(1,30) + 20);
levels2 = (3 * nsites1) + (7 * randn(1,30) + 20);

%
% Create a tiled chart layout so you can visualize the data side-by-side. Then create an axes object in the first tile and plot the data for the west side of the city. Add a title and axis labels. Then, repeat the process in the second tile to plot the east side data.

tiledlayout(1,2,'TileSpacing','compact')

% West side
ax1 = nexttile;
bubblechart(ax1,nsites1,levels1,towns1);
title('West Side')
xlabel('Number of Industrial Sites')

% East side
ax2 = nexttile;
bubblechart(ax2,nsites2,levels2,towns2);
title('East Side')
xlabel('Number of Industrial Sites')
ylabel('Contamination Level')

%
% Reduce all the bubble sizes to make it easier to see all the bubbles. In this case, change the range of diameters to be between 5 and 30 points.

bubblesize(ax1,[5 30])
bubblesize(ax2,[5 30])

%
% The west side towns are three times the size of the east side towns, but the bubble sizes do not reflect this information in the preceding charts. This is because the smallest and largest bubbles map to the smallest and largest data points in each of the axes. To display the bubbles on the same scale, define a vector called alltowns that includes the populations from both sides of the city. Use the bubblelim function to reset the scaling for both charts. Next, use the xlim and ylim functions to display the charts with the same x- and y-axis limits.

% Adjust scale of the bubbles
alltowns = [towns1 towns2];
newlims = [min(alltowns) max(alltowns)];
bubblelim(ax1,newlims)
bubblelim(ax2,newlims)

% Adjust x-axis limits
allx = [xlim(ax1) xlim(ax2)];
xmin = min(allx);
xmax = max(allx);
xlim([ax1 ax2],[xmin xmax]);

% Adjust y-axis limits
ally = [ylim(ax1) ylim(ax2)];
ymin = min(ally);
ymax = max(ally);
ylim([ax1 ax2],[ymin ymax]);

% Copyright 2020 The MathWorks, Inc.