R中带省略号的PCA(带省略号)

时间:2019-03-19 17:08:17

标签: r colors pca

我正在尝试制作PCA图以进行发布。这意味着没有颜色。但是,当您告诉它对数据类别进行分组时,我尝试过的所有程序包都会为图表着色。

我已经尝试过以下软件包:ggbiplot,ggfortify和factoextra。不幸的是,没有成功。

关于我尝试过的代码(来自https://xlinux.nist.gov/dads/HTML/patriciatree.html):

- name: Include host specific variables
  include_vars: "{{ ansible_hostname }}.yml"

- name: Include host specific secret variables
  include_vars: "{{ ansible_hostname }}_secret.yml"

并返回here

但是我需要的是:This image

希望我很清楚。

2 个答案:

答案 0 :(得分:2)

@SantiagoCapobianco的回答包含所有内容,但并未将它们放在一起。

在汽车套件中始终使用dataEllipse并带有正确的参数。

library(car)

mtcars.country <- factor(mtcars.country)

dataEllipse(mtcars.pca$x[,1], mtcars.pca$x[,2], mtcars.country, 
    levels=0.8, xlim=c(-5,5), ylim=c(-4,4), center.pch=0,
    col=rep("black", 3), pch=15:17)
legend("topleft", legend=levels(mtcars.country), pch=15:17, bty='n')

Data Ellipses

答案 1 :(得分:1)

这可能是答案的一半,因为它缺少省略号:

data("mtcars")

mtcars.pca <- prcomp(mtcars[, c(1:7, 10, 11)], center = TRUE, scale = TRUE)
mtcars$country <- as.factor(c(rep("Japan", 3), rep("US",4), rep("Europe", 7),rep("US",3), "Europe", rep("Japan", 3), rep("US",4), rep("Europe", 3), "US", rep("Europe", 3)))

plot(mtcars.pca$x, pch = as.numeric(mtcars$country))
legend("topright", legend = c("Japan", "US", "Europe"), pch = 1:3)

结果: enter image description here

要绘制椭圆,可以使用car包装:

library(car)
dataEllipse(mtcars.pca$x[, 1:2], groups = mtcars$country, levels = 0.98, add = TRUE)

enter image description here

对此问题的信用: Drawing ellipse in R