Vector Data Visualization Part 2: Rules and Labels
Credit: Youtube Channel “Statistics Canada”
“`html
Vector Data Visualization Part 2: Rules and Labels in QGIS
In the last article we explored the basics of styling vector layers – symbols, colors, and categories. This post dives deeper into Rule-Based Rendering and Labeling in QGIS, two powerful tools that let you create rich, context-sensitive maps.
Why Use Rule-Based Rendering?
Rule-based rendering (RBR) is the engine behind complex styles such as:
- Dependent on attribute ranges (e.g., population density)
- Geometry-based rules (e.g., draw roads thicker if they are marked as arterial)
- Conditional combinations (e.g., highlight parks in rural areas differently than in cities)
RBR allows you to group multiple style rules into a single layer, eliminating the need for duplicate layers.
Creating a Rule-Based Style
- Open the Layer Properties: Right‑click the layer → Properties → Symbology.
- Change to Rule Renderer: The default is Single Symbol. Click the drop‑down menu and choose Rule Renderer.
- Add a New Rule: Click the + Add button.
- Define the Filter Expression: Use the
Expression
editor to filter features. For example:"pop_density" > 1000
This rule will match all polygons/points with a population density above 1,000.
- Set Symbol Settings: Click the symbol to open the Symbol Selector. Adjust color, width, fill, or other parameters.
- Assign Layer/Canvas Order: If rules overlap, decide which one draws on top.
- Save and Repeat: Create as many rules as needed. QGIS will display a preview in the style panel.
Example: Multi‑Layered Road Styling
“`html
Rule | Filter Expression | Symbol |
---|---|---|
Highway | "type" = ‘motorway’ | Stroke width: 4 mm, Stroke color: red |
Arterial | "type" = ‘primary’ | Stroke width: 3 mm, Stroke color: orange |
Local | "type" = ‘secondary’ | Stroke width: 2 mm, Stroke color: yellow |
“`
Tip: Use Case
or IF
statements if you want a single rule to adjust multiple properties based on attributes:
CASE
WHEN "type" = 'motorway' THEN '[Stroke color="red", Stroke width=4]'
WHEN "type" = 'primary' THEN '[Stroke color="orange", Stroke width=3]'
ELSE '[Stroke color="yellow", Stroke width=2]'
END
Labeling in QGIS
Proper labeling is essential to convey information at a glance. QGIS offers a flexible labeling engine that works with dozen of variable types.
Basic Labeling Steps
- Open Layer Properties → Labels. Turn on labeling by choosing Single labels.
- Select the field containing the text. For example, if you have a field called
name
use it as the label source. - Adjust font, size, color, and placement. Use the Placement tab for advanced controls.
Expression-Based Labels
Expressions let you combine fields, apply formatting, or use conditional logic. Example: Show the county name followed by its year of establishment.
'"' || "county_name" || '" (Established: ' || "est_year" || ')'
Use format_number
or format_date
for numeric or date formatting.
Casing and Text Markup
QGIS 3.x supports text markup tags. Wrap your expression in <u>…</u>
for underlined text, <b>…</b>
for bold, etc. Example:
'¤' + format_number("area_sqkm", 2) + ' <b>(km²)</b>'
Label Placement Strategies
| Placement | Use Case | Description |
|———–|———-|————-|
| Offset | Most simple | Places label at a specific offset from the feature. |
| Perpendicular to line | Road labels | Keeps the text aligned with the road direction. |
| Around point | Scatter points | Uses a circular placement allowing multiple labels to cluster nicely. |
| Label with buffer | Avoid overlap | Creates a buffer zone to prevent label collisions. |
| Low Relief | Terrain mapping | Places labels around walls of elevation models for 3D-like effect. |
If you experience label overlap, experiment with Buffer > Show buffer and set a buffer width that gives labels space.
Conditional Labels
Show labels only when a condition is true. For instance, label only cities with a population > 50,000:
CASE
WHEN "population" > 50000 THEN concat("city_name", ' [', "population", ']')
ELSE NULL
END
Layer Rendering Order and Label Priority
When multiple layers stack up, label priority becomes critical. In Layer Styling Panel, you can set Label priority
from 0 (low) to 10 (high). Items with higher priority will be drawn on top, preventing important text from being obscured.
Exporting and Final Tips
- Print Composer: Use Project → New Print Layout to assemble the final map. Add Map, Legend, Scale Bar, North Arrow etc.
- Export Settings: When exporting to PDF or image, check
Export to vector format
if you need editable labels in Illustrator. - Check DPI: For a print‑ready map, set DPI to 300 in the Print Composer.
Conclusion
Rule-based rendering and advanced labeling unlock expressive map design possibilities in QGIS. Building a rule set once and reusing it across projects saves time and ensures visual consistency. Use expressions to make labels context‑aware and let QGIS’s placement engine do the heavy lifting. Experiment, iterate, and enjoy the rich visual storytelling that QGIS provides.
Happy mapping!
“`