微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

如何解决任何人都可以帮助用圆和日食制作 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

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。