Vega Lite
mark
How Can You Create Square Marks in Vega-Lite?

How Can You Create Square Marks in Vega-Lite?

If you're venturing into the world of data visualization with Vega-Lite, you might wonder how to use square marks effectively. These little squares can pack a punch when it comes to visualizing your data. Let's walk through how to set them up, with some handy examples to make things crystal clear.

Quick Summary

To create square marks in Vega-Lite, here's a simple reference:

// Single View Specification
{
  "data": ... ,
  "mark": "square",
  "encoding": ... ,
  ...
}

Square marks work similarly to point marks but have two key differences:

  1. The shape is always set to square.
  2. They are filled by default.

Square Mark Properties

Square marks in Vega-Lite come with a variety of properties. You can use all standard mark properties, but here are some special ones exclusive to squares:

  • Size: Defines the size of each square.

Example: Scatterplot with Square Marks

Visualizing your data with square marks can be extremely effective in scatterplots. Here’s an example to get you started:

{
  "data": {
    "values": [
      {"a": 1, "b": 28}, {"a": 2, "b": 55}, {"a": 3, "b": 43},
      {"a": 4, "b": 91}, {"a": 5, "b": 81}, {"a": 6, "b": 53},
      {"a": 7, "b": 19}, {"a": 8, "b": 87}, {"a": 9, "b": 52}
    ]
  },
  "mark": "square",
  "encoding": {
    "x": {"field": "a", "type": "ordinal"},
    "y": {"field": "b", "type": "quantitative"}
  }
}

Configuring Square Marks Globally

You can also set up default properties for all square marks at the top level of your configuration. This way, you maintain consistent styling across your visualizations:

// Top-level View Specification
{
  ...
  "config": {
    "square": {
      "size": 100,
      "color": "steelblue"
    },
    ...
  }
}

Setting square properties globally ensures all square marks in your visualization adopt these settings unless overridden by specific encodings.

FAQ

1. What are square marks best used for?

Square marks are excellent for scatterplots, where you need to display individual data points with a filled shape.

2. How do square marks differ from point marks?

Square marks are always shown as filled squares, whereas point marks can have various shapes and might not be filled by default.

3. Can I customize the size and color of square marks?

Yes, you can customize properties like size and color either within individual visualizations or globally through the config object.

By using square marks, you can create clean and descriptive data visualizations that effectively communicate your data insights.


Hope this helps in making your data visualization journey with Vega-Lite smoother and more enjoyable. Happy visualizing!