Professional Documents
Culture Documents
Data Visualization With Ggplot2: Choropleths
Data Visualization With Ggplot2: Choropleths
Choropleths
Data Visualization with ggplot2
Chapter Contents
● Maps
● GIS = Geographic Information System
● Choropleths
● Cartographic maps
● Animations
Data Visualization with ggplot2
45
40
lat
35
30
25
−120 −100 −80
long
Data Visualization with ggplot2
Choropleths >
>
>
library(ggplot2)
library(ggalt)
usa <- map_data("usa")
> ggplot(usa, aes(long, lat, group = group)) +
geom_polygon() +
50 coord_proj("+proj=wintri")
45
40
lat
35
30
25
Weed prices
value
350
300
250
Data Visualization with ggplot2
Weed prices
value
400
350
300
250
200
Data Visualization with ggplot2
Alternatives
oregon ●
washington ●
colorado ●
california ●
montana ●
nevada ●
idaho ●
new mexico ●
utah ●
michigan ●
alaska ●
arizona ●
florida ●
hawaii ●
maine ●
rhode island ●
georgia ●
indiana ●
ohio ●
texas ●
wyoming ●
mississippi ●
kentucky ●
alabama ●
region
nebraska ●
connecticut ●
south carolina ●
new york ●
arkansas ●
district of columbia ●
massachusetts ●
new jersey ●
north carolina ●
kansas ●
tennessee ●
wisconsin ●
illinois ●
minnesota ●
west virginia ●
missouri ●
new hampshire ●
oklahoma ●
pennsylvania ●
delaware ●
louisiana ●
iowa ●
virginia ●
maryland ●
vermont ●
south dakota ●
north dakota ●
Alternatives
michigan ●
indiana ●
ohio ●
nebraska ●
Midwest
kansas ●
wisconsin ●
illinois ●
minnesota ●
missouri ●
iowa ●
south dakota ●
north dakota ●
maine ●
rhode island ●
connecticut ●
Northeast
new york ●
massachusetts ●
new jersey ●
new hampshire ●
pennsylvania ●
vermont
state
florida ●
georgia ●
texas ●
mississippi ●
kentucky ●
alabama ●
south carolina
South
●
arkansas ●
north carolina ●
tennessee ●
west virginia ●
oklahoma ●
delaware ●
louisiana ●
virginia ●
maryland ●
oregon ●
washington ●
colorado ●
california ●
montana ●
nevada
West
●
idaho ●
new mexico ●
utah ●
alaska ●
arizona ●
hawaii ●
wyoming ●
Alternatives
michigan ●
indiana ●
illinois ●
nebraska ●
kansas ●
minnesota ●
missouri ● Midwest West North Central
iowa ●
south dakota ●
north dakota ●
new york ●
maine ●
rhode island ●
connecticut ●
Northeast New England
massachusetts ●
new hampshire ●
vermont ●
mississippi ●
state
kentucky ●
South East South Central
alabama ●
tennessee ●
florida ●
georgia ●
south carolina ●
north carolina ●
South South Atlantic
west virginia ●
delaware ●
virginia ●
maryland ●
texas ●
arkansas ●
South West South Central
oklahoma ●
louisiana ●
colorado ●
montana ●
nevada ●
idaho ●
West Mountain
new mexico ●
utah ●
arizona ●
wyoming ●
oregon ●
washington ●
hawaii ●
Let’s practice!
DATA VISUALIZATION WITH GGPLOT2
Cartographic Maps
Data Visualization with ggplot2
Cartographic map
● Drawn
● Topographical maps
● Altitude, infrastructure ...
● Photographic
● Satellite images
● Hybrid
● ggmap
Data Visualization with ggplot2
Get coordinates
> berlin_sites <- c("Brandenburger Tor", "Potsdamer Platz",
"Victory Column Berlin", "Checkpoint Charlie",
"Reichstag Berlin", "Alexander Platz")
> str(xx)
'data.frame': 6 obs. of 3 variables:
$ lon : num 13.4 13.4 13.4 13.4 13.4 ...
$ lat : num 52.5 52.5 52.5 52.5 52.5 ...
$ location: chr "Brandenburger Tor" "Potsdamer Platz" ...
Data Visualization with ggplot2
●
Data Visualization with ggplot2
●
Data Visualization with ggplot2
location
● Alexander Platz
●
● Brandenburger Tor
●
● Checkpoint Charlie
● Potsdamer Platz
● Reichstag
● ● Victory Column
●
Data Visualization with ggplot2
Final Plot
Alexander
Platz
Reichstag
Brandenburger
Tor
Victory
Column
Potsdamer
Platz
Checkpoint
Charlie
DATA VISUALIZATION WITH GGPLOT2
Let’s practice!
DATA VISUALIZATION WITH GGPLOT2
Animations
Data Visualization with ggplot2
Animations
● Dense temporal data
● Great exploratory tool
● Several ways
● for loop to produce gif
● animation
● gganimate
Data Visualization with ggplot2
Motion Chart
● Hans Rosling
● Karolinska Institute in Stockholm
● Founder of Gapminder
● UN data
● Life expectancy, GDP ...
Data Visualization with ggplot2
Gapminder data
> # import tab-delimited data
> gapminder <- read.delim("gapminder.tsv",
stringsAsFactors = FALSE)
> str(gapminder)
'data.frame': 1704 obs. of 6 variables:
$ country : chr "Afghanistan" "Afghanistan" "Afghanistan" ...
$ year : int 1952 1957 1962 1967 1972 1977 1982 1987 ...
$ pop : num 8425333 9240934 10267083 11537966 13079460 ...
$ continent: chr "Asia" "Asia" "Asia" "Asia" ...
$ lifeExp : num 28.8 30.3 32 34 36.1 ...
$ gdpPercap: num 779 821 853 836 740 ...
Data Visualization with ggplot2
1.00e+09
60
1.25e+09
Continent
Africa
40 Americas
Asia
Europe
Oceania
20
Population size
Let’s practice!