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.

Revisions

  1. vankesteren revised this gist Apr 21, 2021. 1 changed file with 8 additions and 6 deletions.
    14 changes: 8 additions & 6 deletions wine_plots.R
    Original file line number Diff line number Diff line change
    @@ -13,14 +13,14 @@ wine <- read_delim("https://archive.ics.uci.edu/ml/machine-learning-databases/wi
    wine_long <-
    wine %>%
    mutate(Cultivar = as_factor(Cultivar)) %>%
    gather(key = "variable", value = "value", -Cultivar)
    pivot_longer(-Cultivar)

    wine_long %>%
    filter(variable != "Total phenols") %>%
    filter(name != "Total phenols") %>%
    ggplot(aes(x = value, fill = Cultivar, colour = Cultivar)) +
    geom_density(alpha = 0.8, colour = "black") +
    geom_rug() +
    facet_wrap(~variable, scales = 'free') +
    facet_wrap(~name, scales = 'free') +
    theme_fira() +
    scale_fill_fira() +
    scale_colour_fira() +
    @@ -32,12 +32,14 @@ wine_long %>%

    ggsave("wine_plot.pdf", device = cairo_pdf, width = 12, height = 8)

    pca <- princomp(wine[,-1], cor = TRUE)$scores[,1:2]
    as_tibble(pca) %>%
    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) +
    geom_polygon(stat = "density_2d", alpha = 0.1, colour = NA, contour = TRUE) +
    geom_point() +
    coord_fixed() +
    theme_fira() +
  2. vankesteren revised this gist Nov 29, 2019. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion wine_plots.R
    Original file line number Diff line number Diff line change
    @@ -33,7 +33,7 @@ wine_long %>%
    ggsave("wine_plot.pdf", device = cairo_pdf, width = 12, height = 8)

    pca <- princomp(wine[,-1], cor = TRUE)$scores[,1:2]
    as_tibble(umap) %>%
    as_tibble(pca) %>%
    set_names("x", "y") %>%
    mutate(Cultivar = as_factor(wine$Cultivar)) %>%
    ggplot(aes(x = x, y = y, colour = Cultivar, fill = Cultivar)) +
  3. vankesteren created this gist Nov 28, 2019.
    50 changes: 50 additions & 0 deletions wine_plots.R
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,50 @@
    # 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)) %>%
    gather(key = "variable", value = "value", -Cultivar)

    wine_long %>%
    filter(variable != "Total phenols") %>%
    ggplot(aes(x = value, fill = Cultivar, colour = Cultivar)) +
    geom_density(alpha = 0.8, colour = "black") +
    geom_rug() +
    facet_wrap(~variable, 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 <- princomp(wine[,-1], cor = TRUE)$scores[,1:2]
    as_tibble(umap) %>%
    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() +
    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)