Choropleths Data Visualization with ggplot2 Chapter Contents - - PowerPoint PPT Presentation

choropleths
SMART_READER_LITE
LIVE PREVIEW

Choropleths Data Visualization with ggplot2 Chapter Contents - - PowerPoint PPT Presentation

DATA VISUALIZATION WITH GGPLOT2 Choropleths Data Visualization with ggplot2 Chapter Contents Maps GIS = Geographic Information System Choropleths Cartographic maps Animations Data Visualization with ggplot2


slide-1
SLIDE 1

DATA VISUALIZATION WITH GGPLOT2

Choropleths

slide-2
SLIDE 2

Data Visualization with ggplot2

Chapter Contents

  • Maps
  • Animations
  • GIS = Geographic Information System
  • Choropleths
  • Cartographic maps
slide-3
SLIDE 3

Data Visualization with ggplot2

Choropleths

25 30 35 40 45 50 −120 −100 −80

long lat > library(ggplot2) > usa <- map_data("usa") > ggplot(usa, aes(long, lat, group = group)) + geom_polygon() + coord_map()

Bunch of polygons

slide-4
SLIDE 4

Data Visualization with ggplot2

Choropleths

25 30 35 40 45 50 −120 −100 −80

long lat > library(ggplot2) > library(ggalt) > usa <- map_data("usa") > ggplot(usa, aes(long, lat, group = group)) + geom_polygon() + coord_proj("+proj=wintri")

slide-5
SLIDE 5

Data Visualization with ggplot2

Many polygons

> states <- map_data("state") > ggplot(states, aes(long, lat, fill = region, group = group)) + geom_polygon(color = "white") + coord_map()

slide-6
SLIDE 6

Data Visualization with ggplot2

Weed prices

250 300 350

value

slide-7
SLIDE 7

Data Visualization with ggplot2

Weed prices

200 250 300 350 400

value

slide-8
SLIDE 8

Data Visualization with ggplot2

Alternatives

  • north dakota

south dakota vermont maryland virginia iowa louisiana delaware pennsylvania

  • klahoma

new hampshire missouri west virginia minnesota illinois wisconsin tennessee kansas north carolina new jersey massachusetts district of columbia arkansas new york south carolina connecticut nebraska alabama kentucky mississippi wyoming texas

  • hio

indiana georgia rhode island maine hawaii florida arizona alaska michigan utah new mexico idaho nevada montana california colorado washington

  • regon

200 250 300 350 400

value region

slide-9
SLIDE 9

Data Visualization with ggplot2

Alternatives

  • north dakota

south dakota iowa missouri minnesota illinois wisconsin kansas nebraska

  • hio

indiana michigan vermont pennsylvania new hampshire new jersey massachusetts new york connecticut rhode island maine maryland virginia louisiana delaware

  • klahoma

west virginia tennessee north carolina arkansas south carolina alabama kentucky mississippi texas georgia florida wyoming hawaii arizona alaska utah new mexico idaho nevada montana california colorado washington

  • regon

Midwest Northeast South West 200 250 300 350 400

value state

slide-10
SLIDE 10

Data Visualization with ggplot2

Alternatives

  • illinois

wisconsin

  • hio

indiana michigan north dakota south dakota iowa missouri minnesota kansas nebraska pennsylvania new jersey new york vermont new hampshire massachusetts connecticut rhode island maine tennessee alabama kentucky mississippi maryland virginia delaware west virginia north carolina south carolina georgia florida louisiana

  • klahoma

arkansas texas wyoming arizona utah new mexico idaho nevada montana colorado hawaii alaska california washington

  • regon

Midwest Midwest Northeast Northeast South South South West West East North Central West North Central Mid−Atlantic New England East South Central South Atlantic West South Central Mountain Pacific 200 250 300 350 400

value state

slide-11
SLIDE 11

DATA VISUALIZATION WITH GGPLOT2

Let’s practice!

slide-12
SLIDE 12

DATA VISUALIZATION WITH GGPLOT2

Cartographic Maps

slide-13
SLIDE 13

Data Visualization with ggplot2

Cartographic map

  • Drawn
  • Topographical maps
  • Altitude, infrastructure ...
  • Photographic
  • Satellite images
  • Hybrid
  • ggmap
slide-14
SLIDE 14

Data Visualization with ggplot2

> # Default style - zoom = 3 > library(ggmap) > def_03 <- get_map(location = "Berlin, Germany", zoom = 3) > ggmap(def_03, extent = "device")

slide-15
SLIDE 15

Data Visualization with ggplot2

> # Default style - zoom = 13 > library(ggmap) > def_13 <- get_map(location = "Berlin, Germany", zoom = 13) > ggmap(def_13, extent = "device")

slide-16
SLIDE 16

Data Visualization with ggplot2

> # Default style - zoom = 20 > library(ggmap) > def_20 <- get_map(location = "Berlin, Germany", zoom = 20) > ggmap(def_20, extent = "device")

slide-17
SLIDE 17

Data Visualization with ggplot2

> # stamen/watercolor - zoom = 13 > library(ggmap) > wc_13 <- get_map(location = "Berlin, Germany", zoom = 13, source = "stamen", maptype = "watercolor") > ggmap(wc_13, extent = "device")

slide-18
SLIDE 18

Data Visualization with ggplot2

> # stamen/toner - zoom = 13 > library(ggmap) > ton_13 <- get_map(location = "Berlin, Germany", zoom = 13, source = "stamen", maptype = "toner") > ggmap(ton_13, extent = "device")

slide-19
SLIDE 19

Data Visualization with ggplot2

> # stamen/hybrid - zoom = 13 > library(ggmap) > hyb_13 <- get_map(location = "Berlin, Germany", zoom = 13, source = "stamen", maptype = "hybrid") > ggmap(ton_13, extent = "device")

slide-20
SLIDE 20

Data Visualization with ggplot2

> # google/satellite - zoom = 13 > library(ggmap) > sat_13 <- get_map(location = "Berlin, Germany", zoom = 13, source = "google", maptype = "satellite") > ggmap(ton_13, extent = "device")

slide-21
SLIDE 21

Data Visualization with ggplot2

Get coordinates

> berlin_sites <- c("Brandenburger Tor", "Potsdamer Platz", 
 "Victory Column Berlin", "Checkpoint Charlie", "Reichstag Berlin", "Alexander Platz") > xx <- geocode(berlin_sites) Information from URL : http://maps.googleapis.com/maps/... Information from URL : ... > # Add column with cleaned up names > xx$location <- sub(" Berlin", "", berlin_sites) > 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" ...

slide-22
SLIDE 22

Data Visualization with ggplot2

> # google/roadmap - zoom = 13 > road_13 <- get_map(location = "Berlin, Germany", zoom = 13, 
 source = "google", maptype = "roadmap") > ggmap(road_13, extent = "device")

slide-23
SLIDE 23

Data Visualization with ggplot2

  • > # google/roadmap - zoom = 13

> road_13 <- get_map(location = "Berlin, Germany", zoom = 13, 
 source = "google", maptype = "roadmap") > ggmap(road_13, extent = "device") + geom_point(data = xx, col = "red")

slide-24
SLIDE 24

Data Visualization with ggplot2

  • > bbox <- make_bbox(lon = xx$lon, lat = xx$lat, f = .1)

> boxed_14 <- get_map(location = bbox, zoom = 14, source = "google", maptype = "roadmap") > ggmap(boxed_14, extent = "device") + geom_point(data = xx, col = "red")

slide-25
SLIDE 25

Data Visualization with ggplot2

  • location
  • Alexander Platz

Brandenburger Tor Checkpoint Charlie Potsdamer Platz Reichstag Victory Column

> bbox <- make_bbox(lon = xx$lon, lat = xx$lat, f = .1) > boxed_14 <- get_map(location = bbox, zoom = 14, source = "google", maptype = "roadmap") > ggmap(boxed_14, extent = "device") + geom_point(data = xx, aes(col = location), size = 3) + scale_colour_brewer(palette = "Set1")

slide-26
SLIDE 26

Data Visualization with ggplot2

Final Plot

Brandenburger Tor Potsdamer Platz Victory Column Checkpoint Charlie Reichstag Alexander Platz

slide-27
SLIDE 27

DATA VISUALIZATION WITH GGPLOT2

Let’s practice!

slide-28
SLIDE 28

DATA VISUALIZATION WITH GGPLOT2

Animations

slide-29
SLIDE 29

Data Visualization with ggplot2

Animations

  • Dense temporal data
  • Great exploratory tool
  • Several ways
  • for loop to produce gif
  • animation
  • gganimate
slide-30
SLIDE 30

Data Visualization with ggplot2

Motion Chart

  • Hans Rosling
  • Karolinska Institute in Stockholm
  • Founder of Gapminder
  • UN data
  • Life expectancy, GDP ...
slide-31
SLIDE 31

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

slide-32
SLIDE 32

Data Visualization with ggplot2

Static plot

> ggplot(gapminder, aes(x = gdpPercap, y = lifeExp, colour = continent, size = pop)) + geom_point(alpha = 0.6) # details omitted

20 40 60 80 85 1e+03 1e+04 1e+05

GDP (per capita, log10) Life Expectancy Population

2.50e+08 5.00e+08 7.50e+08 1.00e+09 1.25e+09

Continent

Africa Americas Asia Europe Oceania

slide-33
SLIDE 33

Data Visualization with ggplot2

Motion chart (1)

> p <- ggplot(gapminder, aes(x = gdpPercap, y = lifeExp, colour = continent, size = pop, frame = year)) + geom_point(alpha = 0.6) # details omitted > gg_animate(p, "chart1.gif")

slide-34
SLIDE 34

Data Visualization with ggplot2

Motion chart (2)

> p <- ggplot(gapminder, aes(x = gdpPercap, y = lifeExp, colour = continent, size = pop, frame = country)) + geom_point(alpha = 0.6) # details omitted > gg_animate(p, "chart2.gif")

slide-35
SLIDE 35

Data Visualization with ggplot2

Motion chart (3)

> p <- ggplot(gapminder, aes(x = gdpPercap, y = lifeExp, colour = continent, size = pop, frame = country)) + geom_point(alpha = 0.6) # details omitted > gg_animate(p, "chart3.gif", interval = 3.0)

slide-36
SLIDE 36

Data Visualization with ggplot2

Population size

Original visualization by Kyle Walker

slide-37
SLIDE 37

DATA VISUALIZATION WITH GGPLOT2

Let’s practice!