god manipulates tables. It does not draw them, and it is not going to, because drawing is a different job with a different vocabulary. A grammar that tried to do both would stop being small enough to hold in your head, which is the one thing this project will not trade.
That other grammar exists. gog is this project’s sibling: a grammar of graphics, by the same author, built on the same discipline and holding the same promise about day one and day two. Most real questions use both, because a question usually starts as a table that is the wrong shape and ends as a picture.
This chapter is one question answered end to end. Nothing else is loaded.
29.1 The table
Gapminder’s country panel: 142 countries, twelve observations each from 1952 to 2007, 1,704 rows in all. It is a real table rather than a demonstration, which is the point of using it here.
Five rows of 1,704. Every table on this page is shown that way, because a chapter that prints 1,704 rows is not showing you anything.
29.2 The question
Which countries gained the most life expectancy, and what did the world look like while it happened?
The first half is the table’s job. Each country has twelve rows and the answer needs two of them, the earliest and the latest, which is what first and last are for. They answer by position, so a sort in front of them is what decides which position that is.
Oman gained more than thirty years of life expectancy in half a century. That is one sentence of god: sort, summarize by two columns, subtract, sort again.
The other end of the same table is a different question and the same pipeline with one word changed.
Nothing above called collect, and the tables still appeared. That is worth explaining, because it is the question everyone asks once.
A pipeline is a plan, and it does not run when you write it. Printing one runs it. That is why every example so far has shown a table: this book prints each pipeline, so each one is asked for its answer.
collect is how you ask for the answer when something other than printing needs it. Handing a table to another function is exactly that case, and gog is about to be handed one.
The bar chart answers the question. This next one is why the pairing is worth having: the same panel, unsummarized, with one more channel.
play turns a column into time. Every country is a point, income runs along the bottom on a log scale, life expectancy up the side, and the year becomes the animation rather than another axis.
That is the Rosling chart, and neither package needed a special case to draw it. play is a channel like any other, so the year binds the way a color does.
29.6 What it replaced
Written in the tools this book keeps comparing itself against, those two steps need a reshaping function from one package and a plotting system from another, each with its own vocabulary, its own idea of what a column reference is, and its own rules about which arguments are quoted. The animation usually needs a third.
The saving is not the line count. It is that summarize and play come from the same design, so the second half of the analysis does not ask you to change how you are thinking. by means in gog what it means here. So does color.
29.7 The one thing to know about using both
In R, load them and go. R looks a column name up in your data before it looks anywhere else, and the two packages take care not to collide.
In Python, one name is claimed by both. col is how each of them names a column, so from god import * followed by from gog import * would leave you with one of them and no warning. Import gog by name instead, which is what the examples above do:
from god import*import gog
Then col.life is god’s and gog.col.life is gog’s, and nothing is ambiguous. Appendix A says the same thing about every other name god takes over.
29.8 Where to go next
The graphics grammar has its own manual, at the gog book, written the way this one is: every plot on the page was drawn by the engine while the page was built.
Read it in the order you would use them. You start in god, with a table that is the wrong shape. You end in gog, looking at the answer.
The table on this page is the Gapminder panel, by way of the gapminder R package. It is public domain, and credited anyway: the package is Jennifer Bryan’s, and the figures are the Gapminder Foundation’s.