任何人都可以帮助用圆和日食制作 pca biplot 吗?

问题描述

示例数据集:

structure(list(Litter = c("Bottle caps & lids","Bottles < 2 L","Drink package rings,six-pack rings,ring carriers","Food containers (fast food,cups,lunch boxes etc.)","Plastic bags (opaque & clear)","Cigarettes,butts & filters","Rope","Fishing net","Foam sponge","Foam (insulation & packaging)","Clothing,shoes,hats & towels","Bottles & jars","Paper (including newspapers & magazines)","Cups,food trays,food wrappers,cigarette packs etc.","Footwear (flip-flops)"),Cox.s.Bazar = c(229L,228L,73L,120L,1311L,442L,208L,125L,225L,207L,29L,60L,74L,96L,111L),Chittagong = c(13L,72L,1L,7L,871L,28L,59L,22L,0L,382L,70L,249L,54L,38L),St..Martin.s.Island = c(29L,213L,37L,45L,578L,147L,30L,32L,48L,107L,18L,97L,54L),Kuakata = c(21L,41L,276L,87L,13L,8L,12L),Kotka = c(16L,3L,47L,19L,5L,0L)),class = "data.frame",row.names = c(NA,-15L))

我解决问题的尝试:

install_github("vqv/ggbiplot")
library(devtools)
library(ggbiplot)

data.class <- Mydata[,1]
data.pca <- prcomp(Mydata[,2:6],scale. = TRUE)

g <- ggbiplot(data.pca,obs.scale = 1,var.scale = 1,groups = data.class,ellipse = TRUE,circle = TRUE)
g <- g + scale_color_discrete(name = '')
g <- g + theme(legend.direction = 'horizontal',legend.position = 'top')
print(g)

解决方法

我怀疑问题在于 https://github.com/vqv/ggbiplot(葡萄酒数据集)上显示的示例具有您的数据没有的“.class”变量。一种解决方案是自己添加它,例如

library(tidyverse)
library(devtools)
install_github("vqv/ggbiplot")
library(ggbiplot)

data <- MyData
data.pca <- prcomp(data[-1],scale. = TRUE)
data.class <- factor(x = c("recyclable","recyclable","not recyclable","not applicable","not recyclable"),levels = c("recyclable","not applicable"))

ggbiplot(data.pca,obs.scale = 1,var.scale = 1,groups = data.class,ellipse = TRUE,circle = TRUE) +
  scale_color_discrete(name = '') +
  theme(legend.direction = 'horizontal',legend.position = 'top')

example_1.png

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...