|
| 1 | +--- |
| 2 | +title: "Week 10: Faceting, SVGs, and Maps" |
| 3 | +toc: true |
| 4 | +--- |
| 5 | +<!-- |
| 6 | +```js |
| 7 | +Inputs.table(penguins) |
| 8 | +``` --> |
| 9 | + |
| 10 | +```js |
| 11 | +Plot.plot({ |
| 12 | + title: "Penguins facets", |
| 13 | + marginLeft: 100, |
| 14 | + marginRight:100, |
| 15 | + y: { grid: true }, |
| 16 | + x: { grid: true }, |
| 17 | + marks: [ |
| 18 | + Plot.frame(), |
| 19 | + Plot.barX(penguins, |
| 20 | + Plot.groupY( |
| 21 | + { x: "count" }, |
| 22 | + { y: "island", tip: true, fill: "sex", fy: "species" } |
| 23 | + ) |
| 24 | + ) |
| 25 | + ], |
| 26 | +}) |
| 27 | +``` |
| 28 | + |
| 29 | +<div id="important">HTML</div> |
| 30 | + |
| 31 | +```js |
| 32 | +document.getElementById("important").innerText = "CHANGED" |
| 33 | +``` |
| 34 | + |
| 35 | +```js |
| 36 | +Plot.plot({ |
| 37 | + title: "Penguins scatterplot", |
| 38 | + y: { grid: true }, |
| 39 | + x: { grid: true }, |
| 40 | + height: 400, |
| 41 | + width: 600, |
| 42 | + marks: [ |
| 43 | + Plot.frame(), |
| 44 | + Plot.dot(penguins, { |
| 45 | + x: "culmen_length_mm", |
| 46 | + y: "culmen_depth_mm", |
| 47 | + tip: true, |
| 48 | + fill: "species" |
| 49 | + } |
| 50 | + ) |
| 51 | + ], |
| 52 | +}) |
| 53 | +``` |
| 54 | + |
| 55 | +```js |
| 56 | + |
| 57 | +// const width = 600 |
| 58 | + |
| 59 | +const xDataDomain = d3.extent(penguins.map(p => p.culmen_length_mm)) |
| 60 | +display(xDataDomain) |
| 61 | + |
| 62 | +const yDataDomain = d3.extent(penguins.map(p => p.culmen_depth_mm)) |
| 63 | +display(yDataDomain) |
| 64 | + |
| 65 | +const d3XScale = d3.scaleLinear() |
| 66 | + .domain(xDataDomain) |
| 67 | + .range([0, 600]) |
| 68 | + |
| 69 | +// display(d3XScale(33)) |
| 70 | +// display(d3XScale(40)) |
| 71 | +// display(d3XScale(58)) |
| 72 | + |
| 73 | +const d3YScale = d3.scaleLinear() |
| 74 | + .domain(yDataDomain) |
| 75 | + .range([400, 0]) |
| 76 | + |
| 77 | +// display(d3YScale(23)) |
| 78 | +// display(d3YScale(18)) |
| 79 | +// display(d3YScale(20)) |
| 80 | +display(d3YScale(50)) |
| 81 | +``` |
| 82 | + |
| 83 | + |
| 84 | + |
| 85 | +```js |
| 86 | +const svg = d3.create('svg') |
| 87 | + .attr("width", 600) |
| 88 | + .attr("height", 400) |
| 89 | + |
| 90 | +svg.selectAll() |
| 91 | + .data(penguins) |
| 92 | + .join("rect") |
| 93 | + .attr("x", p => d3XScale(p.culmen_length_mm)) |
| 94 | + // .attr("cy", 10) |
| 95 | + .attr("y", p => d3YScale(p.culmen_depth_mm)) |
| 96 | + .attr("height", 10) |
| 97 | + .attr("width", 10) |
| 98 | + .attr("fill", penguin => penguin.species === "Adelie" ? "#efb118" : penguin.species === "Chinstrap" ? "#4269d0" : "#ff725c") |
| 99 | + |
| 100 | +view(svg.node()) |
| 101 | +``` |
| 102 | + |
| 103 | + |
| 104 | +```js |
| 105 | +const geo = await FileAttachment('unemployment_data/geo.json').json() |
| 106 | +display(geo) |
| 107 | + |
| 108 | +const unemployment = await FileAttachment('unemployment_data/us-county-unemployment.csv') |
| 109 | + .csv({ typed: false }) |
| 110 | + .then((data) => data.map(d => ({ ...d, rate: +d.rate}))) |
| 111 | +display(unemployment) |
| 112 | +``` |
| 113 | + |
| 114 | +```js |
| 115 | +const counties = topojson.feature(geo, geo.objects.counties) |
| 116 | +display(counties) |
| 117 | + |
| 118 | +const states = topojson.feature(geo, geo.objects.states) |
| 119 | +display(states) |
| 120 | + |
| 121 | +const nation = topojson.feature(geo, geo.objects.nation) |
| 122 | +display(nation) |
| 123 | +``` |
| 124 | + |
| 125 | +```js |
| 126 | +const iterationResults = unemployment.map(d => [d.id, d.rate]) |
| 127 | +display(iterationResults) |
| 128 | +const lookup = new Map(iterationResults) |
| 129 | +display(lookup) |
| 130 | +display(lookup.get("01001")) |
| 131 | +// new Map([[0, 1], [2, 3]]) |
| 132 | +``` |
| 133 | + |
| 134 | +```js |
| 135 | +Plot.plot({ |
| 136 | + projection: "albers-usa", |
| 137 | + color: { |
| 138 | + type: "quantile", |
| 139 | + n: 9, |
| 140 | + scheme: "blues", |
| 141 | + }, |
| 142 | + marks: [ |
| 143 | + Plot.geo(counties, { |
| 144 | + // fill: "pink", |
| 145 | + stroke: "white", |
| 146 | + tip: true, |
| 147 | + title: county => `${county.properties.name} (${county.properties.id}) |
| 148 | + Unemployment: ${lookup.get(county.properties.id)} |
| 149 | + `, |
| 150 | + fill: county => lookup.get(county.properties.id) |
| 151 | + }), |
| 152 | + Plot.geo(states, { stroke: "blue" }), |
| 153 | + Plot.geo(nation, { stroke: "red" }), |
| 154 | + ] |
| 155 | +}) |
| 156 | +``` |
| 157 | + |
0 commit comments