LESSON 3: Pie and 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 pie charts and bar charts.

In this lesson you will:
  • Create and edit a pie chart.
  • Create and edit a bar chart.
  • Use stacked and side-by-side bar chart styles.
  • Employ vertical and horizontal bar orientations.
  • Combine vectors into larger arrays.
NYC view of Empire State Building

Contents


DATA FOR THIS LESSON

File Description
NYCDiseases.mat
  • The data set contains of 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 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 originally appeared 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 LESSON 3


EXAMPLE 1: Load the data about New York contagious diseases

Create a new cell in which you type and execute:

   load NYCDiseases.mat;    % Load the disease data

You should see 4 variables in the Workspace Browser:


EXAMPLE 2: Compute yearly totals of the three diseases

Create a new cell in which you type and execute:

   totalMeasles = sum(measles(:));   % Find the total number of measles cases
   totalMumps = sum(mumps(:));       % Find the total number of mumps cases
   totalCP = sum(chickenPox(:));     % Find the total number of chicken pox cases
   diseaseTotals = [totalMeasles, totalMumps, totalCP]; % Make a totals vector

You should see new 4 variables in the Workspace Browser:

In the space below:  


EXAMPLE 3: Show a pie chart of the yearly totals of the three diseases

Create a new cell in which you type and execute:

   figure                                      % Create a new figure window
   pie(diseaseTotals, {'Measles', 'Mumps', 'Chicken pox'}) % Draw a pie chart
   title('Contagious childhood diseases in New York City 1931-1971');

You should see a Figure Window with a pie chart of the three diseases:

In the space below:  


Create a new cell right here (beginning of a cell starts with %%). Define 3 variables for the total number of cases of measles, mumps, and chicken pox, respectively for the year 1941. Write MATLAB code to create a pie chart of the number of measles, mumps, and chicken pox cases for 1941. Compare the results with the pie chart of EXAMPLE 3.


EXAMPLE 4: Make a bar chart of the disease totals

Create a new cell in which you type and execute:

   figure                 % Create a new figure
   bar(diseaseTotals)     % Plot a bar chart in it
   set(gca, ...           % Label the tick marks rather than x-axis
       'XTickLabelMode', 'manual', ...         % Doing tick marks by hand
       'XTickLabel', {'Measles', 'Mumps', 'Chicken pox'})
   ylabel('Cases');       % Label the y-axis
   title('Contagious childhood diseases in NYC: 1931-1971');

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


Create a new cell right here (beginning of a cell starts with %%). Write MATLAB code to create a bar graph of the number of measles cases for 1941.


EXAMPLE 5: Calculate individual and overall monthly totals

Create a new cell in which you type and execute:

   measlesByMonth = sum(measles);       % Find the monthly totals of measles
   mumpsByMonth = sum(mumps);           % Find the monthly totals of mumps
   CPByMonth = sum(chickenPox);         % Find the monthly totals of chicken pox
   byMonth = [measlesByMonth', mumpsByMonth', CPByMonth']; %Make 3 columns of totals

You should see 4 variables in the Workspace Browser:


EXAMPLE 6: Make a bar chart of the measles monthly case totals (thousands)

Create a new cell in which you type and execute:

   figure                                        % Create a new figure
   bar(measlesByMonth./1000)   % Plot a bar chart of measles 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 a title on the graph

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


Add a cell in which you create a new figure similar to that created by EXAMPLE 6. Plot mumps instead of measles.


EXAMPLE 7: Make a side-by-side bar chart of monthly totals for the 3 diseases

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');  % Put a title on the graph
   legend('Measles', 'Mumps', 'Chicken pox')

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


EXAMPLE 8: Make a stacked bar chart of monthly totals for the 3 diseases

Create a new cell in which you type and execute:

   figure                                        % Create a new figure
   bar(byMonth./1000, 'stack') % Plot a 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');  % Put a title on the graph
   legend('Measles', 'Mumps', 'Chicken pox')     % Need a legend

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


EXAMPLE 9: Make a horizontal bar chart of monthly totals for the 3 diseases

Create a new cell in which you type and execute:

   figure                                        % Create a new figure
   barh(byMonth./1000)         % 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 stack bar chart:


EXAMPLE 10: Make a stacked horizontal bar chart using a different color scheme

Create a new cell in which you type and execute:

   figure                                        % Create a new figure
   colormap summer                               % Use a new color scheme
   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 summer color scheme:


SUMMARY OF SYNTAX

MATLAB syntax Description
[a, b, c] forms an array with values of a, b and c placed side-by-side.
[a; b; c] forms an array with values of a, b and c placed vertically end-to-end.
pie(y) draws a pie chart for y. Each element of y corresponds to a slice of the pie in proportion to the element's fraction of the total. MATLAB draws a partial pie if the sum of the elements of y is less than 1.
bar(y) draws a bar for each element of y against the values 1, 2, ... . If y is a 2D array, MATLAB groups the bars by row.
bar(x, y) draws a bar for each element of y against the values of x. If y is a 2D array, MATLAB groups the bars by row.
barh(y) is the same as bar, except that MATLAB draws horizontal bars rather than vertical bars. (Beware that you will need to reverse the labels for the x and y axes.)
bar(y, 'stack') draws a bar chart in which the height of a bar is the sum of the corresponding row of y.
colormap aMap sets the list of colors to use (the current color map) to those listed in the variable aMap. MATLAB has several built-in lists including summer and jet. Alternatively, you can create your own list of colors.


This lesson was written by Kay A. Robbins of the University of Texas at San Antonio and last modified on 17-Jan-2011. Please contact krobbins@cs.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>.