September 22 2022

Agenda

  • Variable types
  • Mapping variable to plot elements
  • Bar plots with Vega

Variable Types

Numeric Variables

Numeric Variables

Can you think of examples of these two types of numeric variables?

  • Continuous numeric variables
  • Discrete numeric variables

What are good visualization mappings (or encodings) for continuous and discrete numeric variables?

Categorical Variables

Categorical Variables

Can you think of examples of these two types of categorical variables?

  • Ordered categorical variables
  • Unordered categorical variables

What are good encodings for ordered and unordered categorical variables?

Case Study: Big Foot Sightings

The data

Planning the plot

We will be using the aggregated data for our first bar plot with Vega.

  • What variables should be mapped to which visualization elements?
  • How should things be ordered?
  • Should everything be visualized, or should the data be filtered in some way?

Implementing the plot

Download the starter code for the project.

Make changes to the bigfoot.js file.

Scales

  1. Create two scales in your spec, one for the y axis and another for the y axis. If you are mapping continuous numeric variables, use a linear scale. For categorical variables, use a band scale.

  2. Include a name, type, domain and range properties for each scale

  3. For range you can use the values height and width

  4. For the domain use the following syntax:

    { data: "dataName", field: "variableName" }

Axes

  1. Create two axes, using the scales you created
  2. Axes require two properties:
  • scale – use the ones you created
  • orient, which can be "bottom","left", "right" and "top"

What do the following properties do?

  • offset
  • tickCount
  • grid

Any other interesting property?

Marks

  1. Create an object in marks if type "rect"
  2. Define the from property with the data property mapped to the name you gave your data
  3. For the encode define an enter property with your y and x axes, a fixed height (of 20 for example) and either a y2 or x2 with value 0 (depending on where you are mapping your categorical variable to)

Ordering variables

What type of variable is state in our bigfoot data?

  • Use the transform property in your data object with:
    • type: "collect"
    • sort by the n field with descending order

Adding labels to bars

  • In marks, add a new object of type “text”
  • Use the same data as your other mark
  • Map x and y to the same variables as your other mark
  • Add baseline, dx, and dy to adjust position of labels

Filtering variables

  1. Add another object to the data array

  2. Give it a name, and the source is the name you gave to the json being imported

  3. Add a transform property, which takes as value an array – add an object to this array, with type: "window" and ops: ["row_number"], as: ["rank"]

  4. Add another transform property with type: "filter" and the following expression:

    expr: "datum.rank <= 5"

Titles and labels

Add any titles and labels you think are needed