【问题标题】:can any one help on making pca biplot with circle and eclipse?任何人都可以帮助制作带有圆圈和日食的 pca biplot 吗?
【发布时间】:2021-05-14 06:07:41
【问题描述】:

示例数据集:

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, 7L, 249L, 54L, 38L), St..Martin.s.Island = c(29L, 213L, 37L, 45L, 578L, 147L, 30L, 32L, 0L, 48L, 107L, 18L, 48L, 97L, 54L), Kuakata = c(21L, 54L, 0L, 41L, 276L, 87L, 13L, 8L, 0L, 37L, 0L, 7L, 7L, 41L, 12L), Kotka = c(16L, 37L, 3L, 0L, 47L, 19L, 0L, 0L, 0L, 0L, 3L, 0L, 5L, 0L, 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)

【问题讨论】:

    标签: r pca ggbiplot


    【解决方案1】:

    我怀疑问题在于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", "recyclable", "recyclable",
                    "recyclable", "not recyclable", "not applicable",
                    "not applicable", "not applicable", "not recyclable",
                    "recyclable", "recyclable", "recyclable", "recyclable",
                    "not recyclable"),
                    levels = c("recyclable", "not 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')
    

    【讨论】:

      猜你喜欢
      • 2012-10-28
      • 1970-01-01
      • 1970-01-01
      • 2020-12-01
      • 1970-01-01
      • 1970-01-01
      • 2019-10-12
      • 2021-12-13
      • 1970-01-01
      相关资源
      最近更新 更多