DATA VISUALIZATION WITH GGPLOT2
Choropleths Data Visualization with ggplot2 Chapter Contents - - PowerPoint PPT Presentation
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
Data Visualization with ggplot2
Chapter Contents
- Maps
- Animations
- GIS = Geographic Information System
- Choropleths
- Cartographic maps
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
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")
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()
Data Visualization with ggplot2
Weed prices
250 300 350
value
Data Visualization with ggplot2
Weed prices
200 250 300 350 400
value
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
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
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
DATA VISUALIZATION WITH GGPLOT2
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
> # Default style - zoom = 3 > library(ggmap) > def_03 <- get_map(location = "Berlin, Germany", zoom = 3) > ggmap(def_03, extent = "device")
Data Visualization with ggplot2
> # Default style - zoom = 13 > library(ggmap) > def_13 <- get_map(location = "Berlin, Germany", zoom = 13) > ggmap(def_13, extent = "device")
Data Visualization with ggplot2
> # Default style - zoom = 20 > library(ggmap) > def_20 <- get_map(location = "Berlin, Germany", zoom = 20) > ggmap(def_20, extent = "device")
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")
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")
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")
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")
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" ...
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")
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")
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")
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")
Data Visualization with ggplot2
Final Plot
Brandenburger Tor Potsdamer Platz Victory Column Checkpoint Charlie Reichstag Alexander Platz
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
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
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")
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")
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)
Data Visualization with ggplot2
Population size
Original visualization by Kyle Walker
DATA VISUALIZATION WITH GGPLOT2