1

是否可以ggbiplot在 R 的包中用另一种颜色标记一些特殊的数据点,如数据点,Alcohol = 13.2然后在集群中。我想强调一些特定的数据点。

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

2 回答 2

0

我不确定这是否可以在ggbiplot调用中完成,但肯定可以通过添加另一层来完成。这是一个例子:

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)

现在子集您要突出显示的点:

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

并提供data以下内容geom_point

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

在此处输入图像描述

你也可以这样做:

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)

在此处输入图像描述

但是,如果您想强调很多点,这可能太多了:

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

在此处输入图像描述

于 2018-05-05T14:26:22.833 回答
0

是的,因为ggbiplot它是一个有效ggplot2对象,您可以像添加geom其他 ggplots 一样添加它。

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

回报:

       PC1        PC2 
-2.2032498 -0.3324551 

这是代码。请注意,对于 x 和 y 轴,我们分别使用geom_point和指向 PC1 和 PC2,其中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) 

在此处输入图像描述

于 2018-05-05T14:27:42.587 回答