自定义图例顺序 ggbiplot,基于 ggplot2

Custom legend order ggbiplot, built off ggplot2

我使用 ggbiplot() 函数创建了一个 PCA 图,形成了 ggbiplot 包,它建立在 ggplot2 之上。这是一个类似的、可重现的例子:

library(ggbiplot)
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))

到目前为止,一切都很好。我想将图例顺序更改为 grginolino、barbera、barolo(而不是现在的 barolo、grignolino、barbera)。名称存储在环境因素变量 wine.class.

对于这样一个简单的问题,我深表歉意,但我无法从 ggplot2 的帮助中找到可以概括这种情况的直接答案。

您需要重新排序 wine.class 个关卡;看下面:

library(ggbiplot)

data(wine, package = "ggbiplot")
wine.pca <- prcomp(wine, scale. = TRUE)
wine.class.reorder <-  factor(wine.class, levels = c("grignolino", "barbera", "barolo"))

ggbiplot(wine.pca, obs.scale = 1, var.scale = 1, groups = wine.class.reorder, 
                   ellipse = TRUE, circle = TRUE)