knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>"
)

This is a small example to illustrate the use of visualizations provided in ggradialbar. The visualizations provided in ggradialbar are meant for visualizing high-dimensional clusters and their evolution.

Importing the required packages
# pkgs <- c("tidyverse", "DataExplorer", "tidymodels", "MASS", "corrplot")
# install.packages(pkgs)
library(tidyverse)
library(DataExplorer)
library(tidymodels)
library(MASS)
library(corrplot)

# install.packages("ggradialbar")
# library(ggradialbar)
Getting started with clustering

The Boston dataset contains Housing Values in suburbs of Boston. The dataframe has 506 rows and 14 columns. You can find the description of the features in the following link: https://www.rdocumentation.org/packages/MASS/versions/7.3-53.1/topics/Boston

data <- Boston  # from MASS package

str(data)

summary(data)
Scaling the data

The range of features varies so normalize variables using z-score normalization.

scaled_dataset <- scale(data, center = TRUE, scale = TRUE)

scaled_dataset <-  as_tibble(scaled_dataset)

head(scaled_dataset)

Using k-means for clustering

kclust <- kmeans(scaled_dataset, centers = 3)


summary(kclust)

Extracting the cluster assignment variable

data <- augment(kclust, scaled_dataset)  # augment adds the point classifications to the original data set. 

head(data)

Pivot data from wide to long

data <- data %>%
  pivot_longer(names_to = "x", values_to = "y", cols = -c(.cluster))

Adding a group names variable to data

Assigning feature names to different groups based on their similar characteristics.

NOTE: This variable is added to be used for the group_names parameter in ggradialbar geoms.

For this example: we are assigning random names to groups.

data <- data %>% mutate(.gr_name =
                    case_when(x  == "crim" | x == "zn" ~ "random", 
                               x == "indus" | x == "chas" ~ "group",
                               x == "nox" | x == "rm" ~ "names",
                               x == "age" | x == "dis" ~ "show",
                               x == "rad" | x == "tax" ~ "visualize",
                               x == "ptratio" | x == "black" ~ "groups",
                               x == "lstat" | x == "medv" ~ "features") 
                ) %>%
   arrange(.cluster)

head(data)

Visualizing cluster evolution

To illustrate how to visualize cluster evolution, we are using the data attached with the ggradialrbar package.

#head(tidy_data)



Ashish-Soni08/ggradialbar documentation built on April 15, 2021, 4:11 a.m.