Skip to content

Instantly share code, notes, and snippets.

@vankesteren
Last active April 21, 2021 09:58
Show Gist options
  • Save vankesteren/e7070e491a410a6cd4d1fb6dceeb74aa to your computer and use it in GitHub Desktop.
Save vankesteren/e7070e491a410a6cd4d1fb6dceeb74aa to your computer and use it in GitHub Desktop.
Plots for a wine data visualisation
# script that outputs a graph
library(tidyverse)
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"
)
)
wine_long <-
wine %>%
mutate(Cultivar = as_factor(Cultivar)) %>%
pivot_longer(-Cultivar)
wine_long %>%
filter(name != "Total phenols") %>%
ggplot(aes(x = value, fill = Cultivar, colour = Cultivar)) +
geom_density(alpha = 0.8, colour = "black") +
geom_rug() +
facet_wrap(~name, scales = 'free') +
theme_fira() +
scale_fill_fira() +
scale_colour_fira() +
labs(
x = "",
y = "",
title = "Chemical properties of three different wine cultivars"
)
ggsave("wine_plot.pdf", device = cairo_pdf, width = 12, height = 8)
pca <- prcomp(wine[,-1], scale. = TRUE)
pc12 <- pca$x[,1:2]
as_tibble(pc12) %>%
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, contour = TRUE) +
geom_point() +
coord_fixed() +
theme_fira() +
scale_colour_fira() +
scale_fill_fira() +
xlim(-7, 7) +
labs(x = "First principal component",
y = "Second principal component")
ggsave("pca_plot.pdf", device = cairo_pdf, width = 12, height = 6)
@vankesteren
Copy link
Author

wine_pca

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment