When to use a line chart? Showing change over time
See how La Espiga's sales changed month by month, and find out why a line joining branches says nothing.
Free course. Your Maestro, an AI, .
"Did sales go up or down?" is the director's question, and on the first page you saw it's about change over time. A line answers it. On this page you'll see what the eye reads in a line, when it's the wrong choice, and you'll fix a line that joins things that shouldn't be joined.
What a line says
A line joins points in order, from left to right, and the eye doesn't read the points: it reads the slope between them. If it rises, it grew; if it falls, it dropped; if it flattens, it stayed the same. That's why it works so well for time: months come one after another, and what happens between January and February is exactly what you want to see.
Here are the sales of all six branches together, month by month, during 2025 (synthetic data).
Read the shape before the numbers. January starts high (that's the rosca de Reyes, the Three Kings bread), February plunges and is the worst month of the year, about 3.3 million, 36% less than January. From March to September sales climb slowly, and in October and November they jump with pan de muerto, the Day of the Dead bread. That story of peaks and valleys is what a line tells better than any other chart.
When time works better as bars
Having dates doesn't force you to use a line. If the question is comparing periods ("which quarter sold the most?") and there are only a few, bars answer it better: the reader compares four lengths and is done. The line wins when there are many points in a row and what matters is the path: whether it went up, when it changed, whether there are seasons.
A line between branches isn't a change
Here's the most common trap. Someone has each branch's sales, picks "line chart" because it looks more dynamic, and gets a line that goes from Centro to Chapultepec to Providencia. That slope means nothing: no time passed between Centro and Chapultepec. The branches could go in any order, and in another order the "trend" would be different. A line always promises the reader an order and a path; if they don't exist, the promise is false.
A note on the axis: a line shows each value with the position of its point, not with a length, so it doesn't need to start at zero the way bars do. But where the axis starts changes how steep the rise looks. You'll read that carefully in module 3.
Try it
This code draws a line with each branch's sales for the year, one after another. It's exactly the trap above. The question is a different one: how did each branch's sales change during the year?
Make two changes (the columns are named in Spanish: sucursal is the branch, mes the month, ventasmxn sales in pesos): 1. In x, change "sucursal:N" to "utcyearmonth(mes):T": it groups by month, and the T tells Altair it's time (the N says they're names). 2. Add a new line inside e
Your turn
Make two changes (the columns are named in Spanish: sucursal is the branch, mes the month, ventas_mxn sales in pesos):
- In
x, change"sucursal:N"to"utcyearmonth(mes):T": it groups by month, and theTtells Altair it's time (theNsays they're names). - Add a new line inside
encode, belowy:color="sucursal:N",. That gives each branch its own line.
You're done when you see six lines from January to December, all with the January peak and the October and November one. If you get an error, check the commas at the end of each line inside encode, or paste the error to your Maestro.
The chart redraws when you pause typing, or right away with Run. Ctrl+Enter runs it. Tab indents; to leave the editor, press Escape, then Tab.
Ready to run your code.
Data: ventas-mensuales, 72 rows
Enter saves a cell and Escape leaves it as it was. Leave a cell empty for a missing value.
| Row | Remove | ||||
|---|---|---|---|---|---|
| 1 | |||||
| 2 | |||||
| 3 | |||||
| 4 | |||||
| 5 | |||||
| 6 | |||||
| 7 | |||||
| 8 | |||||
| 9 | |||||
| 10 | |||||
| 11 | |||||
| 12 | |||||
| 13 | |||||
| 14 | |||||
| 15 | |||||
| 16 | |||||
| 17 | |||||
| 18 | |||||
| 19 | |||||
| 20 |
Notice what it tells you now: all six branches follow the same season, and Zapopan stays on top all year. With one change to the x axis, the same table went from saying nothing to telling a story.
If your chart came out different or you don't get what color did, ask your Maestro.
Six lines is about the limit of what reads well together. With ten or twenty it turns into a plate of spaghetti; then it's better to show the one or two that matter and fade the rest, or make a small chart per branch.
Check yourself: A coworker shows you a line chart with the year's sales of five products: concha, bolillo, dona, cuerno and oreja, in that order. The line falls from the concha to the oreja. What do you tell them?
Have monthly data at work? Tell your Maestro what you measure and decide together whether it calls for a line.