@@ -5,3 +5,226 @@ toc: true
55
66# Week 7: Transforms and Data Manipulation
77
8+ ``` js
9+ const aapl = FileAttachment (" stock_data/aapl.csv" ).csv ({ typed: true })
10+ ```
11+
12+ ``` js
13+ display (aapl)
14+ ```
15+
16+ ``` js
17+ const filtered = aapl .filter ((d ,i ) => i < 51 )
18+ ```
19+
20+ ``` js
21+ Plot .plot ({
22+ marks: [
23+ Plot .frame (),
24+ // Plot.line(
25+ // filtered,
26+ // {
27+ // x: "Date",
28+ // y: "Open",
29+ // stroke: "green"
30+ // }
31+ // ),
32+ // Plot.line(
33+ // filtered,
34+ // {
35+ // x: "Date",
36+ // y: "Close",
37+ // stroke: "red"
38+ // }
39+ // ),
40+ Plot .rect (filtered, {
41+ x: " Date" , // x1 and x2 can both just be date, so can set x as "Date"
42+ interval: " day" , // this ensures the xscale treats it like a single day, UTC date. Without it, we get an error on the chart.
43+ y1 : (d ) => Math .min (d .Open , d .Close ),
44+ y2 : (d ) => Math .max (d .Open , d .Close ),
45+ opacity: 0.5
46+ })
47+ ]
48+ })
49+ ```
50+
51+
52+ ``` js
53+ // penguins
54+ // const somteingElse = "oiejrwrjit"
55+ // view(Inputs.table(penguins))
56+ Inputs .table (penguins)
57+ ```
58+
59+ Bar chart of count of penguins per island:
60+ ``` js
61+ Plot .plot ({
62+ height: 300 ,
63+ marks: [
64+ Plot .frame (),
65+ Plot .barY (
66+ penguins,
67+ Plot .groupX (
68+ { y: " count" },
69+ { x: " island" } // options
70+ // { x: "species" } // options
71+ )
72+ )
73+ ]
74+ })
75+ ```
76+
77+ ``` js
78+ Inputs .table (diamonds)
79+ ```
80+
81+ ``` js
82+ Plot .plot ({
83+ marks: [
84+ Plot .rectY (
85+ diamonds,
86+ Plot .binX (
87+ { y: " count" },
88+ { x: {
89+ thresholds: 10 ,
90+ value: " price"
91+ },
92+ tip: true }
93+ )
94+ )
95+ ]
96+ })
97+ ```
98+
99+
100+ ``` js
101+ Plot .plot ({
102+ y: {
103+ grid: true
104+ },
105+ marks: [
106+ Plot .ruleY ([0 ]),
107+ Plot .line (aapl, { x: " Date" , y: " Close" , stroke: " lightgrey" , tip: true }),
108+ Plot .lineY (aapl,
109+ Plot .windowY ({
110+ k: 180
111+ }, {
112+ x: " Date" ,
113+ y: " Close" ,
114+ // tip: true
115+ })
116+ )
117+ ]
118+ })
119+ ```
120+
121+
122+
123+
124+
125+ ``` js
126+ Plot .plot ({
127+ marks: [
128+ Plot .dot (penguins,
129+ { x: " culmen_length_mm" , y: " culmen_depth_mm" , tip: true , fill: " species" , r: " body_mass_g" }
130+ )
131+ ]
132+ })
133+ ```
134+
135+
136+ ``` js
137+ Inputs .table (penguins)
138+ ```
139+
140+ ``` js
141+ // { [island]: count, [island]: count }
142+
143+ const countsByIsland = {
144+ // "Torgersen": #
145+ };
146+ for (const p of penguins) {
147+ // display(p)
148+ // countsByIsland["Torgersen"]
149+ if (p .island in countsByIsland) {
150+ countsByIsland[p .island ] = countsByIsland[p .island ] + 1
151+ } else {
152+ countsByIsland[p .island ] = 1
153+ }
154+
155+ // display(countsByIsland)
156+ }
157+
158+ ```
159+
160+
161+ ``` js
162+ // { [island]: count, [island]: count }
163+
164+ const countsByIsland = {
165+ // "Torgersen": #
166+ };
167+ penguins .forEach ((p ) => {
168+ // display(p)
169+ // countsByIsland["Torgersen"]
170+ if (p .island in countsByIsland) {
171+ countsByIsland[p .island ] = countsByIsland[p .island ] + 1
172+ } else {
173+ countsByIsland[p .island ] = 1
174+ }
175+
176+ // display(countsByIsland)
177+ }
178+
179+ ` ` `
180+
181+ ` ` ` js
182+ penguins .forEach (p => console .log (" haha" ))
183+
184+ const newBasket = penguins .map (p => " haha" )
185+ console .log (newBasket)
186+ ` ` `
187+
188+
189+ ` ` ` js
190+ Plot .plot ({
191+ title: " hello world" ,
192+ subtitle: " more" ,
193+ y: {
194+ // margin: { left: 10 }
195+ domain: [100 , 300 ]
196+ },
197+ marks: [
198+ Plot .line (aapl, {
199+ x: " Date" ,
200+ y: " Close"
201+ }),
202+ // Plot.line(aapl, {
203+ // x: "Date",
204+ // // y: (d) => d["Close"] * 1.123,
205+ // y: (d, i) => d["Close"] + (i * 0.23),
206+ // stroke: "pink"
207+ // })
208+ ]
209+ })
210+ ` ` `
211+
212+ <style>
213+ .my-card {
214+ color: "blue"
215+ }
216+
217+ </style>
218+
219+ <div class="my-card" style="font-family: monospace">
220+ ${ Plot .plot ({
221+ title: " hello world" ,
222+ subtitle: " more" ,
223+ marks: [
224+ Plot .line (aapl, {
225+ x: " Date" ,
226+ y: " Close"
227+ }),
228+ ]
229+ })}
230+ </div>
0 commit comments