Here, I wanted to see if I could detect any patterns correlating the number of pizza orders and the time of year. This visualization shows us two spikes in total pizza ordering occurring at the end of February and middle of March, and a dip in early November. These sudden changes occur for all the days of the week, however some days are impacted more dramatically. For example, the first spike was not as prominent for pizza orders on day 0 of the week (Sunday). I infer that the second spike, occurring in the middle of March, may be associated with March Madness, which is the largest and most viewed college basketball tournament in the US. The first round begins the second week of March and it is a crazy week with multiple games a day, so people probably host viewing parties or order pizza to sit and watch the games.
"These sudden changes occur for all the days of the week" — incorrect.
visually it's hard to see changes in stacked area charts: the bottom layer transfers its own patterns to all other layers. you'd think all days have a spike, but in fact only 0 (sunday) has a spike in march and only 1 (monday) has a spike in february
instead of stacked area the facet (a.k.a small multiple) approach would be better. you can easily do it in the chart above by setting "Facet Y" dropdown to "day_of_week". then you'll see immediately what i mean
I cleaned and edited the dataset to exclude the size label in the "name" variable and only represent pizza orders by the type. I did this because it was too difficult to visualize patterns with so many categories for this variable. I wanted to find out which type of pizza was the bestseller in this dataset, and after cleaning the data and isolating the type, I found that Hawaiian was the most popular.... which very much upsets me as an Italian (pineapple should never ever go on pizza sorry I don't make the rules)
And while I previously exhibited the comparison of the sum of orders between the different types of pizza, I also wanted to exhibit their ordering trends in a different way: by date.
applying my comment from the previous cell, here you can do it like this
name: row.name.split(" Pizza ")[0],
size: row.name.split(" Pizza ")[1]
then you don't need fixNames dictionary
whenever you have to write a high-effort dictionary like this there is probably an easier way to do it. since all of our pizzas follow `name + " Pizza " + size` pattern we can slice each name by word " Pizza " and turn it into two columns. you can try this in a new cell:
"Buffalo Chicken Pizza Large".split(" Pizza ")
this returns an array. from there the name would be element [0] and size element [1]
you don't really need a function to access a value, you can access a property of an object directly. the above is equivalent to: pizzaorders[99].orders
ah, here i see why you would use an accessor function!
a concise arrow function in place would allow a more direct expression:
maxOrders = d3.max(pizzaorders, d => d.orders)
but you can also argue that your way allows you to avoid repeating the accessor code in many places and you would be right :)
not sure what are you trying to achieve, but is that it?
Plot.plot({
marginLeft: 70, //increase margin to fix longer numbers
marks: [
Plot.barY( //bar instead of rect, easier to use
fixedNames.filter(f => f.name === names), //filter the dataset to only keep the selected name
Plot.groupX({y: "sum"}, {y: "orders", x: "state", fill: "state"}) // group instead of bin
),
Plot.ruleY([0])
],
color: {
legend: true
}
})
however you won't see any difference in pizza orders by name between the two states, this is expected in this synthetic dataset
the problem is that the resulting array of names is not unique.
a dumb approach would be to write a script to manually remove duplicates, but there is this neat feature in JS called Set. A set is a unique list, so by converting our array to Set and back we would get what we want:
replace
fixedNames.map(name)
with
Array.from(new Set(fixedNames.map(name)))
this took me a while to understand and i didn't know how to do temporal binning, you just taught me that!
you are missing a y axis encoding, add this:
y: "orders",
then, you are comparing bin to "day" but your options are ["Day", "Month"], case sensitive
replace "day" with "Day" and it will work
Thank you! I tried to add the y encoding but got feedback "Error: invalid bin reduce: orders" , so I'm gonna have to figure out how to code to bin the orders column first
So for each day, there are 8 entries for pizza orders. Since I removed the size component of the pizza names, there are 4 entries for the same type of pizza on every day per state (small, medium, large, xl were all removed but their original distinguished entries are still separate), and there are 2 states. So, the sum takes into account the total number of orders across all of the entries per pizza type on a day. For example, on January 1 there are 8 rows of counts identified Hawaiian Pizza, 4 from each state-- originally, these had been broken down by size, but now they are all being clumped into the pizza type. So, when there is a darker hue in the heatmap, this means that the sum of orders of a type of pizza across all sizes in the original dataset is high. Did I explain this ok?