class: center, middle, inverse, title-slide .title[ # Base Plotting in
R
: More Tricks ] .subtitle[ ##
EFB 654: R and Reproducible Research
] .author[ ### Elie Gurarie ] .date[ ###
February 18, 2026
] --- class: inverse .pull-left-40[ ## Goal: plotting many dimensions at once Many layers of information! - colors - sizes - labels - legends - scales ] .pull-right-60[ <img src="PlottingPartII_files/figure-html/final-plot-1.png" alt="" style="display: block; margin: auto;" /> ] --- ## The data .small[ ``` r wdi <- read.csv("WDI.csv") str(wdi) ``` ``` ## 'data.frame': 208 obs. of 16 variables: ## $ country : chr "India" "China" "United States" "Indonesia" ... ## $ iso2c : chr "IN" "CN" "US" "ID" ... ## $ iso3c : chr "IND" "CHN" "USA" "IDN" ... ## $ year : int 2022 2022 2022 2022 2022 2022 2022 2022 2022 2022 ... ## $ status : logi NA NA NA NA NA NA ... ## $ lastupdated: chr "2026-01-28" "2026-01-28" "2026-01-28" "2026-01-28" ... ## $ LifeExp : num 71.7 78.2 77.4 70.9 67.4 ... ## $ GDP : num 2347 12971 76657 4731 1538 ... ## $ Area : num 2973190 9388210 9147420 1892555 770880 ... ## $ Population : int 1425423212 1412175000 334017321 278830529 243700667 223150896 210306415 169384897 144236933 128613117 ... ## $ region : chr "South Asia" "East Asia & Pacific" "North America" "East Asia & Pacific" ... ## $ capital : chr "New Delhi" "Beijing" "Washington D.C." "Jakarta" ... ## $ longitude : num 77.2 116.3 -77 106.8 72.8 ... ## $ latitude : num 28.6 40 38.9 -6.2 30.5 ... ## $ income : chr "Lower middle income" "Upper middle income" "High income" "Upper middle income" ... ## $ lending : chr "IBRD" "IBRD" "Not classified" "IBRD" ... ``` ] --- ## Step 1 — a plain scatter plot .pull-left[ ``` r plot(wdi$GDP/1e3, wdi$LifeExp) ``` Very basic. - How to deal with clustering? ] .pull-right[ <img src="PlottingPartII_files/figure-html/plain-1.png" alt="" style="display: block; margin: auto;" /> ] --- ## Step 1b — log scale on x .pull-left[ ``` r par(cex.lab = 1.2, las = 1, mgp = c(2, .25, 0), tck = 0.01) plot(wdi$GDP/1e3, wdi$LifeExp, log = "x", xlab = "GDP per capita (1000 USD, log scale)", ylab = "Life expectancy (years)") ``` `log = "x"` applies a log axis (`log = "y"`, `log = "xy"` also work) `las = 1` — horizontal tick labels `tck = 0.01` — inward ticks `mgp` — controls axis label / tick label / tick line positions ] .pull-right[ <img src="PlottingPartII_files/figure-html/logplot-1.png" alt="" style="display: block; margin: auto;" /> ] --- .pull-left[ ## `par()` reference | parameter | what it does | |-----------|-------------| | `cex.lab` | axis label size | | `cex.axis`| tick label size | | `mar` | margins (bottom, left, top, right) | | `mgp` | axis title, tick label, tick line positions | | `las` | tick label orientation | | `tck` | tick length (+inward, −outward) | | `family` | font family (`"serif"`, `"sans"`, `"mono"`) | | `bty` | box type (`"o"`, `"l"`, `"u"`, `"c"`, `"n"`) ] .pull-right[ ``` r par("mar") # default margins ``` ``` ## [1] 5.1 4.1 4.1 2.1 ``` ``` r par("mgp") # default mgp ``` ``` ## [1] 3 1 0 ``` Here are some of my preferences: ``` r par(mar = c(3,3,2,2), mgp = c(1.5,.25,0), tck = 0.01, bty = "l") ``` ] --- .pull-left[ ## Step 2 — size points by population .small[ ``` r par(cex.lab = 1.2, las = 1, mgp = c(2, .25, 0), tck = 0.01) plot(wdi$GDP/1e3, wdi$LifeExp, log = "x", xlab = "GDP per capita ($1000 USD, log scale)", ylab = "Life expectancy (years)", cex = sqrt(wdi$Population / max(wdi$Population, na.rm = TRUE)) * 15) ``` ] `cex` controls point size `sqrt()` makes **area** proportional to population ] .pull-right[ <img src="PlottingPartII_files/figure-html/cex-pop-1.png" alt="" style="display: block; margin: auto;" /> ] --- ## Why `sqrt()`? -- .pull-left[ A circle's **area** = π r² If we set `cex` proportional to population directly, the *radius* scales linearly — but our eye reads **area**. To make area proportional to population: `$$r \propto \sqrt{\text{population}}$$` So we use: ```r cex = sqrt(Population / max_pop) * 15 ``` The `* 15` just scales up to a visible range. ] .pull-right[ <img src="PlottingPartII_files/figure-html/sqrt-demo-1.png" alt="" style="display: block; margin: auto;" /> ] --- .pull-left[ ## Step 2b — add transparency .small[ ``` r par(cex.lab = 1.2, las = 1, mgp = c(2, .25, 0), tck = 0.01) plot(wdi$GDP/1e3, wdi$LifeExp, log = "x", xlab = "GDP per capita (1000 USD, log scale)", ylab = "Life expectancy (years)", cex = sqrt(wdi$Population / max(wdi$Population, na.rm = TRUE)) * 15, pch = 21, col = "grey40", bg = rgb(0, 0, 0, 0.25)) ``` ] `pch = 21` — filled circles with separate border/fill `rgb(r, g, b, alpha)` — alpha: 0 = transparent, 1 = opaque Transparency reveals overlapping points. ] .pull-right[ <img src="PlottingPartII_files/figure-html/transparent-1.png" alt="" style="display: block; margin: auto;" /> ] --- ## `rgb()` and color in R .pull-left[.small[ ``` r # Pure red, 50% transparent rgb(1, 0, 0, 0.5) # Any named color with alpha: adjustcolor("steelblue", alpha.f = 0.4) # scales package convenience function: scales::alpha("steelblue", 0.4) ``` ``` r rgb(1, 0, 0, 0.5) ``` ``` ## [1] "#FF000080" ``` ``` r adjustcolor("steelblue", alpha.f = 0.4) ``` ``` ## [1] "#4682B466" ``` ]] .pull-right[ All values 0–1 (or use `maxColorValue = 255`) <img src="PlottingPartII_files/figure-html/unnamed-chunk-6-1.png" alt="" style="display: block; margin: auto;" /> ] --- .pull-left[ ## Step 3 — color by region .small[ ``` r wdi$region <- factor(wdi$region) cols <- c( "East Asia & Pacific" = "steelblue", "Europe & Central Asia" = "forestgreen", "Latin America & Caribbean" = "goldenrod", "Middle East & North Africa" = "darkorange", "North America" = "mediumpurple", "South Asia" = "firebrick", "Sub-Saharan Africa" = "sienna") cont_col <- cols[match(wdi$region, levels(wdi$region))] plot(wdi$GDP/1e3, wdi$LifeExp, log = "x", xlab = "GDP per capita (1000 USD, log scale)", ylab = "Life expectancy (years)", cex = sqrt(wdi$Population / max(wdi$Population, na.rm=TRUE))*15, pch = 21, col = cont_col, bg = scales::alpha(cont_col, .7)) ``` ]] .pull-right[ <img src="PlottingPartII_files/figure-html/region-col-1.png" alt="" style="display: block; margin: auto;" /> ] --- ## How `match()` maps colors to categories .small[ `levels()` gives factor levels in order ``` r levels(wdi$region) ``` ``` ## [1] "East Asia & Pacific" "Europe & Central Asia" ## [3] "Latin America & Caribbean" "Middle East & North Africa" ## [5] "North America" "South Asia" ## [7] "Sub-Saharan Africa" ``` `match()` returns each row's position in that ordered list ``` r wdi$region[1:20] ``` ``` ## [1] South Asia East Asia & Pacific ## [3] North America East Asia & Pacific ## [5] South Asia Sub-Saharan Africa ## [7] Latin America & Caribbean South Asia ## [9] Europe & Central Asia Latin America & Caribbean ## [11] Sub-Saharan Africa East Asia & Pacific ## [13] East Asia & Pacific Middle East & North Africa ## [15] Sub-Saharan Africa East Asia & Pacific ## [17] Middle East & North Africa Europe & Central Asia ## [19] Europe & Central Asia East Asia & Pacific ## 7 Levels: East Asia & Pacific ... Sub-Saharan Africa ``` ``` r match(wdi$region, levels(wdi$region))[1:20] ``` ``` ## [1] 6 1 5 1 6 7 3 6 2 3 7 1 1 4 7 1 4 2 2 1 ``` `cols[match(...)]` picks the right color by index ``` r cols[match(wdi$region, levels(wdi$region))] ``` ``` ## South Asia East Asia & Pacific ## "firebrick" "steelblue" ## North America East Asia & Pacific ## "mediumpurple" "steelblue" ## South Asia Sub-Saharan Africa ## "firebrick" "sienna" ## Latin America & Caribbean South Asia ## "goldenrod" "firebrick" ## Europe & Central Asia Latin America & Caribbean ## "forestgreen" "goldenrod" ## Sub-Saharan Africa East Asia & Pacific ## "sienna" "steelblue" ## East Asia & Pacific Middle East & North Africa ## "steelblue" "darkorange" ## Sub-Saharan Africa East Asia & Pacific ## "sienna" "steelblue" ## Middle East & North Africa Europe & Central Asia ## "darkorange" "forestgreen" ## Europe & Central Asia East Asia & Pacific ## "forestgreen" "steelblue" ## Europe & Central Asia Europe & Central Asia ## "forestgreen" "forestgreen" ## Sub-Saharan Africa Sub-Saharan Africa ## "sienna" "sienna" ## Europe & Central Asia Sub-Saharan Africa ## "forestgreen" "sienna" ## East Asia & Pacific Latin America & Caribbean ## "steelblue" "goldenrod" ## East Asia & Pacific Sub-Saharan Africa ## "steelblue" "sienna" ## Europe & Central Asia Sub-Saharan Africa ## "forestgreen" "sienna" ## Middle East & North Africa Latin America & Caribbean ## "darkorange" "goldenrod" ## Middle East & North Africa Europe & Central Asia ## "darkorange" "forestgreen" ## South Asia North America ## "firebrick" "mediumpurple" ## Middle East & North Africa Europe & Central Asia ## "darkorange" "forestgreen" ## Sub-Saharan Africa Europe & Central Asia ## "sienna" "forestgreen" ## East Asia & Pacific Latin America & Caribbean ## "steelblue" "goldenrod" ## Sub-Saharan Africa Sub-Saharan Africa ## "sienna" "sienna" ## Middle East & North Africa Sub-Saharan Africa ## "darkorange" "sienna" ## Sub-Saharan Africa South Asia ## "sienna" "firebrick" ## Latin America & Caribbean Sub-Saharan Africa ## "goldenrod" "sienna" ## East Asia & Pacific Sub-Saharan Africa ## "steelblue" "sienna" ## Sub-Saharan Africa Sub-Saharan Africa ## "sienna" "sienna" ## Middle East & North Africa South Asia ## "darkorange" "firebrick" ## Sub-Saharan Africa Sub-Saharan Africa ## "sienna" "sienna" ## Europe & Central Asia Latin America & Caribbean ## "forestgreen" "goldenrod" ## Europe & Central Asia Sub-Saharan Africa ## "forestgreen" "sienna" ## Latin America & Caribbean Latin America & Caribbean ## "goldenrod" "goldenrod" ## Europe & Central Asia Sub-Saharan Africa ## "forestgreen" "sienna" ## East Asia & Pacific Sub-Saharan Africa ## "steelblue" "sienna" ## Sub-Saharan Africa Sub-Saharan Africa ## "sienna" "sienna" ## Sub-Saharan Africa Sub-Saharan Africa ## "sienna" "sienna" ## Middle East & North Africa Latin America & Caribbean ## "darkorange" "goldenrod" ## Europe & Central Asia Latin America & Caribbean ## "forestgreen" "goldenrod" ## Middle East & North Africa Latin America & Caribbean ## "darkorange" "goldenrod" ## Latin America & Caribbean Europe & Central Asia ## "goldenrod" "forestgreen" ## Europe & Central Asia Latin America & Caribbean ## "forestgreen" "goldenrod" ## Europe & Central Asia Europe & Central Asia ## "forestgreen" "forestgreen" ## East Asia & Pacific Europe & Central Asia ## "steelblue" "forestgreen" ## Europe & Central Asia Middle East & North Africa ## "forestgreen" "darkorange" ## Europe & Central Asia Middle East & North Africa ## "forestgreen" "darkorange" ## Europe & Central Asia Sub-Saharan Africa ## "forestgreen" "sienna" ## Europe & Central Asia Europe & Central Asia ## "forestgreen" "forestgreen" ## Sub-Saharan Africa East Asia & Pacific ## "sienna" "steelblue" ## East Asia & Pacific Europe & Central Asia ## "steelblue" "forestgreen" ## Middle East & North Africa Europe & Central Asia ## "darkorange" "forestgreen" ## Latin America & Caribbean Latin America & Caribbean ## "goldenrod" "goldenrod" ## Europe & Central Asia Europe & Central Asia ## "forestgreen" "forestgreen" ## Latin America & Caribbean Sub-Saharan Africa ## "goldenrod" "sienna" ## Europe & Central Asia Middle East & North Africa ## "forestgreen" "darkorange" ## East Asia & Pacific Europe & Central Asia ## "steelblue" "forestgreen" ## Europe & Central Asia Europe & Central Asia ## "forestgreen" "forestgreen" ## Sub-Saharan Africa Europe & Central Asia ## "sienna" "forestgreen" ## Latin America & Caribbean East Asia & Pacific ## "goldenrod" "steelblue" ## Middle East & North Africa Sub-Saharan Africa ## "darkorange" "sienna" ## Middle East & North Africa Middle East & North Africa ## "darkorange" "darkorange" ## Latin America & Caribbean Europe & Central Asia ## "goldenrod" "forestgreen" ## Europe & Central Asia East Asia & Pacific ## "forestgreen" "steelblue" ## Latin America & Caribbean Europe & Central Asia ## "goldenrod" "forestgreen" ## Europe & Central Asia Sub-Saharan Africa ## "forestgreen" "sienna" ## Latin America & Caribbean Europe & Central Asia ## "goldenrod" "forestgreen" ## Middle East & North Africa Sub-Saharan Africa ## "darkorange" "sienna" ## Europe & Central Asia Europe & Central Asia ## "forestgreen" "forestgreen" ## Sub-Saharan Africa Sub-Saharan Africa ## "sienna" "sienna" ## Sub-Saharan Africa Europe & Central Asia ## "sienna" "forestgreen" ## Sub-Saharan Africa Europe & Central Asia ## "sienna" "forestgreen" ## Europe & Central Asia Sub-Saharan Africa ## "forestgreen" "sienna" ## Europe & Central Asia Middle East & North Africa ## "forestgreen" "darkorange" ## East Asia & Pacific Latin America & Caribbean ## "steelblue" "goldenrod" ## Europe & Central Asia Europe & Central Asia ## "forestgreen" "forestgreen" ## Sub-Saharan Africa Sub-Saharan Africa ## "sienna" "sienna" ## Middle East & North Africa East Asia & Pacific ## "darkorange" "steelblue" ## Sub-Saharan Africa Latin America & Caribbean ## "sienna" "goldenrod" ## East Asia & Pacific South Asia ## "steelblue" "firebrick" ## East Asia & Pacific Europe & Central Asia ## "steelblue" "forestgreen" ## Europe & Central Asia Latin America & Caribbean ## "forestgreen" "goldenrod" ## Middle East & North Africa South Asia ## "darkorange" "firebrick" ## Sub-Saharan Africa East Asia & Pacific ## "sienna" "steelblue" ## Latin America & Caribbean Latin America & Caribbean ## "goldenrod" "goldenrod" ## Europe & Central Asia East Asia & Pacific ## "forestgreen" "steelblue" ## East Asia & Pacific Latin America & Caribbean ## "steelblue" "goldenrod" ## East Asia & Pacific Sub-Saharan Africa ## "steelblue" "sienna" ## East Asia & Pacific Latin America & Caribbean ## "steelblue" "goldenrod" ## Europe & Central Asia East Asia & Pacific ## "forestgreen" "steelblue" ## Latin America & Caribbean East Asia & Pacific ## "goldenrod" "steelblue" ## Sub-Saharan Africa Latin America & Caribbean ## "sienna" "goldenrod" ## East Asia & Pacific Latin America & Caribbean ## "steelblue" "goldenrod" ## Latin America & Caribbean East Asia & Pacific ## "goldenrod" "steelblue" ## Latin America & Caribbean Latin America & Caribbean ## "goldenrod" "goldenrod" ## Europe & Central Asia Europe & Central Asia ## "forestgreen" "forestgreen" ## Latin America & Caribbean Latin America & Caribbean ## "goldenrod" "goldenrod" ## North America Europe & Central Asia ## "mediumpurple" "forestgreen" ## Europe & Central Asia East Asia & Pacific ## "forestgreen" "steelblue" ## Latin America & Caribbean East Asia & Pacific ## "goldenrod" "steelblue" ## Latin America & Caribbean Latin America & Caribbean ## "goldenrod" "goldenrod" ## East Asia & Pacific Europe & Central Asia ## "steelblue" "forestgreen" ## Europe & Central Asia Europe & Central Asia ## "forestgreen" "forestgreen" ## Latin America & Caribbean East Asia & Pacific ## "goldenrod" "steelblue" ## East Asia & Pacific East Asia & Pacific ## "steelblue" "steelblue" ``` ] --- .pull-left[ ## Step 4 — add a legend ``` r par(cex.lab = 1.2, las = 1, mar = c(4, 4, 2, 2), mgp = c(2, .25, 0), tck = 0.01) plot(wdi$GDP/1e3, wdi$LifeExp, log = "x", xlab = "GDP per capita (1000 USD, log scale)", ylab = "Life expectancy (years)", cex = sqrt(wdi$Population / max(wdi$Population, na.rm=TRUE))*15, pch = 21, col = cont_col, bg = scales::alpha(cont_col, .7)) legend("bottomright", title = "Region", legend = levels(wdi$region), pt.bg = cols, col = "grey40", pch = 21, pt.cex = 1.8, bty = "n") ``` ] .pull-right[ <img src="PlottingPartII_files/figure-html/legend-plot-1.png" alt="" style="display: block; margin: auto;" /> ] --- ## `legend()` key arguments .pull-left[ ``` r legend( x = "bottomright", # position or coordinates legend = ..., # text labels col = ..., # point/line colors pch = ..., # point character lty = ..., # line types (if lines) pt.cex = 2, # symbol size in legend cex = 0.9, # text size bty = "n", # no box around legend title = "Region" # optional title ) ``` Position keywords: `"topleft"`, `"topright"`, `"bottomleft"`, `"bottomright"`, `"top"`, `"right"`, ... Or give exact coordinates: ```r legend(x = 0.5, y = 60, ...) ``` ] .pull-right[ For **filled** circles (`pch = 21`), use both `col` (border) and `pt.bg` (fill): ``` r legend("bottomright", legend = levels(wdi$region), pt.bg = cols, # fill col = "grey40", # border pch = 21, pt.cex = 1.8, bty = "n") ``` `pch 21–25` are **filled** shapes — border color (`col`) and fill (`bg` or `pt.bg`) are independent. ] --- .pull-left[ ## Step 5 — reorder by population .small[ ``` r # Sort descending so large (populous) countries # are drawn first — smaller ones on top wdi <- wdi[order(wdi$Population, decreasing = TRUE), ] cont_col <- cols[match(wdi$region, levels(wdi$region))] cex_pop <- sqrt(wdi$Population / max(wdi$Population, na.rm = TRUE)) * 15 par(cex.lab = 1.2, las = 1, mar = c(4, 4, 2, 2), mgp = c(2, .25, 0), tck = 0.01) plot(wdi$GDP/1e3, wdi$LifeExp, log = "x", xlab = "GDP per capita (1000 USD, log scale)", ylab = "Life expectancy (years)", cex = cex_pop, pch = 21, col = cont_col, bg = scales::alpha(cont_col, .7)) legend("bottomright", title = "Region", legend = levels(wdi$region), pt.bg = cols, col = "grey40", pch = 21, pt.cex = 1.8, bty = "n") ``` ]] .pull-right[ <img src="PlottingPartII_files/figure-html/reorder-1.png" alt="" style="display: block; margin: auto;" /> Large countries drawn first → small countries on top and visible. ] --- .pull-left[ ## Step 6 — label large countries .small[ ] ``` r # (continuing from previous slide — plot already drawn) f() wdi_big <- subset(wdi, Population > 1e8) text(labels = wdi_big$country, x = wdi_big$GDP/1e3, y = wdi_big$LifeExp, cex = cex_pop[wdi$Population > 1e8] / 5, adj = 0.5) ``` `adj = 0.5` — centered on the point (`adj`: 0=left-justified, 1=right-justified) `font = 2` — **bold** `font = 3` — *italic* `pos = 4` — offset to the right of coordinates ] .pull-right[ <img src="PlottingPartII_files/figure-html/text-full-1.png" alt="" style="display: block; margin: auto;" /> ] --- .pull-left[ ## Step 7 — custom grid lines with `abline()` .small[ ``` r # type="n" draws axes but NO points — # then add grid, then points on top plot(..., type = "n") abline(h = seq(40, 90, 10), col = "grey80", lty = 3) abline(v = c(.5, 1, 2, 5, 10, 20, 50, 100), col = "grey80", lty = 3) points(...) # drawn on top of grid ``` `abline(h = ...)` — horizontal lines at given y values `abline(v = ...)` — vertical lines at given x values Preferred over `grid()` here because the x-axis is log-scale — `grid()` spaces lines at tick positions, `abline()` lets you choose exact values. ]] .pull-right[ <img src="PlottingPartII_files/figure-html/grid-show-1.png" alt="" style="display: block; margin: auto;" /> ] --- .pull-left[ ## Step 8 — a second legend for point size ``` r legend("right", title = "Population (millions)", legend = c(10, 50, 100, NA, 500), pt.cex = sqrt( c(10, 50, 100, NA, 500) * 1e6 / max(wdi$Population, na.rm = TRUE) ) * 15, col = "darkgrey", pch = 21, pt.bg = "grey", bty = "n", ncol = 1) ``` Key trick: `pt.cex` uses the **same formula** as the plot, applied to round reference population values. `NA` entries create blank rows — useful for spacing. ] .pull-right[ <img src="PlottingPartII_files/figure-html/size-legend-show-1.png" alt="" style="display: block; margin: auto;" /> ] --- ## The full code ``` r wdi <- read.csv("WDI.csv") wdi <- wdi[order(wdi$Population, decreasing = TRUE), ] wdi$region <- factor(wdi$region) cols <- c("East Asia & Pacific" = "steelblue", "Europe & Central Asia" = "forestgreen", "Latin America & Caribbean" = "goldenrod", "Middle East & North Africa" = "darkorange", "North America" = "mediumpurple", "South Asia" = "firebrick", "Sub-Saharan Africa" = "sienna") cont_col <- cols[match(wdi$region, levels(wdi$region))] cex_pop <- sqrt(wdi$Population / max(wdi$Population, na.rm = TRUE)) * 15 par(cex.lab = 1.2, mar = c(4, 4, 4, 2), mgp = c(2, .25, 0), tck = 0.01, family = "serif", las = 1) plot(wdi$GDP/1e3, wdi$LifeExp, log = "x", ylab = "Life expectancy (years)", xlab = "GDP per capita (1000 USD, log scale)", type = "n") title("Health vs. Wealth by Nations (2020)", cex.main = 2, font.main = 1) title(sub = "World Development Indicators (World Bank) - accessed via the WDI package in R)") abline(h = seq(40, 90, 5), col = "grey80", lty = 3) abline(v = c(.5,1,2,5,10,20,50,100), col = "grey80", lty = 3) points(wdi$GDP/1e3, wdi$LifeExp, cex = cex_pop + .5, pch = 21, col = cont_col, bg = scales::alpha(cont_col, .7)) legend("bottomright", title = "Region", legend = levels(wdi$region), pt.bg = cols, col = "grey40", pch = 21, pt.cex = 1.8, bty = "n") legend("right", title = "Population (millions)", legend = c(10, 50, 100, NA, 500), pt.cex = sqrt(c(10, 50, 100, NA, 500)*1e6 / max(wdi$Population, na.rm = TRUE))*15, col = "darkgrey", pch = 21, pt.bg = "grey", bty = "n") wdi_big <- subset(wdi, Population > 1e8) text(labels = wdi_big$country, x = wdi_big$GDP/1e3, y = wdi_big$LifeExp, cex = cex_pop[wdi$Population > 1e8]/5, adj = 0.5) ``` --- ## Summary: dimensions encoded in one plot | Visual channel | Variable | R tool | |----------------|----------|--------| | x position (log) | GDP per capita | `log = "x"` | | y position | Life expectancy | `plot(x, y)` | | Point **size** | Population | `cex = sqrt(pop/max_pop) * 15` | | Point **color** | Region | named `cols` + `match()` | | Text labels | Country name | `text(labels = ...)` | **Key functions:** `par()` · `plot()` · `points()` · `abline()` · `rgb()` · `scales::alpha()` · `match()` · `legend()` · `order()` · `subset()` · `text()` · `title()` --- ## Exercise Using the `wdi` data make a scatter plot of **latitude** vs. **longitude** - Size points by **Area** - Color the poins by **region** - Make the color transparency *proportional to the density: i.e. Population/Area* - Make the **shape of the point** (e.g. circle, square, triangle ... options in `?points`) reflect the **income bracket**. - Label the **capitals** of the 20 largest countries - Create a legend labeling the **point types / income brackets** and **region colors** - Make it PRETTY!