1
votes

Is it possible in ggbiplot package in R to mark some special data points like data point with Alcohol = 13.2 in another color then in the clusters. I want to highlight some specific data points.

data(wine)
wine.pca <- prcomp(wine, scale. = TRUE)
print(ggbiplot(wine.pca, obs.scale = 1, var.scale = 1, groups = wine.class, 
   ellipse = TRUE, circle = TRUE))
2

2 Answers

0
votes

Yes, because ggbiplot is a valid ggplot2 object, you can add geom on it just as you would with other ggplots.

cond <- wine.pca$x[which(wine$Alcohol == 13.2),]
cond[1:2]

Returns:

       PC1        PC2 
-2.2032498 -0.3324551 

Here's the code. Notice we use geom_point and point to PC1 and PC2 respectively for the x and y axes where wine$Alcohol==13.2:

library(ggbiplot)
data(wine) 
wine.pca <- prcomp(wine, scale. = TRUE)
ggbiplot(wine.pca, obs.scale = 1, var.scale = 1, groups = wine.class, ellipse = TRUE, circle = TRUE) + 
geom_point(aes(x=cond[1], y=cond[2]), col="goldenrod2", size=3) 

enter image description here

0
votes

I am not sure if this can be done within the ggbiplot call, but it is certainly possible by adding another layer. Here is an example:

library(ggbiplot)

data(wine)
wine.pca <- prcomp(wine, scale. = TRUE)
p <- ggbiplot(wine.pca, obs.scale = 1, var.scale = 1, groups = wine.class, 
               ellipse = TRUE, circle = TRUE)

now subset the points you would like to highlight:

wine.pca$x[wine$Alcohol == 13.2, 1:2, drop = FALSE] #drop = FALSE to keep matrix class

and provide that as data to geom_point:

p + geom_point(data = wine.pca$x[wine$Alcohol == 13.2, 1:2, drop = FALSE],
              aes(x = PC1, y = PC2), color = "black")

enter image description here

you can also do something like this:

p + geom_point(data = wine.pca$x[wine$Alcohol == 13.2, 1:2, drop = FALSE],
               aes(x = PC1, y = PC2), color = "black", shape = 21, size = 7)

enter image description here

however this might be too much if you want to highlight many points:

p + geom_point(data = wine.pca$x[wine$Alcohol >= 13.2, 1:2],
               aes(x = PC1, y = PC2), color = "black", shape = 21, size = 7)

enter image description here