|
| 1 | +--- |
| 2 | +title: "Week 6 Class" |
| 3 | +toc: true |
| 4 | +--- |
| 5 | + |
| 6 | +```js |
| 7 | +const penguins = await FileAttachment("./data/penguins.csv").csv({ typed: true }) |
| 8 | +display(penguins) // data! yay! |
| 9 | +``` |
| 10 | + |
| 11 | + |
| 12 | +<!-- ```js |
| 13 | +for (let i = 0; i < 10; i++) { |
| 14 | + display(i) |
| 15 | +} |
| 16 | +``` --> |
| 17 | + |
| 18 | +<!-- ```js |
| 19 | +Plot.plot({ |
| 20 | + height: 500, // pixels |
| 21 | + y: { |
| 22 | + grid: true |
| 23 | + }, |
| 24 | + marks: [ |
| 25 | + Plot.frame(), |
| 26 | + Plot.line(aapl, { |
| 27 | + x: "Date", |
| 28 | + y: d => d["Close"], |
| 29 | + }), |
| 30 | + Plot.ruleY([-200, 0]), |
| 31 | + ] |
| 32 | +}) |
| 33 | +``` --> |
| 34 | +<!-- |
| 35 | +```js |
| 36 | +Plot.plot({ |
| 37 | + marks: [ |
| 38 | + Plot.frame(), |
| 39 | + Plot.dot(penguins, { |
| 40 | + x: "bill_length_mm", |
| 41 | + y: "bill_depth_mm", |
| 42 | + stroke: "species", |
| 43 | + fill: "island", |
| 44 | + tip: true |
| 45 | + }) |
| 46 | + ] |
| 47 | +}) |
| 48 | +``` --> |
| 49 | + |
| 50 | +<!-- ```js |
| 51 | +const groupOutput = Plot.groupX( |
| 52 | + { y: "count" }, |
| 53 | + { x: "island" } |
| 54 | + ) |
| 55 | +display(groupOutput) |
| 56 | +``` --> |
| 57 | + |
| 58 | + |
| 59 | +<!-- ```js |
| 60 | +Plot.plot({ |
| 61 | + height: 200, |
| 62 | + marks: [ |
| 63 | + Plot.frame(), |
| 64 | + Plot.barX(penguins, |
| 65 | + // groupOutput |
| 66 | + Plot.groupY( |
| 67 | + { x: "count" }, |
| 68 | + { y: "island", fill: "species", fy:"species" } |
| 69 | + ) |
| 70 | + ) |
| 71 | + ] |
| 72 | +}) |
| 73 | +``` --> |
| 74 | + |
| 75 | + |
| 76 | + |
| 77 | +<!-- ```js |
| 78 | +Inputs.table(olympians) |
| 79 | +``` |
| 80 | +
|
| 81 | +```js |
| 82 | +const binOutput = Plot.binX( |
| 83 | + { y: "count" }, |
| 84 | + { x: "weight" } |
| 85 | + ) |
| 86 | +display(binOutput) |
| 87 | +``` |
| 88 | +
|
| 89 | +```js |
| 90 | +Plot.plot({ |
| 91 | + height: 300, |
| 92 | + marks: [ |
| 93 | + Plot.frame(), |
| 94 | + Plot.rectY(olympians, |
| 95 | + Plot.binX( |
| 96 | + { y: "count" }, |
| 97 | + { x: "weight" } |
| 98 | + ) |
| 99 | + ) |
| 100 | + ] |
| 101 | +}) |
| 102 | +``` --> |
| 103 | + |
| 104 | + |
| 105 | + |
| 106 | + |
| 107 | +```js |
| 108 | +Plot.plot({ |
| 109 | + marks: [ |
| 110 | + Plot.barY(penguins, |
| 111 | + Plot.groupX( |
| 112 | + { y: "count" }, |
| 113 | + { |
| 114 | + x: "island", |
| 115 | + fill: "island", // three colors per bar, as per the island |
| 116 | + // fill: "species" // makes a stacked bar chart, with a legend! |
| 117 | + } |
| 118 | + ) |
| 119 | + ) |
| 120 | + ], |
| 121 | + color: { legend: true } |
| 122 | +}) |
| 123 | +``` |
| 124 | + |
| 125 | +```JSON |
| 126 | +[ |
| 127 | + { island: "Biscoe", count: 168 }, |
| 128 | + { island: "Dream", count: 124 }, |
| 129 | + { island: "Torgersen", count: 52 } |
| 130 | +] |
| 131 | +``` |
| 132 | + |
| 133 | +```js |
| 134 | +let dream = 0; |
| 135 | +let biscoe = 0; |
| 136 | +let torgersen = 0; |
| 137 | +for (let p = 0; p < penguins.length; p++) { |
| 138 | + const penguin = penguins[p] |
| 139 | + if (penguin.island === "Dream") dream = dream + 1; |
| 140 | + if (penguin.island === "Biscoe") biscoe = biscoe + 1; |
| 141 | + if (penguin.island === "Torgersen") torgersen = torgersen + 1; |
| 142 | + // display(penguin) |
| 143 | +} |
| 144 | +``` |
| 145 | + |
| 146 | +Biscoe: ${biscoe}, |
| 147 | +Dream: ${dream}, |
| 148 | +Torgersen: ${torgersen} |
| 149 | + |
| 150 | +```js |
| 151 | +const forLoopPenguins = [ |
| 152 | + { island: "Biscoe", count: biscoe }, |
| 153 | + { island: "Dream", count: dream }, |
| 154 | + { island: "Torgersen", count: torgersen } |
| 155 | +] |
| 156 | +display(forLoopPenguins) |
| 157 | +``` |
| 158 | + |
| 159 | + |
| 160 | +```js |
| 161 | +Plot.plot({ |
| 162 | + height: 300, |
| 163 | + marks: [ |
| 164 | + Plot.frame(), |
| 165 | + Plot.barY( |
| 166 | + forLoopPenguins, |
| 167 | + { x: "island", y: "count" } |
| 168 | + ) |
| 169 | + ] |
| 170 | +}) |
| 171 | +``` |
| 172 | + |
| 173 | +```js |
| 174 | +let biscoeCountAgain = 0; |
| 175 | +let dreamCountAgain = 0; |
| 176 | +let torgersenCountAgain = 0; |
| 177 | +penguins.forEach(p => { |
| 178 | + if (p.island === "Biscoe") biscoeCountAgain = biscoeCountAgain + 1 |
| 179 | + if (p.island === "Dream") dreamCountAgain = dreamCountAgain + 1 |
| 180 | + if (p.island === "Torgersen") torgersenCountAgain = torgersenCountAgain + 1 |
| 181 | +}) |
| 182 | +display(biscoeCountAgain) |
| 183 | +display(dreamCountAgain) |
| 184 | +display(torgersenCountAgain) |
| 185 | +``` |
| 186 | + |
| 187 | + |
| 188 | +```js |
| 189 | +const reducedPenguins = penguins.reduce((accumulator, currentValue) => { |
| 190 | + return { |
| 191 | + ...accumulator, |
| 192 | + [currentValue.island]: accumulator[currentValue.island] + 1 |
| 193 | + } |
| 194 | +}, { |
| 195 | + "Biscoe": 0, |
| 196 | + "Dream": 0, |
| 197 | + "Torgersen": 0 |
| 198 | +}) |
| 199 | +display(reducedPenguins) |
| 200 | +``` |
0 commit comments