1

I'm new to R, I searched but I find outdate info only. I've done a simple single linkage clustering process.

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

How can I plot a scatterplot which uses a color each cluster?

Exactly like this example

enter image description here

alfredopacino
  • 2,979
  • 9
  • 42
  • 68
  • 3
    Maybe [this](https://stackoverflow.com/questions/11462901/cluster-presentation-dendrogram-alternative-in-r) will help: uses `ggplot2` to plot scatter plot from `hclust`. – pogibas Oct 18 '18 at 14:50

1 Answers1

0

I created some sample data to work with. If your data looks different, please provide some sample data as part of your question.

To create a scatter plot colored by group, first create your groups using the cutree function. You can specify an integer value to indicate how may groups you want to create.

Next use your favorite graphing package (e.g. ggplot) to create the scatter plot.

# 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)

I would recommend exploring http://www.cookbook-r.com/Graphs/ to learn more about ggplot.

MatAff
  • 1,250
  • 14
  • 26