joelostblom / viz-oer

An interactive open educational resource for learning data visualization
https://joelostblom.github.io/viz-oer/
1 stars 0 forks source link

Draw paralell from viz grammar to grammars in programming languages to clarify the concept #11

Closed joelostblom closed 2 months ago

joelostblom commented 3 months ago

For ch1. Something like this, but maybe shorter:

We are familiar with grammatical rules for natural languages, where they tell us how to combine words into sentences to convey particular meanings. Grammatical rules exist for all programming languages as well, these are the rules of how you compose For example, if you write 1 + 2, the returned answer will be 3. You might argue that is just a mathematical rule and not a programming language grammar, but it is the grammar that says that the programming language should rely on the mathematical concept of addition when the + operator is used. Compare to the example 'one' + 'two'; here there is no underlying mathematical principal of what should happen when two strings are added together using the + operator, but we can still perform this operation and the grammar of the ... Different programming languages have different grammar, in the previous example the grammar of Python defines that the addition of string should perform concatenation so the returned result is 'onetwo'; the grammar of R says that there is no such thing as addition of strings, so it throws an error complaining that non-numeric arguments were passed to the + operator.

Likewise for a visualization grammar, different packages make different choices for what happens when different visual elements are added together, and which those visual elements are. The core grammar of most visualization libraries contain elements representing:

  • the main chart/canvas area to be drawn on
  • how the data will be mapped on the canvas (e.g. variable A on the x-axis, variable B as the color, etc)
  • the different geometrical marks that will represent the data points (points, lines, etc)

The grammar then also declares what should happen when these elements are combined in different elements, e.g. if I say points + line, what should happen? Should the line be put above the points? Or maybe they should be put next to each other?

To help further understand what a visualization grammar is, it can be helpful to contrast it to visualizations made using a non-grammatical approach. Such libraries often have dedicated functions for each visualization, e.g. barplot(), scatterplot(), etc, and there is no notion of composing small graphical units into larger visualizations, just one function per . This is not to say that this approach isn't effective, but it can lead to more memorization and doesn't allow you to combine [ref krutchen paper] In our personal opinion, it is also more fun and rewarding when we can be creative and try to compose something because we deduce that it should work that way, and then have it actually work! (compared to memorizing a function)

rorywhite200 commented 3 months ago

This is great. I tried to incorporate an abridged version of your explanation in the introduction to grammars. I also added a table to compare the components of language, programming and graphical grammars - but this may need some refinement to get real equivalencies.