Cluster graph on USArrest dataset
We use the “USArrests” dataset, which contains crime statistics for different U.S. states. We’ll perform k-means clustering with k=3 clusters and visualize the results using the “factoextra” and “factoMineR” libraries.
Install and load the required libraries. If not already installed, run:
install.packages("factoextra")
install.packages("factoMineR")
Load the package and dataset
R
# Load the required libraries library (factoextra) library (factoMineR) # Load the "USArrests" dataset data ( "USArrests" ) |
Explore the dataset
We use functions like head(USArrests) to check the structure of the dataset.
R
head (USArrests) |
Output:
Murder Assault UrbanPop Rape
Alabama 13.2 236 58 21.2
Alaska 10.0 263 48 44.5
Arizona 8.1 294 80 31.0
Arkansas 8.8 190 50 19.5
California 9.0 276 91 40.6
Colorado 7.9 204 78 38.7
Perform K-Means Clustering
R
# Perform k-means clustering (e.g., k=3 clusters) kmeans_model <- kmeans (USArrests, centers = 3) # Visualize the k-means clusters fviz_cluster (kmeans_model, data = USArrests, geom = "point" ) |
Output:
We perform k-means clustering on the “USArrests” dataset. Specifically, you are creating a k-means model with three clusters (centers = 3). The result, kmeans_model, will contain information about the clusters, including cluster assignments for each data point and the cluster centers.
- fviz_cluster: This function is used to create cluster visualizations. It takes the k-means model kmeans_model as input.
- data = USArrests: This specifies the dataset you are working with, which is “USArrests” in this case.
- geom = “point”: This specifies that you want to represent the data points as points in the plot.
The resulting plot will show the data points colored according to their assigned clusters, and it will also display the cluster centers. This visualization allows you to see how the data has been segmented into clusters by the k-means algorithm.
Cluster Graph in R
R’s cluster graph functionality can be a useful tool for visualizing data and seeing patterns within it. In disciplines including biology, the social sciences, and data analysis, cluster graphs are frequently used to group together related data points. In this article, we’ll demonstrate how to display a cluster graph in R by combining the ggplot2 package for data analysis and visualization with the ggraph tool for graph visualization.