can any one help on making pca biplot with circle and eclipse?

310 Views Asked by At

Example dataset:

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))

My attempt to solve the problem:

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)
1

There are 1 best solutions below

0
On

I suspect the problem is that the example shown on https://github.com/vqv/ggbiplot (the wine dataset) has a ".class" variable that your data doesn't have. One solution is to add it yourself, e.g.

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')

example_1.png