Based on the help below I tried this script for plotting PCA with Convex hulls without success, any idea how can I solve it?
library(ggbiplot)
library(plyr)
data <-read.csv("C:/Users/AAA.csv")
my.pca <- prcomp(data[,1:9] , scale. = TRUE)
find_hull <- function(my.pca) my.pca[chull(my.pca$x[,1], my.pca$x[,2]), ]
hulls <- ddply(my.pca , "Group", find_hull)
ggbiplot(my.pca, obs.scale = 1, var.scale = 1,groups = data$Group) +
scale_color_discrete(name = '') + geom_polygon(data=hulls, alpha=.2) +
theme_bw() + theme(legend.direction = 'horizontal', legend.position = 'top')
Thanks.
The script below plot PCA with ellipses (slightly modified example from https://github.com/vqv/ggbiplot as 'opts' is deprecated)
library(ggbiplot)
data(wine)
wine.pca <- prcomp(wine, scale. = TRUE)
g <- ggbiplot(wine.pca, obs.scale = 1, var.scale = 1,
groups = wine.class, ellipse = TRUE, circle = TRUE)
g <- g + scale_color_discrete(name = '')
g <- g + theme(legend.direction = 'horizontal', legend.position = 'top')
print(g)
Removing the the ellipses is easy but I am trying to to replace them with Convex hulls without any success, any idea how to do it?
Thanks
Yes, we can design a new geom for ggplot, and then use that with ggbiplot. Here's a new geom that will do convex hulls:
And here it is in use with ggbiplot, we set
prop
to 1 to indicate that we want to draw a polygon that encloses all the points: