The term scagnostics refers to scatter plot diagnostics, originally described by John and Paul Tukey. This is a collection of techniques for automatically extracting interesting visual features from pairs of variables. This package is an implementation of graph theoretic scagnostics developed by Wilkinson, Anand, and Grossman (2005) in pure R and is designed to be easily integrated into a tidy data workflow.
cassowaryr package provides functions to compute
scagnostics on pairs of numeric variables in a data set.
The package’s primary use is as a step in exploratory data analysis, to give users an idea of the shape of their data and identify interesting pairwise relationships.
The package can be installed from CRAN using
and from GitHub using
to install the development version.
The usage is illustrated with the package’s example data, datasauRus
dozen. This data is also available in the datasauRus package. There are
several pairs of variables that have with the same mean, variance and
correlation but strikingly different visual features. We will use a
handful of these pairwise plots to show the best way to utilise the
cassowaryr package. Here is a plot of the selected
datasauRus dozen plots:
library(cassowaryr) library(ggplot2) library(dplyr) #> #> Attaching package: 'dplyr' #> The following objects are masked from 'package:stats': #> #> filter, lag #> The following objects are masked from 'package:base': #> #> intersect, setdiff, setequal, union # pick examples <- datasaurus_dozen %>% exampledata filter(dataset %in% c("slant_up", "circle", "dots", "away")) #plot them %>% exampledata ggplot(aes(x=x,y=y, colour=dataset))+ geom_point() + facet_wrap("dataset") + theme_minimal() + theme(legend.position = "none", aspect.ratio=1)
From a data frame, there are several ways to calculate scagnostics.
If we simply have two variables we wish to calculate several scagnostics
on, we use the
calc_scags function and pass through the two
variables as vectors.
calc_scags(exampledata$x, exampledata$y, scags=c("clumpy2", "convex", "striated2")) %>% ::kable(digits=4, align="c") knitr
If instead we have a data frame with two variables and a grouping
variable (a long form of a data set) then we can use the
calc_scags function to get the scagnostics for each
<- exampledata %>% longscags group_by(dataset) %>% summarise(calc_scags(x, y, scags=c("clumpy2", "convex", "striated2", "dcor"))) %>% longscags ::kable(digits=4, align="c") knitr
Finally, if we have a wide data set consisting of only numerical
variables, we can use the
calc_scags_wide to find the
scagnostics on every pairwise combination of variables.
<- datasaurus_dozen_wide[,c(1:2,5:6,9:10,17:18)] exampledata_wide <- calc_scags_wide(exampledata_wide, scags=c("clumpy2", "convex", "striated2", "dcor")) widescagshead(widescags, 4) %>% ::kable(digits=4, align="c") knitr
If the resulting scagnostic data set is small enough, we can find interesting scatter plots by simply looking at the table, however this is often not the case. If we want to find pairwise plots that are different to the others, we can find outliers on combinations of the scagnostics using an interactive scatter plot matrix (SPLOM). The code (but not the output) on how to do this is shown below:
library(GGally) library(plotly) <- widescags %>% splom_data mutate(lab = paste0(Var1, " , ", Var2)) %>% select(-c(Var1, Var2)) <- ggpairs(splom_data, columns=c(1:4), aes(label=lab)) + p theme_minimal() ggplotly(p)
There are several functions that can summarise the scagnostics
top_pairs allows us to find the top
scagnostic for each pair of variables, while
finds the top pair of variables for each scagnostic. Their usage is
similar and looks like:
top_scags(widescags) %>% ::kable(digits=4, align="c") knitr
Occasionally we will get unexpected results for a scagnostic. To
diagnose a scagnostic result, the package has several draw functions
that will plot the graph based objects that are used to construct the
draw_mst(). Below shows the MST drawn for the
dots pair of variables in the
datasaurus_dozen, and it can be seen to have some
difficulty, as would be expected, defining the MST when all points are
<- exampledata %>% drawexample filter(dataset== "dots") draw_mst(drawexample$x, drawexample$y, alpha=0.2) + theme_minimal()
Tukey, J. W. and Tukey, P. A. (1985). “Computer graphics and exploratory data analysis: An introduction”, In Proceedings of the Sixth Annual Conference and Exposition: Computer Graphics’85, 3:773-785. National Computer Graphics Association, Fairfax, VA.
Wilkinson, L., Anand, A. and Grossman, R. (2005) “Graph-theoretic scagnostics”, IEEE Symposium on Information Visualization, 2005. INFOVIS 2005., pp. 157-164, doi: 10.1109/INFVIS.2005.1532142.