csss 569 visualizing data and models - lab 7: visualizing...

59
CSSS 569 Visualizing Data and Models Lab 7: Visualizing Spatial Data Kai Ping (Brian) Leung Department of Political Science, UW February 20, 2020

Upload: others

Post on 05-Oct-2020

0 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: CSSS 569 Visualizing Data and Models - Lab 7: Visualizing ...staff.washington.edu/kpleung/vis/slides/lab7_slides.pdfCSSS 569 Visualizing Data and Models Lab7: VisualizingSpatialData

CSSS 569 Visualizing Data and ModelsLab 7: Visualizing Spatial Data

Kai Ping (Brian) Leung

Department of Political Science, UW

February 20, 2020

Page 2: CSSS 569 Visualizing Data and Models - Lab 7: Visualizing ...staff.washington.edu/kpleung/vis/slides/lab7_slides.pdfCSSS 569 Visualizing Data and Models Lab7: VisualizingSpatialData

Introduction

AGO

ARG

ATA

AUS

BOL

BRA

CAN

CHN

COD

COL

DZA

ETH

GRL

IDN

IND

IRN

KAZ

LBYMEX

MLI

MNG

NER

PER

RUS

SAU

SDNTCD

USA

ZAF

Income class1. High income: OECD2. High income: nonOECD3. Upper middle income4. Lower middle income5. Low income

Metro population (2020)

10 mln 20 mln 30 mln 40 mln

Population growth (%)Less than 00 to 1010 to 2020 to 3030 or more

Page 3: CSSS 569 Visualizing Data and Models - Lab 7: Visualizing ...staff.washington.edu/kpleung/vis/slides/lab7_slides.pdfCSSS 569 Visualizing Data and Models Lab7: VisualizingSpatialData

Introduction

Rent in 20080 to 500500 to 1,0001,000 to 1,5001,500 to 2,0002,000 to 2,5002,500 to 3,000

Hispanic population in 2008 (%)0 to 1010 to 2020 to 3030 to 4040 to 5050 to 6060 to 70

% of households receiving public assistance in 2000

0 to 55 to 1010 to 1515 to 2020 to 25

Page 4: CSSS 569 Visualizing Data and Models - Lab 7: Visualizing ...staff.washington.edu/kpleung/vis/slides/lab7_slides.pdfCSSS 569 Visualizing Data and Models Lab7: VisualizingSpatialData

Dealing with and visualizing spatial data in R

I Numerous spatial data formats

I .shp (shapefile; the most common); .geojson, .json; .gml;.csv; .tiff. . .

I Countless packages to work with spatial data

I Recent package sf allows geospatial data to be stored in dataframes

I Well integrated with tidyverse

I Many packages to draw maps

I tmap allows easy visualization of static and interactive mapsI Also employs the “grammar of graphics”

Page 5: CSSS 569 Visualizing Data and Models - Lab 7: Visualizing ...staff.washington.edu/kpleung/vis/slides/lab7_slides.pdfCSSS 569 Visualizing Data and Models Lab7: VisualizingSpatialData

Dealing with and visualizing spatial data in R

I Numerous spatial data formatsI .shp (shapefile; the most common); .geojson, .json; .gml;

.csv; .tiff. . .

I Countless packages to work with spatial data

I Recent package sf allows geospatial data to be stored in dataframes

I Well integrated with tidyverse

I Many packages to draw maps

I tmap allows easy visualization of static and interactive mapsI Also employs the “grammar of graphics”

Page 6: CSSS 569 Visualizing Data and Models - Lab 7: Visualizing ...staff.washington.edu/kpleung/vis/slides/lab7_slides.pdfCSSS 569 Visualizing Data and Models Lab7: VisualizingSpatialData

Dealing with and visualizing spatial data in R

I Numerous spatial data formatsI .shp (shapefile; the most common); .geojson, .json; .gml;

.csv; .tiff. . .I Countless packages to work with spatial data

I Recent package sf allows geospatial data to be stored in dataframes

I Well integrated with tidyverseI Many packages to draw maps

I tmap allows easy visualization of static and interactive mapsI Also employs the “grammar of graphics”

Page 7: CSSS 569 Visualizing Data and Models - Lab 7: Visualizing ...staff.washington.edu/kpleung/vis/slides/lab7_slides.pdfCSSS 569 Visualizing Data and Models Lab7: VisualizingSpatialData

Dealing with and visualizing spatial data in R

I Numerous spatial data formatsI .shp (shapefile; the most common); .geojson, .json; .gml;

.csv; .tiff. . .I Countless packages to work with spatial data

I Recent package sf allows geospatial data to be stored in dataframes

I Well integrated with tidyverseI Many packages to draw maps

I tmap allows easy visualization of static and interactive mapsI Also employs the “grammar of graphics”

Page 8: CSSS 569 Visualizing Data and Models - Lab 7: Visualizing ...staff.washington.edu/kpleung/vis/slides/lab7_slides.pdfCSSS 569 Visualizing Data and Models Lab7: VisualizingSpatialData

Dealing with and visualizing spatial data in R

I Numerous spatial data formatsI .shp (shapefile; the most common); .geojson, .json; .gml;

.csv; .tiff. . .I Countless packages to work with spatial data

I Recent package sf allows geospatial data to be stored in dataframes

I Well integrated with tidyverse

I Many packages to draw maps

I tmap allows easy visualization of static and interactive mapsI Also employs the “grammar of graphics”

Page 9: CSSS 569 Visualizing Data and Models - Lab 7: Visualizing ...staff.washington.edu/kpleung/vis/slides/lab7_slides.pdfCSSS 569 Visualizing Data and Models Lab7: VisualizingSpatialData

Dealing with and visualizing spatial data in R

I Numerous spatial data formatsI .shp (shapefile; the most common); .geojson, .json; .gml;

.csv; .tiff. . .I Countless packages to work with spatial data

I Recent package sf allows geospatial data to be stored in dataframes

I Well integrated with tidyverseI Many packages to draw maps

I tmap allows easy visualization of static and interactive mapsI Also employs the “grammar of graphics”

Page 10: CSSS 569 Visualizing Data and Models - Lab 7: Visualizing ...staff.washington.edu/kpleung/vis/slides/lab7_slides.pdfCSSS 569 Visualizing Data and Models Lab7: VisualizingSpatialData

Dealing with and visualizing spatial data in R

I Numerous spatial data formatsI .shp (shapefile; the most common); .geojson, .json; .gml;

.csv; .tiff. . .I Countless packages to work with spatial data

I Recent package sf allows geospatial data to be stored in dataframes

I Well integrated with tidyverseI Many packages to draw maps

I tmap allows easy visualization of static and interactive maps

I Also employs the “grammar of graphics”

Page 11: CSSS 569 Visualizing Data and Models - Lab 7: Visualizing ...staff.washington.edu/kpleung/vis/slides/lab7_slides.pdfCSSS 569 Visualizing Data and Models Lab7: VisualizingSpatialData

Dealing with and visualizing spatial data in R

I Numerous spatial data formatsI .shp (shapefile; the most common); .geojson, .json; .gml;

.csv; .tiff. . .I Countless packages to work with spatial data

I Recent package sf allows geospatial data to be stored in dataframes

I Well integrated with tidyverseI Many packages to draw maps

I tmap allows easy visualization of static and interactive mapsI Also employs the “grammar of graphics”

Page 12: CSSS 569 Visualizing Data and Models - Lab 7: Visualizing ...staff.washington.edu/kpleung/vis/slides/lab7_slides.pdfCSSS 569 Visualizing Data and Models Lab7: VisualizingSpatialData

Overview of tmap package

ggplot2 tmap

Data ggplot(...) + tm_shape(...) +Layers geom_...(...) + tm_...(...) +Small Multiples facet_grid(...) tm_facets(...)Layout theme(...) tm_layout(...)

Page 13: CSSS 569 Visualizing Data and Models - Lab 7: Visualizing ...staff.washington.edu/kpleung/vis/slides/lab7_slides.pdfCSSS 569 Visualizing Data and Models Lab7: VisualizingSpatialData

Prerequisite

install.packages(c("sf", "tmap"))

# Load packageslibrary(tidyverse)library(sf)library(tmap)

# Load data (from tmap)data(World, metro)

Page 14: CSSS 569 Visualizing Data and Models - Lab 7: Visualizing ...staff.washington.edu/kpleung/vis/slides/lab7_slides.pdfCSSS 569 Visualizing Data and Models Lab7: VisualizingSpatialData

Basics tmap

print(World[1,])

## Simple feature collection with 1 feature and 15 fields## geometry type: MULTIPOLYGON## dimension: XY## bbox: xmin: 5298517 ymin: 3762310 xmax: 6474206 ymax: 4839642## epsg (SRID): NA## proj4string: +proj=eck4 +lon_0=0 +x_0=0 +y_0=0 +datum=WGS84 +units=m +no_defs## iso_a3 name sovereignt continent area## 1 AFG Afghanistan Afghanistan Asia 652860 [km^2]## pop_est pop_est_dens economy## 1 28400000 43.5009 7. Least developed region## income_grp gdp_cap_est life_exp well_being footprint## 1 5. Low income 784.1549 59.668 3.8 0.79## inequality HPI geometry## 1 0.4265574 20.22535 MULTIPOLYGON (((5310471 451...

Page 15: CSSS 569 Visualizing Data and Models - Lab 7: Visualizing ...staff.washington.edu/kpleung/vis/slides/lab7_slides.pdfCSSS 569 Visualizing Data and Models Lab7: VisualizingSpatialData

Basics tmap

tm_shape(World) +tm_polygons() +tm_layout(frame = FALSE)

Page 16: CSSS 569 Visualizing Data and Models - Lab 7: Visualizing ...staff.washington.edu/kpleung/vis/slides/lab7_slides.pdfCSSS 569 Visualizing Data and Models Lab7: VisualizingSpatialData

Basics tmaptm_polygons() is composed of two parts: tm_borders() andtm_fill()

tm_shape(World) +tm_borders() +tm_layout(frame = FALSE)

Page 17: CSSS 569 Visualizing Data and Models - Lab 7: Visualizing ...staff.washington.edu/kpleung/vis/slides/lab7_slides.pdfCSSS 569 Visualizing Data and Models Lab7: VisualizingSpatialData

Basics tmaptm_polygons() is composed of two parts: tm_borders() andtm_fill()

tm_shape(World) +tm_fill() +tm_layout(frame = FALSE)

Page 18: CSSS 569 Visualizing Data and Models - Lab 7: Visualizing ...staff.washington.edu/kpleung/vis/slides/lab7_slides.pdfCSSS 569 Visualizing Data and Models Lab7: VisualizingSpatialData

Basics tmaptm_polygons() is composed of two parts: tm_borders() andtm_fill()

tm_shape(World) +tm_borders() +tm_fill() +tm_layout(frame = FALSE)

Page 19: CSSS 569 Visualizing Data and Models - Lab 7: Visualizing ...staff.washington.edu/kpleung/vis/slides/lab7_slides.pdfCSSS 569 Visualizing Data and Models Lab7: VisualizingSpatialData

Basics tmaptm_polygons() is composed of two parts: tm_borders() andtm_fill()

tm_shape(World) +tm_borders(lty = 2) +tm_fill() +tm_layout(frame = FALSE)

Page 20: CSSS 569 Visualizing Data and Models - Lab 7: Visualizing ...staff.washington.edu/kpleung/vis/slides/lab7_slides.pdfCSSS 569 Visualizing Data and Models Lab7: VisualizingSpatialData

Basics tmap

tm_shape(World) +tm_polygons() +tm_layout(frame = FALSE)

Page 21: CSSS 569 Visualizing Data and Models - Lab 7: Visualizing ...staff.washington.edu/kpleung/vis/slides/lab7_slides.pdfCSSS 569 Visualizing Data and Models Lab7: VisualizingSpatialData

Basics tmapAll palettes from RColorBrewer are supported

tm_shape(World) +tm_polygons(col = "income_grp", palette = "-Blues") +tm_layout(frame = FALSE)

income_grp1. High income: OECD2. High income: nonOECD3. Upper middle income4. Lower middle income5. Low income

Page 22: CSSS 569 Visualizing Data and Models - Lab 7: Visualizing ...staff.washington.edu/kpleung/vis/slides/lab7_slides.pdfCSSS 569 Visualizing Data and Models Lab7: VisualizingSpatialData

Basics tmapUse white border to give it a “modern” look

tm_shape(World) +tm_polygons(col = "income_grp", palette = "-Blues",

border.col = "white", border.alpha = 0.5) +tm_layout(frame = FALSE)

income_grp1. High income: OECD2. High income: nonOECD3. Upper middle income4. Lower middle income5. Low income

Page 23: CSSS 569 Visualizing Data and Models - Lab 7: Visualizing ...staff.washington.edu/kpleung/vis/slides/lab7_slides.pdfCSSS 569 Visualizing Data and Models Lab7: VisualizingSpatialData

Basics tmapLegend title:

tm_shape(World) +tm_polygons(col = "income_grp", palette = "-Blues",

border.col = "white", border.alpha = 0.5,title = "Income class") +

tm_layout(frame = FALSE)

Income class1. High income: OECD2. High income: nonOECD3. Upper middle income4. Lower middle income5. Low income

Page 24: CSSS 569 Visualizing Data and Models - Lab 7: Visualizing ...staff.washington.edu/kpleung/vis/slides/lab7_slides.pdfCSSS 569 Visualizing Data and Models Lab7: VisualizingSpatialData

Basics tmapAdd country labels as an additional layer:tm_shape(World) +

tm_polygons(col = "income_grp", palette = "-Blues",border.col = "white", border.alpha = 0.5,title = "Income class") +

tm_text(text = "iso_a3", size = "AREA", col = "grey25") +tm_layout(frame = FALSE)

AGO

ARG

ATA

AUS

BOL

BRA

CAN

CHN

COD

COL

DZA

ETH

GRL

IDN

IND

IRN

KAZ

LBYMEX

MLI

MNG

NER

PER

RUS

SAU

SDNTCD

USA

ZAF

Income class1. High income: OECD2. High income: nonOECD3. Upper middle income4. Lower middle income5. Low income

Page 25: CSSS 569 Visualizing Data and Models - Lab 7: Visualizing ...staff.washington.edu/kpleung/vis/slides/lab7_slides.pdfCSSS 569 Visualizing Data and Models Lab7: VisualizingSpatialData

Basics tmap

worldMap <-tm_shape(World) +tm_polygons(col = "income_grp", palette = "-Blues",

border.col = "white", border.alpha = 0.5,title = "Income class") +

tm_text(text = "iso_a3", size = "AREA", col = "grey25") +tm_layout(frame = FALSE)

Page 26: CSSS 569 Visualizing Data and Models - Lab 7: Visualizing ...staff.washington.edu/kpleung/vis/slides/lab7_slides.pdfCSSS 569 Visualizing Data and Models Lab7: VisualizingSpatialData

Basics tmap

Let’s add another dimension of information: cities’ population

metro <- metro %>%mutate(growth = (pop2020 - pop2010) / pop2010 * 100)

print(metro[1, ])

## Simple feature collection with 1 feature and 13 fields## geometry type: POINT## dimension: XY## bbox: xmin: 69.17246 ymin: 34.52889 xmax: 69.17246 ymax: 34.52889## epsg (SRID): 4326## proj4string: +proj=longlat +datum=WGS84 +no_defs## name name_long iso_a3 pop1950 pop1960 pop1970 pop1980## 1 Kabul Kabul AFG 170784 285352 471891 977824## pop1990 pop2000 pop2010 pop2020 pop2030## 1 1549320 2401109 3722320 5721697 8279607## geometry growth## 1 POINT (69.17246 34.52889) 53.71319

Page 27: CSSS 569 Visualizing Data and Models - Lab 7: Visualizing ...staff.washington.edu/kpleung/vis/slides/lab7_slides.pdfCSSS 569 Visualizing Data and Models Lab7: VisualizingSpatialData

Basics tmap

worldMap +tm_shape(metro) +tm_bubbles(size = "pop2020")

AGO

ARG

ATA

AUS

BOL

BRA

CAN

CHN

COD

COL

DZA

ETH

GRL

IDN

IND

IRN

KAZ

LBYMEX

MLI

MNG

NER

PER

RUS

SAU

SDNTCD

USA

ZAF

Income class1. High income: OECD2. High income: nonOECD3. Upper middle income4. Lower middle income5. Low income

pop2020

10 mln 20 mln 30 mln 40 mln

Page 28: CSSS 569 Visualizing Data and Models - Lab 7: Visualizing ...staff.washington.edu/kpleung/vis/slides/lab7_slides.pdfCSSS 569 Visualizing Data and Models Lab7: VisualizingSpatialData

Basics tmap

worldMap +tm_shape(metro) +tm_bubbles(size = "pop2020", col = "growth")

AGO

ARG

ATA

AUS

BOL

BRA

CAN

CHN

COD

COL

DZA

ETH

GRL

IDN

IND

IRN

KAZ

LBYMEX

MLI

MNG

NER

PER

RUS

SAU

SDNTCD

USA

ZAF

Income class1. High income: OECD2. High income: nonOECD3. Upper middle income4. Lower middle income5. Low income

pop2020

10 mln 20 mln 30 mln 40 mln

growth−50 to 00 to 5050 to 100100 to 150

Page 29: CSSS 569 Visualizing Data and Models - Lab 7: Visualizing ...staff.washington.edu/kpleung/vis/slides/lab7_slides.pdfCSSS 569 Visualizing Data and Models Lab7: VisualizingSpatialData

Basics tmap

worldMap +tm_shape(metro) +tm_bubbles(size = "pop2020", col = "growth",

palette = "-RdYlGn", midpoint = NA)

AGO

ARG

ATA

AUS

BOL

BRA

CAN

CHN

COD

COL

DZA

ETH

GRL

IDN

IND

IRN

KAZ

LBYMEX

MLI

MNG

NER

PER

RUS

SAU

SDNTCD

USA

ZAF

Income class1. High income: OECD2. High income: nonOECD3. Upper middle income4. Lower middle income5. Low income

pop2020

10 mln 20 mln 30 mln 40 mln

growth−50 to 00 to 5050 to 100100 to 150

Page 30: CSSS 569 Visualizing Data and Models - Lab 7: Visualizing ...staff.washington.edu/kpleung/vis/slides/lab7_slides.pdfCSSS 569 Visualizing Data and Models Lab7: VisualizingSpatialData

Basics tmap

worldMap +tm_shape(metro) +tm_bubbles(size = "pop2020", col = "growth",

palette = "-RdYlGn", midpoint = NA,breaks = c(-Inf, 0, 10, 20, 30, Inf))

AGO

ARG

ATA

AUS

BOL

BRA

CAN

CHN

COD

COL

DZA

ETH

GRL

IDN

IND

IRN

KAZ

LBYMEX

MLI

MNG

NER

PER

RUS

SAU

SDNTCD

USA

ZAF

Income class1. High income: OECD2. High income: nonOECD3. Upper middle income4. Lower middle income5. Low income

pop2020

10 mln 20 mln 30 mln 40 mln

growthLess than 00 to 1010 to 2020 to 3030 or more

Page 31: CSSS 569 Visualizing Data and Models - Lab 7: Visualizing ...staff.washington.edu/kpleung/vis/slides/lab7_slides.pdfCSSS 569 Visualizing Data and Models Lab7: VisualizingSpatialData

Basics tmap

worldMap +tm_shape(metro) +tm_bubbles(size = "pop2020", col = "growth",

palette = "-RdYlGn", midpoint = NA,breaks = c(-Inf, 0, 10, 20, 30, Inf),alpha = 0.9,border.col = "white",border.lwd = 0.1,title.size = "Metro population (2020)",title.col = "Population growth (%)")

Page 32: CSSS 569 Visualizing Data and Models - Lab 7: Visualizing ...staff.washington.edu/kpleung/vis/slides/lab7_slides.pdfCSSS 569 Visualizing Data and Models Lab7: VisualizingSpatialData

Basics tmap

AGO

ARG

ATA

AUS

BOL

BRA

CAN

CHN

COD

COL

DZA

ETH

GRL

IDN

IND

IRN

KAZ

LBYMEX

MLI

MNG

NER

PER

RUS

SAU

SDNTCD

USA

ZAF

Income class1. High income: OECD2. High income: nonOECD3. Upper middle income4. Lower middle income5. Low income

Metro population (2020)

10 mln 20 mln 30 mln 40 mln

Population growth (%)Less than 00 to 1010 to 2020 to 3030 or more

Page 33: CSSS 569 Visualizing Data and Models - Lab 7: Visualizing ...staff.washington.edu/kpleung/vis/slides/lab7_slides.pdfCSSS 569 Visualizing Data and Models Lab7: VisualizingSpatialData

Basics tmap

Save the resulting map using tmap_save()

tmap_save(world_map, filename = "worldMap.pdf")

Page 34: CSSS 569 Visualizing Data and Models - Lab 7: Visualizing ...staff.washington.edu/kpleung/vis/slides/lab7_slides.pdfCSSS 569 Visualizing Data and Models Lab7: VisualizingSpatialData

Useful functions from tmap: New York example

Rent in 20080 to 500500 to 1,0001,000 to 1,5001,500 to 2,0002,000 to 2,5002,500 to 3,000

Hispanic population in 2008 (%)0 to 1010 to 2020 to 3030 to 4040 to 5050 to 6060 to 70

% of households receiving public assistance in 2000

0 to 55 to 1010 to 1515 to 2020 to 25

Page 35: CSSS 569 Visualizing Data and Models - Lab 7: Visualizing ...staff.washington.edu/kpleung/vis/slides/lab7_slides.pdfCSSS 569 Visualizing Data and Models Lab7: VisualizingSpatialData

Useful functions from tmap: New York example

I Prerequisite

I Download the .zip data file hereI Unzip it and put it in your working directory

Page 36: CSSS 569 Visualizing Data and Models - Lab 7: Visualizing ...staff.washington.edu/kpleung/vis/slides/lab7_slides.pdfCSSS 569 Visualizing Data and Models Lab7: VisualizingSpatialData

Useful functions from tmap: New York example

I PrerequisiteI Download the .zip data file here

I Unzip it and put it in your working directory

Page 37: CSSS 569 Visualizing Data and Models - Lab 7: Visualizing ...staff.washington.edu/kpleung/vis/slides/lab7_slides.pdfCSSS 569 Visualizing Data and Models Lab7: VisualizingSpatialData

Useful functions from tmap: New York example

I PrerequisiteI Download the .zip data file hereI Unzip it and put it in your working directory

Page 38: CSSS 569 Visualizing Data and Models - Lab 7: Visualizing ...staff.washington.edu/kpleung/vis/slides/lab7_slides.pdfCSSS 569 Visualizing Data and Models Lab7: VisualizingSpatialData

Useful functions from tmap: New York example

I Load .shp file with sf

nyc.bound <- st_read("nyc/nyc.shp")

## Reading layer `nyc' from data source `/Users/LKP/Desktop/CSSS 569 Data Vis/Teaching Materials/slides/lab7/nyc/nyc.shp' using driver `ESRI Shapefile'## Simple feature collection with 55 features and 34 fields## geometry type: MULTIPOLYGON## dimension: XY## bbox: xmin: 913037.2 ymin: 120117 xmax: 1067549 ymax: 272751.4## epsg (SRID): 2263## proj4string: +proj=lcc +lat_1=41.03333333333333 +lat_2=40.66666666666666 +lat_0=40.16666666666666 +lon_0=-74 +x_0=300000.0000000001 +y_0=0 +ellps=GRS80 +towgs84=0,0,0,0,0,0,0 +units=us-ft +no_defs

Page 39: CSSS 569 Visualizing Data and Models - Lab 7: Visualizing ...staff.washington.edu/kpleung/vis/slides/lab7_slides.pdfCSSS 569 Visualizing Data and Models Lab7: VisualizingSpatialData

New York example: overview

Variable Description

rent2008 median monthly contract rent in 2008forhis08 % of hispanic population in 2008pubast00 % of households receiving public assistance in 2000

Page 40: CSSS 569 Visualizing Data and Models - Lab 7: Visualizing ...staff.washington.edu/kpleung/vis/slides/lab7_slides.pdfCSSS 569 Visualizing Data and Models Lab7: VisualizingSpatialData

New York example: exercise 1I Replicate the following map (or choose any palette you see fit)

Rent in 20080 to 500500 to 1,0001,000 to 1,5001,500 to 2,0002,000 to 2,5002,500 to 3,000

Page 41: CSSS 569 Visualizing Data and Models - Lab 7: Visualizing ...staff.washington.edu/kpleung/vis/slides/lab7_slides.pdfCSSS 569 Visualizing Data and Models Lab7: VisualizingSpatialData

New York example: exercise 1tm_shape(nyc.bound) +

tm_polygons(col = "rent2008")

rent20080 to 500500 to 1,0001,000 to 1,5001,500 to 2,0002,000 to 2,5002,500 to 3,000

Page 42: CSSS 569 Visualizing Data and Models - Lab 7: Visualizing ...staff.washington.edu/kpleung/vis/slides/lab7_slides.pdfCSSS 569 Visualizing Data and Models Lab7: VisualizingSpatialData

New York example: exercise 1tm_shape(nyc.bound) +

tm_polygons(col = "rent2008", palette = "BrBG")

rent20080 to 500500 to 1,0001,000 to 1,5001,500 to 2,0002,000 to 2,5002,500 to 3,000

Page 43: CSSS 569 Visualizing Data and Models - Lab 7: Visualizing ...staff.washington.edu/kpleung/vis/slides/lab7_slides.pdfCSSS 569 Visualizing Data and Models Lab7: VisualizingSpatialData

New York example: exercise 1tm_shape(nyc.bound) +

tm_polygons(col = "rent2008", palette = "BrBG",border.col = "white", title = "Rent in 2008") +

tm_layout(frame = FALSE)

Rent in 20080 to 500500 to 1,0001,000 to 1,5001,500 to 2,0002,000 to 2,5002,500 to 3,000

Page 44: CSSS 569 Visualizing Data and Models - Lab 7: Visualizing ...staff.washington.edu/kpleung/vis/slides/lab7_slides.pdfCSSS 569 Visualizing Data and Models Lab7: VisualizingSpatialData

New York example: interactive mode

I Interactive map visualization

tmap_mode("view")

## tmap mode set to interactive viewing

Page 45: CSSS 569 Visualizing Data and Models - Lab 7: Visualizing ...staff.washington.edu/kpleung/vis/slides/lab7_slides.pdfCSSS 569 Visualizing Data and Models Lab7: VisualizingSpatialData

New York example: interactive mode

I Interactive map visualization

tm_shape(nyc.bound) +tm_polygons(col = "rent2008", palette = "BrBG",

title = "Rent in 2008",alpha = 0.7) +

tm_basemap(server = "OpenStreetMap", alpha = 0.5)

Page 46: CSSS 569 Visualizing Data and Models - Lab 7: Visualizing ...staff.washington.edu/kpleung/vis/slides/lab7_slides.pdfCSSS 569 Visualizing Data and Models Lab7: VisualizingSpatialData

New York example: interactive modeI Interactive map visualization

Page 47: CSSS 569 Visualizing Data and Models - Lab 7: Visualizing ...staff.washington.edu/kpleung/vis/slides/lab7_slides.pdfCSSS 569 Visualizing Data and Models Lab7: VisualizingSpatialData

New York example: interactive mode

I Switching back to plotting mode

tmap_mode("plot")

## tmap mode set to plotting

Page 48: CSSS 569 Visualizing Data and Models - Lab 7: Visualizing ...staff.washington.edu/kpleung/vis/slides/lab7_slides.pdfCSSS 569 Visualizing Data and Models Lab7: VisualizingSpatialData

New York example: exercise 2

I Create two more maps based on forhis08 and pubast00:

Rent in 20080 to 500500 to 1,0001,000 to 1,5001,500 to 2,0002,000 to 2,5002,500 to 3,000

Hispanic population in 2008 (%)0 to 1010 to 2020 to 3030 to 4040 to 5050 to 6060 to 70

% of households receiving public assistance in 2000

0 to 55 to 1010 to 1515 to 2020 to 25

Page 49: CSSS 569 Visualizing Data and Models - Lab 7: Visualizing ...staff.washington.edu/kpleung/vis/slides/lab7_slides.pdfCSSS 569 Visualizing Data and Models Lab7: VisualizingSpatialData

New York example: exercise 2rentNYC <- tm_shape(nyc.bound) +

tm_polygons(col = "rent2008", palette = "BrBG",border.col = "white", border.alpha = 0.5,title = "Rent in 2008") +

tm_layout(legend.text.size = 0.5,legend.width = 0.7,frame = FALSE)

hisNYC <- tm_shape(nyc.bound) +tm_polygons(col = "forhis08",

border.col = "white", border.alpha = 0.5,title = "Hispanic population in 2008 (%)") +

tm_layout(legend.text.size = 0.5,legend.width = 0.7,frame = FALSE)

pubastNYC <- tm_shape(nyc.bound) +tm_polygons(col = "pubast00", palette = "Blues",

border.col = "white", border.alpha = 0.5,title = "% of households receiving \npublic assistance in 2000") +

tm_layout(legend.text.size = 0.5,legend.width = 0.7,frame = FALSE)

Page 50: CSSS 569 Visualizing Data and Models - Lab 7: Visualizing ...staff.washington.edu/kpleung/vis/slides/lab7_slides.pdfCSSS 569 Visualizing Data and Models Lab7: VisualizingSpatialData

New York example: exercise 2

tmap_arrange(rentNYC, hisNYC, pubastNYC, nrow = 1)

Rent in 20080 to 500500 to 1,0001,000 to 1,5001,500 to 2,0002,000 to 2,5002,500 to 3,000

Hispanic population in 2008 (%)0 to 1010 to 2020 to 3030 to 4040 to 5050 to 6060 to 70

% of households receiving public assistance in 2000

0 to 55 to 1010 to 1515 to 2020 to 25

Page 51: CSSS 569 Visualizing Data and Models - Lab 7: Visualizing ...staff.washington.edu/kpleung/vis/slides/lab7_slides.pdfCSSS 569 Visualizing Data and Models Lab7: VisualizingSpatialData

New York example: small multiples

I First, create some cutpoints based on forhis08

nyc.bound$cut.forhis <- cut(nyc.bound$forhis08,breaks = 3)

print(nyc.bound$cut.forhis[1:10])

## [1] (9.63,29.6] (9.63,29.6] (9.63,29.6] (49.5,69.4]## [5] (49.5,69.4] (49.5,69.4] (49.5,69.4] (29.6,49.5]## [9] (29.6,49.5] (49.5,69.4]## Levels: (9.63,29.6] (29.6,49.5] (49.5,69.4]

Page 52: CSSS 569 Visualizing Data and Models - Lab 7: Visualizing ...staff.washington.edu/kpleung/vis/slides/lab7_slides.pdfCSSS 569 Visualizing Data and Models Lab7: VisualizingSpatialData

New York example: small multiples

I Small multiples using tm_facets()

tm_shape(nyc.bound) +tm_polygons(col = "rent2008", palette = "BrBG",

title = "Rent in 2008") +tm_facets(by = "cut.forhis", nrow = 1,

free.coords = FALSE,drop.units = FALSE)

(9.63,29.6] (29.6,49.5] (49.5,69.4]Rent in 2008

0 to 500500 to 1,0001,000 to 1,5001,500 to 2,0002,000 to 2,5002,500 to 3,000