Convex hull in UMAP plot - R

Viewed 171

I performed a UMAP on the wine dataset and added a convex hull using the ggpubr stat_chull option. It seems that the convex hull is not closed, how can I fix it? any other package I can use?

library(ggplot2)
library(ggpubr)
library(Rtsne)
library(umap)

UCI <- "ftp://ftp.ics.uci.edu/pub"
REPOS <- "machine-learning-databases"
w.url <- sprintf("%s/%s/wine/wine.data", UCI, REPOS)
w <- read.csv(w.url, header=F) 
colnames(w) <- c('Type', 'Alcohol', 'Malic', 'Ash', 
                    'Alcalinity', 'Magnesium', 'Phenols', 
                    'Flavanoids', 'Nonflavanoids',
                    'Proanthocyanins', 'Color', 'Hue', 
                    'Dilution', 'Proline')
w$Type <- as.factor(w$Type)


####For UMAP 
w.umap = umap(w[,2:14])
w.labels = w$Type

head(w.umap$layout, 3)

df <- data.frame(x = w.umap$layout[,1],
                 y = w.umap$layout[,2],
                 wType = w.labels)
ggplot(df, aes(x, y, colour = wType)) + geom_point(size=3) + ggtitle("UMAP") + stat_chull(aes(color = w.labels))
1 Answers

The default geom for stat_chull is a line rather than a polygon. If you change to a polygon, it will automatically join the ends:

ggplot(df, aes(x, y, colour = wType)) + geom_point(size=3) + 
  ggtitle("UMAP") + 
  stat_chull(aes(color = w.labels, fill = wType), geom = "polygon", alpha = 0.1)

enter image description here

Here I have filled the polygons faintly for emphasis, but if you don't want this you can set alpha to 0:

ggplot(df, aes(x, y, colour = wType)) + geom_point(size=3) + 
  ggtitle("UMAP") + 
  stat_chull(aes(color = w.labels, fill = wType), geom = "polygon", alpha = 0, size = 2)

enter image description here

Related