Get startedGet started for free

Adding your own cognostics

1. Adding your own cognostics

The automatic cognostics that we have seen have given us some useful ways to interact with our display, but there are often cases where you have a metric in mind that you think will add useful interactivity to your display. TrelliscopeJS allows you to add these easily.

2. New variables as cognostics

As you might have noticed with the original TrelliscopeJS display that we created in this chapter, cognostics were automatically added for the variables that were in the data. When using TrelliscopeJS with ggplot2, we can add new cognostics by simply adding new variables to our data. If the variable varies within each panel group, a set of summary statistics is computed. If the variable is constant within each panel group, a single cognostic is created for that variable.

3. Latest life expectancy as a cognostic

Suppose we want to be able to interact with the gapminder visualizations based on the latest observed life expectancy for each country. To do this, we can use dplyr's group_by() and mutate() functions to compute the new variable. Note that even though we are grouping, we do not summarise, as we wish to preserve the original data. The group_by() causes the operation to be performed independently within each country, which will ensure that this new variable will show up as a single cognostic when we feed this data into a ggplot trelliscope display. It is a bit inefficient in terms of the size of the data to repeat a cognostic value within each group. In the next chapter, we will learn about another way to create TrelliscopeJS displays that allows more plotting systems to be used and provides a more natural way to specify custom cognostics.

4. Hyperlinks as cognostics

There is a special cognostic that provides a hyperlink to another web page with contextual information about the subset of data being visualized. For example, suppose we wish to have a link available to the Wikipedia article about the country being plotted. We can add a new variable to our dataset that contains this URL using dplyr's group_by and mutate as before. Trelliscope will detect this as a link and display it in a special way as a label such that you can easily visit the link from within the display.

5. Customizing custom cognostics

We can customize aspects of how a cognostic behaves by default in the viewer, such as its description and whether it shows up by default as a label. We can do this by wrapping the variable in a function called cog(), which notes the settings and passes them to the viewer. The most common attributes you can specify are a text description of the cognostic and an indicator for whether the cognostic should show up as a label by default or not. For example, here, we modify our wiki variable to give it a more meaningful description and to specify that it should show up as a panel label by default.

6. Let's practice!

Let's try adding some custom cognostics.