## ── Attaching packages ───────────────────────────────── tidyverse 1.2.1 ──
## ✔ ggplot2 3.2.1.9000 ✔ purrr 0.3.2
## ✔ tibble 2.1.3 ✔ dplyr 0.8.1
## ✔ tidyr 0.8.3 ✔ stringr 1.4.0
## ✔ readr 1.3.1 ✔ forcats 0.4.0
## ── Conflicts ──────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag() masks stats::lag()
library(firatheme)
wine <- read_delim("https://archive.ics.uci.edu/ml/machine-learning-databases/wine/wine.data",
delim = ",",
col_names = c(
"Cultivar", "Alcohol", "Malic acid", "Ash", "Alcalinity of ash", "Magnesium",
"Total phenols", "Flavanoids", "Nonflavanoid phenols", "Proanthocyanins",
"Color intensity", "Hue", "OD280/OD315", "Proline"
)
)
## Parsed with column specification:
## cols(
## Cultivar = col_double(),
## Alcohol = col_double(),
## `Malic acid` = col_double(),
## Ash = col_double(),
## `Alcalinity of ash` = col_double(),
## Magnesium = col_double(),
## `Total phenols` = col_double(),
## Flavanoids = col_double(),
## `Nonflavanoid phenols` = col_double(),
## Proanthocyanins = col_double(),
## `Color intensity` = col_double(),
## Hue = col_double(),
## `OD280/OD315` = col_double(),
## Proline = col_double()
## )

## $x
## [1] ""
##
## $y
## [1] ""
##
## $title
## [1] "Chemical properties of three different wine cultivars"
##
## attr(,"class")
## [1] "labels"
## ggsave("wine_plot.pdf", device = cairo_pdf, width = 12, height = 8)
pca <- princomp(wine[,-1], cor = TRUE)$scores[,1:2]
as_tibble(pca) %>%
set_names("x", "y") %>%
mutate(Cultivar = as_factor(wine$Cultivar)) %>%
ggplot(aes(x = x, y = y, colour = Cultivar, fill = Cultivar)) +
geom_polygon(stat = "density_2d", alpha = 0.1, colour = NA) +
geom_point() +geom_smooth()+theme_bw()+
coord_fixed() +
xlim(-7, 7) +
labs(x = "First principal component",
y = "Second principal component")
## Warning: Computation failed in `stat_density2d()`:
## there is no package called 'isoband'
## `geom_smooth()` using method = 'loess' and formula 'y ~ x'
