GPT答: |
7i24.cn
|
聚类图:
```R
# 载入数据集iris
data(iris)
# 提取数据集中的前四列作为特征变量
x <- iris[,1:4]
# 使用kmeans算法进行聚类,将数据分为3个簇
kmeans_result <- kmeans(x, centers = 3)
# 绘制聚类图,使用ggplot2包进行绘制
library(ggplot2)
ggplot(data = iris, aes(x = Petal.Length, y = Petal.Width)) +
geom_point(aes(color=factor(kmeans_result$cluster))) +
ggtitle("Iris Clustering") +
xlab("Petal Length") + ylab("Petal Width")
```
热图:
```R
# 载入数据集iris并提取前四列作为特征变量x和花的种类y(即标签)
data(iris)
x <- iris[,1:4]
y <- iris$Species
# 使用pheatmap包绘制热图,需要先安装pheatmap包:install.packages('pheatmap')
library(pheatmap)
pheatmap(x, scale="row", annotation_col=y,
cluster_rows=T, cluster_cols=T,
color=colorRampPalette(c("#7F0000","red","#FF7F00",
"yellow","#007F00","cyan",
"#00007F","blue","#7F007F"))(50))
```
注意:在运行代码之前需要先安装相关的包。
|