是否可以在R中的ggbiplot
包中标记某些特殊数据点,例如数据点,其中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))
答案 0 :(得分: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)
答案 1 :(得分:0)
是的,因为ggbiplot
是有效的ggplot2
对象,您可以像在其他ggplots上一样添加geom
。
cond <- wine.pca$x[which(wine$Alcohol == 13.2),]
cond[1:2]
返回:
PC1 PC2
-2.2032498 -0.3324551
这是代码。请注意,我们使用geom_point
并分别指向PC1和PC2的x轴和y轴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)