Using Vega-Lite’s geoshape mark to draw GeoJSON regions
Vega-Lite’s geoshape mark draws geographic points, lines, and areas from GeoJSON after a map projection converts them to screen space. To map a measure to regional boundaries, join geometry and measure data by a stable region ID, put the GeoJSON field in the shape channel, and encode the value in a channel such as color.
Using Vega-Lite’s geoshape mark to draw GeoJSON regions
Vega-Lite’s geoshape mark draws geographic points, lines, and areas from GeoJSON after a map projection converts them to screen space. To map a measure to regional boundaries, join geometry and measure data by a stable region ID, put the GeoJSON field in the shape channel, and encode the value in a channel such as color.
What the geoshape mark draws
A mark is the geometric element used to represent a data item. Instead of repeating a standard symbol such as a circle or point, geoshape uses the GeoJSON geometry in each data row as the map shape. It can therefore fill or outline irregular regions such as states, countries, watersheds, or protected areas.
Here, mark: "geoshape" specifies how to render the data, while encoding.shape identifies the field that contains the GeoJSON geometry. The shape field must contain an actual GeoJSON Geometry or Feature, not a region name or the string "Polygon". Put a quantitative measure in a separate channel such as color. In other words, geography supplies each region’s boundary and the joined measure supplies its color.
Conceptual diagram: geographic boundary geometry is projected onto the screen, then each shape is colored using its associated value. All shapes and colors are illustrative, not real places or observations.
Join boundary data to measurements
A regional table may contain attributes such as region_id and rate, while a boundary file contains coordinates and a separate ID. Before mapping them together, make the key types and formatting rules consistent. For example, if the string ID "0012" is read as the number 12 in one file, its leading zeros disappear and the join can fail. Compare row counts before and after the join, matched IDs, duplicate keys, and measure totals to find missing regions or duplicated aggregation.
The official Vega-Lite map example uses lookup to join region codes in a table to TopoJSON boundaries, converts a named TopoJSON feature object into a GeoJSON FeatureCollection, and passes it to the shape channel. A choropleth depends on a correct key between each geometry and its measure. If a region is split into several boundary fragments, check that all its fragments remain attached after the join.
The following specification uses two invented rectangles and arbitrary values for explanation:
{
"$schema": "https://vega.github.io/schema/vega-lite/v6.json",
"width": 480,
"height": 300,
"projection": {"type": "equalEarth"},
"data": {
"values": [
{
"region": "A",
"rate": 12,
"geometry": {
"type": "Polygon",
"coordinates": [[[0, 0], [3, 0], [3, 3], [0, 3], [0, 0]]]
}
},
{
"region": "B",
"rate": 24,
"geometry": {
"type": "Polygon",
"coordinates": [[[3, 0], [6, 0], [6, 3], [3, 3], [3, 0]]]
}
}
]
},
"mark": {"type": "geoshape", "stroke": "white", "strokeWidth": 1},
"encoding": {
"shape": {"field": "geometry", "type": "geojson"},
"color": {
"field": "rate",
"type": "quantitative",
"title": "Illustrative rate"
},
"tooltip": [
{"field": "region", "type": "nominal", "title": "Region"},
{"field": "rate", "type": "quantitative", "title": "Illustrative rate"}
]
}
}The rectangles are not real boundaries, and the specification was not rendered in a Vega-Lite runtime. To reproduce it with other data, replace geometry with actual GeoJSON and rate with a measure defined for a period and denominator. In this example, shape selects the geometry field, color maps a quantitative value to a continuous color scale, and tooltip exposes the region and value on pointer hover.
Check GeoJSON geometry and map projection
In GeoJSON, a Polygon represents an area with closed linear rings, and a MultiPolygon groups several separate polygons into one geometry. An island region may be represented as a MultiPolygon. A polygon can have an exterior ring and interior holes, so confirm that holes and boundaries survive simplification or conversion. Under RFC 7946, GeoJSON positions use WGS 84 decimal degrees, with longitude then latitude as the first two values. Mixing files that use another order, or reversing the axes, can place shapes in the wrong location.
A map projection converts longitude and latitude to screen x and y positions. A flat map cannot preserve area, shape, distance, and direction all at once. Vega-Lite can create an implicit projection for a geoshape mark or GeoJSON encoding when none is specified; the documented default is Equal Earth. Explicitly choose a projection that fits the map extent—such as one country or the whole world—and the comparison you need. Equal-area projections can still distort regional shapes, so do not describe every distance or shape as accurate.
For a TopoJSON boundary file, set format.type to topojson and feature to the name of the object to extract. Vega-Lite converts that object into a GeoJSON FeatureCollection. A mesh conversion may be useful when you want shared boundary lines that are not associated with individual regions. The feature option treats regions as separate shapes; mesh combines geometry from multiple objects into a unified network.
Validate the result step by step
Check geometry first. Temporarily remove the value color and draw only the boundaries. Confirm location, orientation, islands, holes, and outlines.
Check the ID join. Normalize string/number types, leading zeros, spaces, and letter case. Count unmatched regions and duplicate IDs.
Check the measure’s meaning. State whether it is a total or a rate and identify the denominator and period in the title, legend, or tooltip. A region with a larger total does not necessarily have a higher per-person rate.
Check color scale and missing values. Provide a legend whose ordering can be read, and distinguish regions with no value from regions with low values. Even with an automatic legend, make the units and meaning clear.
Check projection and geometry. Confirm the projection suits the map extent, the coordinates are longitude then latitude, and region IDs and holes survive boundary simplification or TopoJSON conversion.
Check the deployment environment. Validate the specification against the target Vega-Lite schema and inspect the rendered legend, tooltip, boundaries, and missing regions. This article gives a JSON example but does not claim a runtime test.
If the view is blank, first confirm that the shape field contains GeoJSON, the join did not produce null geometry, and the boundary URL or data can be read. If shapes appear in the wrong place, inspect coordinate order, coordinate reference, explicit projection, and map extent. If only lines appear without filled areas, check the polygon ring closure and nested coordinate structure.
References
[Vega-Lite: Geoshape mark](https://vega.github.io/vega-lite/docs/geoshape.html)
[Vega-Lite: Encoding channels and the geojson data type](https://vega.github.io/vega-lite/docs/encoding.html)
[Vega-Lite: Projections](https://vega.github.io/vega-lite/docs/projection.html)
[Vega-Lite: TopoJSON data format](https://vega.github.io/vega-lite/docs/data.html#topojson)
[Vega-Lite example: choropleth with measures joined to county boundaries](https://vega.github.io/vega-lite/examples/interactive_geo_facet_species.html)
[IETF RFC 7946: The GeoJSON Format](https://datatracker.ietf.org/doc/html/rfc7946)