A Comprehensive Guide to the Grammar of Graphics for Effective Visualization of Multi-dimensional Data
By: Dipanjan, Dj
Tags: Machine Learning, Data Visualization, Data Science, Towards Data Science, Python

We illustrate the same using a pyramid architecture to show an inherent layered hierarchy of components. Typically, to build or describe any visualization with one or more dimensions, we can use the components as follows.

Data: Always start with the data, identify the dimensions you want to visualize. Aesthetics: Confirm the axes based on the data dimensions, positions of various data points in the plot. Also check if any form of encoding is needed including size, shape, color and so on which are useful for plotting multiple data dimensions. Scale: Do we need to scale the potential values, use a specific scale to represent multiple values or a range? Geometric objects: These are popularly known as ‘geoms’. This would cover the way we would depict the data points on the visualization. Should it be points, bars, lines and so on? Statistics: Do we need to show some statistical measures in the visualization like measures of central tendency, spread, confidence intervals? Facets: Do we need to create subplots based on specific data dimensions? Coordinate system: What kind of a coordinate system should the visualization be based on — should it be cartesian or polar?

We will now be looking at how to leverage this layered framework to build effective data visualizations for multi-dimensional data with some hands-on examples.

Grammar of Graphics in Action

We will now apply the concept we learnt in the previous section on some actual data. We will be building all our visualizations in Python, but I would also recommend people to check out the ggplot2 package in R, which is one of the most inspiring frameworks till now for building nice and clean publication quality visualizations. We do have a ggplot framework in Python which has been built by Yhat, Inc. However, if you check the repository, no commits or updates have been pushed for the last two years, and unfortunately, this causes some errors especially due to backward incompatibility issues with newer versions of pandas . Hence, to emulate the true layered grammar of graphics syntax in Python, we will use another interesting framework called plotnine .

Plotnine is an open-source Python implementation for a layered grammar of graphics framework which is based on ggplot2 . Thus, using the previously specified components in this layered grammar, we can build effective visualizations.

Start with the data

We always start by loading up and looking at the dataset we want to analyze and visualize. We will use the famous mtcars dataset available as one of the pre-loaded datasets in plotnine .

The mtcars dataset consists of data that was extracted from the 1974 Motor Trend US magazine, and depicts fuel consumption and 10 other attributes of automobile design and performance for 32 automobiles (1973–74 models). The details of each attribute are depicted in the figure above. Let’s build some visualizations now.

Visualize two-dimensions (2-D)

We can now visualize data up to two dimensions using some of the components from our layered grammar of graphics framework including data, scale, aesthetics and geoms. We choose a dot or scatter plot in this case for our geometric object to represent each data point.

We can clearly see from the above visualization that mpg has a negative correlation with the cat wt .

Visualize three-dimensions (3-D)

To visualize three dimensions from our dataset, we can leverage color as one of our aesthetic components to visualize one additional dimension besides our other two dimensions as depicted in the following example.

In the above visualization, we depict the cars with different number of gears as separate categories using the color aesthetic along with our other two data dimensions (variables). It is quite clear that cars with a smaller number of gears on average tend to have higher wt and lower mpg .

Visualize four-dimensions (4-D)

To visualize four dimensions from our dataset, we can leverage color as well as size as two of our aesthetics besides other regular components including geoms, data and scale.

The visualization shows us how powerful aesthetics can be in helping us visualize multiple data dimensions in a single plot. It is quite clear that cars with higher number of cyl (cylinders) have lower number of gears and in turn, their wt is higher and mpg lower.

Alternatively, we can also use color and facets to depict data in four dimensions instead of size as depicted in the following example.

Facets are definitely one of the most powerful components towards building an effective data visualization as shown in the visualization above, where we can clearly see cars with higher cyl count have lower gear count and similar trends as the previous visualization with color and size.

Visualize data dimensions and statistics

To visualize data dimensions and some relevant statistics (like fitting a linear model), we can leverage statistics along with the other components in our layered grammar.

This enables us to see linear model trends for mpg based on wt due to the statistics component.

Visualize five-dimensions (5-D)

To visualize data in five dimensions, you know the drill by now! We will leverage the power of aesthetics including color, size and facets.

Here we use am as the facet where 0 indicates cars with automatic transmission and 1 indicates cars with manual transmission. The plot shows that cars with manual transmission have higher number of gears as compared to cars with automatic transmission. Also, majority of cars with a higher number of cylinders ( cyl ) have automatic transmission. Other insights are similar to what we have observed in the previous plots.

Visualize six-dimensions (6-D)

To visualize data in six dimensions, we can add in an additional facet on the y-axis along with a facet on the x-axis, and color and size as aesthetics.

We represent transmission ( am ) as 0 (automatic) and 1 (manual) as a facet on the y-axis and number of carburetors ( carb ) as a facet on the x-axis besides the other dimensions represented using other aesthetics similar to our previous plots. An interesting insight from the above visualization is that cars with higher number of gears have manual transmission ( am ) and higher number of carburetors ( carb ). Do you notice any other interesting insights?

Can we go higher?

The pressing question is, can we go higher than six dimensions? Well, it definitely becomes more and more difficult to hack our way around the limitations of a two-dimensional rendering device to visualize more data dimensions.

One method is to use more facets and subplots. Besides this, you can also use the notion of time if your dataset has a temporal aspect as depicted in the following example.

Hans Rosling’s famous visualization of global population, health and economic indicators

This depicts Hans Rosling’s famous visualization of depicting global population, health and various economic indicators across all countries. This was also presented in an official TED conference which I would recommend everyone to check out if they haven’t done it already!

This should give you a good perspective on how to leverage the layered grammar of graphics to visualize multi-dimensional data.

Conclusion

Like I have mentioned time and again, data visualization is an art as well as a science. This article should give you enough motivation and examples to get started with understanding and leveraging a layered grammar of graphics framework towards building effective visualizations of your own on multi-dimensional data.