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.
NYC view of Empire State Building

Contents

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

EXAMPLE 1: Load the data about New York contagious diseases (load .mat files)

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:

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>.