当前位置:网站首页>K-Means Clustering Visualization in R: Step By Step Guide
K-Means Clustering Visualization in R: Step By Step Guide
2022-07-02 11:49:00 【Xiaoyu 2022】
library(ggpubr)
library(factoextra)
data("iris")
df <- iris
head(df, 3)
# Compute k-means with k = 3
set.seed(123)
res.km <- kmeans(scale(df[, -5]), 3, nstart = 25)
# K-means clusters showing the group of each individuals
res.km$cluster
fviz_cluster(res.km, data = df[, -5],
palette = c("#2E9FDF", "#00AFBB", "#E7B800"),
geom = "point",
ellipse.type = "convex",
ggtheme = theme_bw()
)

library(ggpubr)
library(factoextra)
data("iris")
df <- iris
head(df, 3)
# Compute k-means with k = 3
set.seed(123)
# Dimension reduction using PCA
res.pca <- prcomp(df[, -5], scale = TRUE)
# Coordinates of individuals
ind.coord <- as.data.frame(get_pca_ind(res.pca)$coord)
# Add clusters obtained using the K-means algorithm
ind.coord$cluster <- factor(res.km$cluster)
# Add Species groups from the original data sett
ind.coord$Species <- df$Species
# Data inspection
head(ind.coord)
# Percentage of variance explained by dimensions
eigenvalue <- round(get_eigenvalue(res.pca), 1)
variance.percent <- eigenvalue$variance.percent
head(eigenvalue)
ggscatter(
ind.coord, x = "Dim.1", y = "Dim.2",
color = "cluster", palette = "npg", ellipse = TRUE, ellipse.type = "convex",
shape = "Species", size = 1.5, legend = "right", ggtheme = theme_bw(),
xlab = paste0("Dim 1 (", variance.percent[1], "% )" ),
ylab = paste0("Dim 2 (", variance.percent[2], "% )" )
) +
stat_mean(aes(color = cluster), size = 4)
边栏推荐
- HOW TO ADD P-VALUES ONTO A GROUPED GGPLOT USING THE GGPUBR R PACKAGE
- R HISTOGRAM EXAMPLE QUICK REFERENCE
- Esp32 audio frame esp-adf add key peripheral process code tracking
- 行業的分析
- Attribute acquisition method and operation notes of C # multidimensional array
- A white hole formed by antineutrons produced by particle accelerators
- Programmer growth Chapter 6: how to choose a company?
- to_ Bytes and from_ Bytes simple example
- Research on and off the Oracle chain
- GGHIGHLIGHT: EASY WAY TO HIGHLIGHT A GGPLOT IN R
猜你喜欢

念念不忘,必有回响 | 悬镜诚邀您参与OpenSCA用户有奖调研

How to Create a Beautiful Plots in R with Summary Statistics Labels

What is the relationship between digital transformation of manufacturing industry and lean production

数字化转型挂帅复产复工,线上线下全融合重建商业逻辑

6方面带你认识LED软膜屏 LED软膜屏尺寸|价格|安装|应用

Tdsql | difficult employment? Tencent cloud database micro authentication to help you

A sharp tool for exposing data inconsistencies -- a real-time verification system

HOW TO ADD P-VALUES TO GGPLOT FACETS

Redis exceeds the maximum memory error oom command not allowed when used memory & gt; ' maxmemory'

HOW TO CREATE A BEAUTIFUL INTERACTIVE HEATMAP IN R
随机推荐
Attribute acquisition method and operation notes of C # multidimensional array
map集合赋值到数据库
MySQL stored procedure cursor traversal result set
2022年4月17日五心红娘团队收获双份喜报
Data analysis - Matplotlib sample code
Amazon cloud technology community builder application window opens
A sharp tool for exposing data inconsistencies -- a real-time verification system
[multithreading] the main thread waits for the sub thread to finish executing, and records the way to execute and obtain the execution result (with annotated code and no pit)
GGPlot Examples Best Reference
On April 17, 2022, the five heart matchmaker team received double good news
Power Spectral Density Estimates Using FFT---MATLAB
微信小程序利用百度api达成植物识别
C#多维数组的属性获取方法及操作注意
数据分析 - matplotlib示例代码
Mmrotate rotation target detection framework usage record
多文件程序X32dbg动态调试
bedtools使用教程
PX4 Position_ Control RC_ Remoter import
行业的分析
How to Easily Create Barplots with Error Bars in R
