September 27 2022

Agenda

  • Transforming data:
    • aggregate
    • collect
    • window
    • filter
  • Building bar plots with transformed data

Case Study: Ferris Wheels

The Data

Transforming the data

Question: What is the top 5 countries with the most Ferris Wheels?

Four transform objects:

  1. aggregate by country
  2. sort by the new variable count in descending order
  3. create a new variable called rank
  4. filter to keep the top 5 observations

Scales and axes

Create two scales, one of type band and another linear to map the variables country and count

Add axes to your spec so that you can check your work.

Marks – bars

  1. Add rectangles from the count data you created
  2. Map x and y to your variables
  3. If you have vertical bars, include y2 and width
  4. If you have horizontal bars, include x2 and height

Marks – text

  1. Add text from the count data you created
  2. Map x and y to your variables
  3. For the property text use your numeric variable
  4. Add the dx and dy properties to adjust the text position
  5. You can also change the default for fill

Titles

Add all the titles you deem necessary

Update and hover

  1. add an update element to the rect marks for color
  2. add a hover element as well
update: {
  fill: { value: "black" }
},
hover: {
  fill: { value: "LightGray" },
  strokeWidth: { value: 2 },
  stroke: { value: "black" }
  
}

Other data transformations

{
  name: "aggregated",
  source: "ferrisWheels",
  transform: [
    {
      type: "aggregate",
      groupby: ["country"],
      fields: ["height"],
      ops: ["mean"],
      as: ["m"]
    }
  ]
}