Maps
1
Data 6 Summer 2022
LECTURE 20
Adding another tool to our visualization toolkit.
Developed by students and faculty at UC Berkeley and Tuskegee University
Week 4
Announcements!
2
Today’s Roadmap
Lecture 20, Data 6 Summer 2022
3
Motivation
4
1. Motivation
2. Scatter Plot Maps
3. Choropleth Maps
➤
5
@TerribleMaps
6
Today’s Data
Over 10,000 fast food restaurants from across the US, with over 500 unique fast food chains.
7
from Kaggle
Quick Review
8
1. Motivation
2. Scatter Plot Maps
3. Choropleth Maps
➤
Scatter Plots
9
The method
t.scatter(column_for_x, column_for_y)
creates a scatter plot using the specified columns. Both columns must contain numerical values.
Optional arguments, in addition to color column_for_x, column_for_y:
.group()
The term “group” in data science is most commonly associated with data aggregation and disaggregation.
Aggregation: A process in which information is gathered and expressed in collective or summary form.
Disaggregation (aka disentanglement): A process of taking aggregated data and breaking it down into smaller information units.
10
The method t.group(column) counts the number of rows for each unique value in column, and returns a two-column table with the results.
Scatter Plot Maps
11
1. Motivation
2. Scatter Plot Maps
3. Choropleth Maps
➤
Latitude and Longitude
Any point on Earth can be described by its latitude and longitude.
12
What’s Wrong with a Scatter Plot?
13
Scatter Plot Maps
When we want to visualize the geographic locations of a lot of data points, it's often helpful to start with a scatter plot map.
14
Scatters Plot + Map = ❤️
Use px.scatter_geo(df, lat, lon)
data frame, latitude, longitude
A Side Note
Plotly
For our maps we will use a Python library called Plotly (px in your notebooks). Plotly is a commonly used visualization library and is really useful for creating maps.
We do not expect you to memorize/remember any Plotly syntax. This is purely for fun.
15
Optional Arguments
We can customize our scatter plot maps by specifying the following arguments:
16
Argument | Example | Behavior |
color | color = ‘name’ | The colors of points on the map are assigned based on their category in the name column of the data frame |
locationmode | locationmode = ‘USA-states’ | Specifies which map to display. We usually set this to ‘USA-states’ to show a state map of the US |
scope | scope = ‘usa’ | Specifies the scope of the map (i.e. what is visible). Setting scope to ‘usa’ zooms the map in just to the US |
title | title = ‘My Map’ | Sets the title of the map |
Questions?
17
Example: All Restaurants
18
Just because we can plot all restaurants in our dataset doesn’t mean we should.
Example: Regional Chains
19
Choropleth Maps
20
1. Motivation
2. Scatter Plot Maps
3. Choropleth Maps
➤
Choropleth Maps
Choropleth maps are useful for visualizing numerical variables across different states or countries. In this sense they are analogous to bar charts, since they encode one categorical variable (state or country) and one numerical variable.
21
Aggregation!
Use px.choropleth(df, locations)
data frame, state abbreviations
Example: Election Mapping
22
Example: Census Data
23
Percent Black or African American
Percent Hispanic or Latino
Example: Redlining
24
LA “Residential Security Map” from The Color of Law (Rothstein)
Plotly Choropleth Maps
We can customize our choropleth maps by specifying the following arguments:
Argument | Example | Behavior |
color | color = ‘name’ | The colors of points on the map are assigned based on their category in the name column of the data frame |
color_discrete_sequence | color_discrete_sequence = px.colors.qualitative.Bold | Specifies the color palette to use for coloring the categories. |
locationmode | locationmode = ‘USA-states’ | Specifies which map to display. We usually set this to ‘USA-states’ to show a state map of the US |
scope | scope = ‘usa’ | Specifies the scope of the map (i.e. what is visible). Setting scope to ‘usa’ zooms the map in just to the US |
title | title = ‘My Map’ | Sets the title of the map |
Colors
There are a lot of options to choose from for color palettes.
px.colors.qualitative.D3
px.colors.qualitative.Set2
…
26
What are some considerations we should keep in mind when choosing a color palette?
Example: Favorite Chains
27
Example: Pizza Chains
28
Example: McDonald’s vs. Burger King
29
Example: Local Burger Chains
30
Questions?
31
In Conclusion…
32
Summary
33
Map wisely!
Recap
Next Time
34
Week 4
Announcements!
35