concept ggpairs() in category R

appears as: ggpairs()
Machine Learning with R, the tidyverse, and mlr

This is an excerpt from Manning's book Machine Learning with R, the tidyverse, and mlr.

In situations where I have a clear outcome variable, I often plot each of my predictor variables against the outcome (as we’ve done in previous chapters). In unsupervised learning situations, we don’t have an outcome variable, so I prefer to plot all variables against each other (provided I don’t have so many variables as to prohibit doing so). We can do this easily using the ggpairs() function from the GGally package, which you may need to install first. We pass our tibble as the first argument to the ggpairs() function, and then we supply any additional aesthetic mappings by passing ggplot2’s aes() function to the mapping argument. Finally, we add a theme_bw() layer to add the black-and-white theme.

Listing 13.2. Plotting the data with ggpairs()
install.packages("GGally")

library(GGally)
ggpairs(swissTib, mapping = aes(col = Status)) +
  theme_bw()

The resulting plot is shown in figure 13.7. The output from ggpairs() takes a little getting used to, but it draws a different kind of plot for each combination of variable types. For example, along the top row of facets are box plots showing the distribution of each continuous variable against the categorical variable. We get the same thing in histogram form down the left column of facets. The diagonal facets show the distributions of values for each variable, ignoring all others. Finally, dot plots shown the bivariate relationships between pairs of continuous variables.

Figure 13.7. The result of calling the ggpairs() function on our banknote dataset. Each variable is plotted against every other variable, with different plot types drawn depending on the combination of variable types.

Let’s plot the data using ggpairs() to remind ourselves of the structure of the data.

Figure 18.8. Plotting the Swiss banknote dataset with ggpairs(). 2D density plots are shown above the diagonal.

Next, let’s use ggpairs() to plot the final clustering. Because we calculated the internal cluster metrics, not considering the noise cluster, we’ll plot the result with and without noise cases. This will allow us to visually confirm whether the assignment of cases as noise is sensible.

Listing 18.15. Plotting the final clustering with outliers
filter(swissClustersGathered, Permutation == "V73") %>%
  select(-Permutation) %>%
  mutate(Cluster = as.factor(Cluster)) %>%
  ggpairs(mapping = aes(col = Cluster),
          upper = list(continuous = "density")) +
  theme_bw()

We first filter our swissClustersGathered tibble to include only rows belonging to permutation 73 (these are the cases clustered using our chosen combination of epsilon and minPts). Next, we remove the column indicating the permutation number and convert the column of cluster membership into a factor. We then use the ggpairs() function to create the plot, mapping cluster membership to the color aesthetic.

Figure 18.12. Plotting our final DBSCAN cluster model with ggpairs(). This plot includes the noise cluster.