是否可以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))
我不确定这是否可以在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)
是的,因为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)