LESSON: Bar charts

FOCUS QUESTION: How can I show proportions and relative sizes of different data groups?

This lesson takes you through different ways of visualizing data using bar charts.

 In this lesson you will: Create bar charts to represent data. Use stacked and side-by-side bar chart styles. Employ vertical and horizontal bar orientations. Combine vectors into larger arrays. Colormaps for changing color schemes.

DATA FOR THIS LESSON

 File Description NYCDiseases.mat The data set contains the monthly totals of the number of new cases of measles, mumps, and chicken pox for New York City during the years 1931-1971. The file is organized into the following variables: measles - an array containing the monthly cases of measles mumps - an array containing the monthly cases of mumps chickenPox - an array containing the monthly cases of chicken pox years - a vector containing the years 1931 through 1971 The data was extracted from the Hipel-McLeod Time Series Datasets Collection, available at http://www.stats.uwo.ca/faculty/aim/epubs/mhsets/readme-mhsets.html. The data was first published in: Yorke, J.A. and London, W.P. (1973). "Recurrent Outbreaks of Measles, Chickenpox and Mumps", American Journal of Epidemiology, Vol. 98, pp. 469.

SETUP FOR BAR CHARTS LESSON

• Create a BarCharts directory on your V: drive and make it your current directory.
• Create a BarChartsLesson script file in your BarCharts directory.

Create a new cell in which you type and execute:

```   load NYCDiseases.mat;    % Load the disease data
```

You should see measles, mumps, chickenPox, and years variables in the Workspace Browser.

EXAMPLE 2: Calculate individual and overall monthly totals (combining columns)

Create a new cell in which you type and execute:

```   measlesByMonth = sum(measles, 1);   % Monthly totals of measles
mumpsByMonth = sum(mumps, 1);       % Monthly totals of mumps
CPByMonth = sum(chickenPox, 1);     % Monthly totals of chicken pox
byMonth = [measlesByMonth', mumpsByMonth', CPByMonth']; % Make columns
```

You should see 4 variables in the Workspace Browser:

• measlesByMonth - row vector of 12 elements containing monthly measles totals
• mumpsByMonth - row vector of 12 elements containing the monthly mumps totals
• CPByMonth - row vector of 12 elements containing the monthly chicken pox totals
• byMonth - an array with 12 rows and 3 columns holding monthly totals for the three diseases.

EXAMPLE 3: Create a bar chart of the measles monthly case totals (bar(Y))

Create a new cell in which you type and execute:

```   figure                                    % Create a new figure
bar(measlesByMonth./1000)                 % Bar chart of monthly totals
xlabel('Month')                           % Label the x-axis
ylabel('Cases (in thousands)')            % Label the y-axis
title('Measles by month in NYC: 1931-1971');  % Put title on the graph
```

You should see a Figure Window containing a labeled bar chart:

EXERCISE 1: Create a bar chart of chicken pox monthly totals.

EXAMPLE 4: Label the bars explicitly (set(gca, ...))

Create a new cell in which you type and execute:

```   mylabels = {'J', 'F', 'M', 'A', 'M', 'J', 'J', 'A', 'S', 'O', 'N', 'D'};
figure                            % Create a new figure
bar(measlesByMonth./1000)         % Plot bar chart of monthly totals
xlabel('Month')                   % Label the x-axis
ylabel('Cases (in thousands)')    % Label the y-axis
title('Measles by month in NYC: 1931-1971');  % Put title on the graph
set(gca,'XTickLabelMode', 'manual', ...% Manually label tick marks
'XTickLabel', mylabels)
```

You should see a Figure Window containing a labeled bar chart:

EXAMPLE 5: Create a bar chart of the measles yearly case totals (bar(X,Y))

Create a new cell in which you type and execute:

```   figure                                       % Create a new figure
bar(years, sum(measles, 2)./1000)            % Bar chart of measles yearly totals
xlabel('Year')                               % Label the x-axis
ylabel('Cases (in thousands)')               % Label the y-axis
title('Measles by year in NYC: 1931-1971')   % Put a title on the graph
```

You should see a Figure Window containing a labeled bar chart:

EXAMPLE 6: Create a side-by-side bar chart of monthly totals

Create a new cell in which you type and execute:

```   figure                             % Create a new figure
bar(byMonth./1000)                 % Plot a bar chart of monthly totals
xlabel('Month')                    % Label the x-axis
ylabel('Cases (in thousands)')     % Label the y-axis
title('Childhood diseases by month in NYC: 1931-1971')  % Title graph
legend('Measles', 'Mumps', 'Chicken pox')
```

You should see a Figure Window containing a labeled bar chart:

EXERCISE 2: Create a bar chart of byMonth'.
Be sure your labeling of the graph makes sense.

EXAMPLE 7: Create a stacked bar chart of monthly totals

Create a new cell in which you type and execute:

```   figure                                        % Create a new figure
bar(byMonth./1000, 'stack') % Plot stacked bar chart of monthly totals
xlabel('Month')                               % Label the x-axis
ylabel('Cases (in thousands)')                % Label the y-axis
title('Childhood diseases by month in NYC: 1931-1971')  % Title graph
legend('Measles', 'Mumps', 'Chicken pox')     % Need a legend
```

You should see a Figure Window containing a labeled stacked bar chart:

EXERCISE 3: Create a stacked bar chart of traffic by hour for count data of Lesson 1.
Make sure your bar chart is fully labeled.

EXAMPLE 8: Create a horizontal stacked bar chart

Create a new cell in which you type and execute:

```   figure                                        % Create a new figure
barh(byMonth./1000, 'stack') % Plot a stacked bar chart of monthly totals
xlabel('Cases (in thousands)')                % Label the x-axis
ylabel('Month')                               % Label the y-axis
title('Childhood diseases by month in NYC: 1931-1971');  % Put a title on the graph
legend('Measles', 'Mumps', 'Chicken pox')     % Need a legend
```

You should see a Figure Window containing a labeled horizontal stacked bar chart that uses a spring color scheme:

SUMMARY OF SYNTAX

 MATLAB syntax Description bar(Y) Display each element in a vector Y by a bar with height determined by the value. The bars are labeled as 1, 2, ... . If Y is a 2D array, MATLAB groups the bars by row. bar(x, Y) Display a bar chart for Y. The x values give the positions of the bar groups. barh(Y) Display a bar chart for Y with the bars oriented horizontally. (Beware that you will need to reverse the labels for the x and y axes.) bar(Y, 'stack') Display a bar chart in which the bars in a group are stacked atop one another rather than presented side-by-side. This representation is good for reading overall totals and estimating the fraction of the total contributed by individual values. `set(gca, PropertyName, PropertyValue)` Set a property of the current axis. The `PropertyName` argument is a string representing the name of the property. The `PropertyValue` argument gives the value of the property.

This lesson was written by Kay A. Robbins of the University of Texas at San Antonio and last modified by Dawn Roberson on 22-Jan-2018. Please contact kay.robbins@utsa.edu with comments or suggestions. The photo is NYC View, a photo of the Empire State Building taken from a roof at Lexington Ave and 37th, image 1857382 from <http://www.istockphoto.com>.