R中的聚类散点图

时间:2018-10-18 14:46:07

标签: r

我是R的新手,我进行了搜索,但仅找到过时的信息。 我已经完成了一个简单的单链接聚类过程。

d<-dist(scale(DATA),method="euclidean",diag=TRUE,upper=TRUE)
hls<-hclust(d,method="complete")

如何绘制散点图,每个散点图使用一种颜色?

就像这个例子一样

enter image description here

1 个答案:

答案 0 :(得分:0)

我创建了一些示例数据来使用。如果您的数据看起来不同,请提供一些示例数据作为问题的一部分。

要创建按组着色的散点图,请首先使用美眉功能创建组。您可以指定一个整数值来指示要如何创建组。

接下来使用您最喜欢的图形包(例如ggplot)创建散点图。

# Sample data
rData <- data.frame(x=c(1,1,3,4), y=c(1,2,5,4))
print(rData)

# Cluster
d <- dist(scale(rData), method="euclidean", diag=TRUE, upper=TRUE)
hls <- hclust(d, method="complete")

# Create groups
cluster <- cutree(hls, 2)

# Create scatter plot
ggData <- cbind(rData, cluster)
ggData$cluster <- as.factor(ggData$cluster)
print(ggData)
ggplot(ggData, aes(x=x, y=y, color=cluster)) + geom_point(size=5)

我建议您探索http://www.cookbook-r.com/Graphs/,以进一步了解ggplot。