Vega Lite
mark
How to Utilize Point Marks in Vega-Lite?

How to Utilize Point Marks in Vega-Lite?

Hey there! Interested in creating scatterplots or dot plots to showcase your data? You're in the right place! This guide will walk you through using the point mark in Vega-Lite, from basic properties to exciting examples.

Let’s get started!

{
  "data": ... ,
  "mark": "point",
  "encoding": ... ,
  ...
}

A point mark represents each data point with a symbol and is incredibly useful for visualizations like scatterplots. Nice and simple, right? Let’s dive deeper!

Point Mark Properties 📝

Basic Format

Here's a basic structure for using a point mark:

// Single View Specification
{
  ...
  "mark": {
    "type": "point",
    ...
  },
  "encoding": ... ,
  ...
}

When defining a point mark, you can include various standard mark properties as well as these special ones:

PropertyDescription
shapeThe shape of the symbols representing points.
sizeThe size of the symbols.

Examples 🎨

Let’s get hands-on with some examples to show how powerful point marks can be!

Dot Plot 📍

Mapping a field to either the x or y channels with point marks creates a dot plot.

// Example: Dot Plot
{
  "mark": "point",
  "encoding": {
    "x": { "field": "your_field", "type": "quantitative" }
  },
  "data": {
    "url": "your_data_source"
  }
}

Scatter Plot 📊

By mapping fields to both the x and y channels, you get a scatter plot.

// Example: Scatter Plot
{
  "mark": "point",
  "encoding": {
    "x": { "field": "your_field_x", "type": "quantitative" },
    "y": { "field": "your_field_y", "type": "quantitative" }
  },
  "data": {
    "url": "your_data_source"
  }
}

By default, point marks are just bordered symbols. To fill them, set filled to true:

// Filled Points Example
{
  "mark": { "type": "point", "filled": true },
  "encoding": {
    "x": { "field": "your_field_x", "type": "quantitative" },
    "y": { "field": "your_field_y", "type": "quantitative" }
  },
  "data": {
    "url": "your_data_source"
  }
}

Bubble Plot 🌐

Want to add another dimension? Map a third field to the size channel.

// Example: Bubble Plot
{
  "mark": "point",
  "encoding": {
    "x": { "field": "your_field_x", "type": "quantitative" },
    "y": { "field": "your_field_y", "type": "quantitative" },
    "size": { "field": "your_field_size", "type": "quantitative" }
  },
  "data": {
    "url": "your_data_source"
  }
}

Adding Color and Shape 🌈

You can also use the color and shape channels to encode additional fields.

// Example: Scatter Plot with Color and Shape
{
  "mark": "point",
  "encoding": {
    "x": { "field": "your_field_x", "type": "quantitative" },
    "y": { "field": "your_field_y", "type": "quantitative" },
    "color": { "field": "your_field_color", "type": "nominal" },
    "shape": { "field": "your_field_shape", "type": "nominal" }
  },
  "data": {
    "url": "your_data_source"
  }
}

Adding Jitter ✨

To add some jitter (random offset) on a discrete scale:

// Example: Dot Plot with Jittering
{
  "mark": { "type": "point", "tooltip": {"content": "data"} },
  "encoding": {
    "x": { "field": "your_field_x", "type": "ordinal", "spacing": 15, "axis": {"labelAngle": 0 }},
    "y": { "field": "your_field_y", "type": "quantitative", "bin": true },
    "xOffset": { "field": "random_field", "type": "quantitative" },
    "size": {"value": 100}
  },
  "data": {
    "url": "your_data_source"
  }
}

Wind Vector Map 🌬️

Encode angle to create wind vector maps:

// Example: Wind Vector Map
{
  "mark": "point",
  "encoding": {
    "longitude": { "field": "longitude", "type": "quantitative" },
    "latitude": { "field": "latitude", "type": "quantitative" },
    "angle": { "field": "wind_direction", "type": "quantitative" }
  },
  "data": {
    "url": "your_data_source"
  }
}

Geo Point 🗺️

Visualize geographic points by using latitude and longitude:

// Example: Geo Point
{
  "mark": "point",
  "encoding": {
    "longitude": { "field": "longitude", "type": "quantitative" },
    "latitude": { "field": "latitude", "type": "quantitative" }
  },
  "data": {
    "url": "your_data_source"
  }
}

Point Config 🔧

Want to set default properties for all your point marks? You can do that with the point property in the top-level config object.

// Top-level View Specification
{
  ...
  "config": {
    "point": {
      "filled": true,
      "size": 100
    },
    ...
  }
}

This config sets up default properties, but they will be overridden by specific mark property encoding channels.

FAQ ❓

1. What is the best use of point marks?

Answer: Point marks are fantastic for scatterplots, dot plots, and any visualizations where you need to represent individual data points. They give a clear and precise visual representation.

2. How can I change the color of the point marks?

Answer: You can map a field to the color channel in the encoding part of your visualization specification. This allows you to color the points based on different field values.

3. Can I set a global configuration for all my point marks?

Answer: Absolutely! Use the point property in the top-level config object to set default values for all your point marks, making them consistent across your visualizations.

We hope this guide sets you on the right path to mastering point marks in Vega-Lite. Time to create some stunning visualizations! 🌟