Last active
April 21, 2021 09:58
-
-
Save vankesteren/e7070e491a410a6cd4d1fb6dceeb74aa to your computer and use it in GitHub Desktop.
Plots for a wine data visualisation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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) |
Author
vankesteren
commented
Nov 28, 2019
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment