instagram

Students learn to generate pie charts and bar charts, and explore other plotting and display functions.

Lesson Goals

Students will be able to:

  • Read pie and bar charts

  • Explain the difference between pie and bar charts

  • Generate pie and bar charts (among others) from the Animals Dataset

Student-facing Lesson Goals

  • Let’s use functions to create graphs from data.

Materials

Preparation

  • Make sure all materials have been gathered

  • Decide how students will be grouped in pairs

  • Computer for each student (or pair), with access to the internet

  • Student workbook, and something to write with

  • All students should log into CPO and open the "Animals Starter File" they saved from the prior lesson. If they don’t have the file, they can open a new one

Supplemental Resources

Language Table

Types

Functions

Values

Number

+, -, *, /, num-sqrt

4, -1.2, 2/3, pi

String

string-length, string-repeat, string-contains

"hello", "91"

Boolean

<, <>, <=, >=, <, >, ==, <>, >=

true, false

Image

star, triangle, circle, square, rhombus, ellipse, regular-polygon, radial-star

🔵🔺🔶

Glossary
bar chart

a display of categorical data that uses bars positioned over category values; each bar’s height reflects the count or percentage of data values in that category

contract

a statement of the name, domain, and range of a function

domain

the type or set of inputs that a function expects

pie chart

a display that uses areas of a circular pie’s slices to show percentages in each category

🔗Displaying Categorical Variables 10 minutes

Overview

Students extend their understanding of Contracts and function application, learning new functions that consume Tables and produce displays and plots.

Launch

Have students ever seen any pictures created from tables of data? Can they think of a situation when they’d want to consume a Table, and use that to produce an image?

The library included at the top of the file includes some helper functions that are useful for Data Science, which we will use throughout this course. Here is the Contract for a function that makes pie charts:

# pie-chart :: Table, String -> Image

And here is an example of using the function:

pie-chart(animals-table, "legs")

  • What is the Name of this function?

  • How many inputs are in its Domain?

  • In the Interactions Area, type pie-chart(animals-table, "sex") and hit Enter. What happens?

Hovering over a pie slice reveals the label, as well as the count and the percentage of the whole. In this example we see that there are 15 females, representing 46.9% of the population.

We can also resize the window by dragging its borders. This allows us to experiment with the data before closing the window and generating the final, non-interactive image.

The function pie-chart consumes a Table of data, along with the name of a categorical column you want to display. The computer goes through the column, counting the number of times that each value appears. Then it draws a pie slice for each value, with the size of the slice being the percentage of times it appears. In this example, we used our animals-table table as our dataset, and made a pie chart showing the distribution of sex across the shelter.

Investigate

Here is the Contract for another function, which makes bar charts:

# bar-chart :: Table, String -> Image

  • Which column of the animals table tells us how many legs an animal has?

  • Use bar-chart to make a display showing how many animals there are of each sex.

  • Experiment with pie and bar charts, passing in different column names. If you get an error message, read it carefully!

  • What do you think are the rules for what kinds of columns can be used by bar-chart and pie-chart?

  • When would you want to use one chart instead of another?

Common Misconceptions

Pie charts and bar charts may show counts or percentages (in Pyret, pie charts show percentages and bar charts show counts). Bar charts look a lot like histograms, which are actually quite different because they display quantitative data, not categorical. Also, a pie chart can only display one categorical variable but a bar chart might be used to display two or more categorical variables.

Synthesize

Pie and Bar Charts display what portion of a sample belongs to each category. If they are based on sample data from a larger population, we use them to infer the proportion of a whole population that might belong to each category.

Pie charts and bar charts are mostly used to display categorical columns.

While bars in some bar charts should follow some logical order (alphabetical, small-medium-large, etc), the pie slices and bars can technically be placed in any order, without changing the meaning of the chart.

🔗Exploring other Displays 30 minutes

Overview

Students freely explore the Data Science display library. In doing so, they experiment with new charts, practice reading Contracts and error messages, and develop better intuition for the programming constructs they’ve seen before.

Launch

There are lots of other functions, for all different kinds of charts and plots. Even if you don’t know what these plots are for yet, see if you can use your knowledge of Contracts to figure out how to use them.

Common Misconceptions

There are many possible misconceptions about displays that students may encounter here. But that’s ok! Understanding all those other plots is not a learning goal for this lesson. Rather, the goal is to have them develop some loose familiarity, and to get more practice reading Contracts.

Synthesize

Today you’ve added more functions to your toolbox. Functions like pie-chart and bar-chart can be used to visually display data, and even transform entire tables!

You will have many opportunities to use these concepts in this course, by writing programs to answer data science questions.

Extension Activity

Sometimes we want to summarize a categorical column in a Table, rather than a pie chart. For example, it might be handy to have a table that has a row for dogs, cats, lizards, and rabbits, and then the count of how many of each type there are. Pyret has a function that does exactly this! Try typing this code into the Interactions Area: count(animals-table, "species")

What did we get back? count is a function that consumes a table and the name of a categorical column, and produces a new table with exactly the columns we want: the name of the category and the number of times that category occurs in the dataset. What are the names of the columns in this new table?

  • Use the count function to make a table showing the number of animals that are fixed (or not) from the shelter.

  • Use the count function to make a table showing the number of animals of each sex from the shelter.

Sometimes the dataset we have is already summarized in a table like this, and we want to make a chart from that. In this situation, we want to base our display on the summary table: the size of the pie slice or bar is taken directly from the count column, and the label is taken directly from the value column. When we want to use summarized data to produce a pie chart, we have the contract for another function:

# pie-chart-summarized :: Table, String, String -> Image

And an example of using that function (applying count to the animals-table to force it into the shape pie-chart-summarized needs):

pie-chart-summarized(count(animals-table,"species"), "value", "count")

🔗Additional Exercises:

These materials were developed partly through support of the National Science Foundation, (awards 1042210, 1535276, 1648684, and 1738598). CCbadge Bootstrap:Data Science by the Bootstrap Community is licensed under a Creative Commons 4.0 Unported License. This license does not grant permission to run training or professional development. Offering training or professional development with materials substantially derived from Bootstrap must be approved in writing by a Bootstrap Director. Permissions beyond the scope of this license, such as to run training, may be available by contacting contact@BootstrapWorld.org.