Cocktails

Pairwise correlation, Network diagram, Principal component analysis (PCA)

Published

May 25, 2020

Notable topics: Pairwise correlation, Network diagram, Principal component analysis (PCA)

Recorded on: 2020-05-25

Timestamps by: Eric Fletcher

View code

Screencast

Timestamps

fct_reorder
forcats

Use fct_reorder from the forcats package to reorder the ingredient factor levels along n.

fct_lump
forcats

Use fct_lump from the forcats package to lump together all the levels except the n most frequent in the category and ingredient variables.

pairwise_cor
widyr

Use pairwise_cor from the widyr package to find the correlation between the ingredients.

reorder_withinscale_x_reordered
tidytext

Use reorder_within from the tidytext package with scale_x_reordered to reorder the the columns in each facet.

graph_from_data_frameggraphgeom_edge_linkgeom_node_pointgeom_node_label
igraphggraph

Use the ggraph and igraph packages to create a network diagram

extract
tidyr

Use extract from the tidyr package with regex = (.*) oz to create a new variable amount which doesn't include the oz.

extract
tidyr

Use extract with regex to turn the strings in the new amount variable into separate columns for the ones, numerator, and denominator.

replace_na
tidyr

Use replace_na from the tidyr package to replace NA with zeros in the ones, numberator, and denominator columns. David ends up reaplcing the zero in the denominator column with ones in order for the calculation to work.

geom_text_repel
ggrepel

Use geom_text_repel from the ggrepel package to add ingredient labels to the geom_point plot.

na_if
dplyr

Use na_if from the dplyr package to replace zeros with NA

scale_size_continuous
ggplot2

Use scale_size_continuous with labels = percent_format() to convert size legend values to percent.

graph_from_data_framegeom_node_point
igraphggraph

Change the size of the points in the network diagram proportional to n using vertices = ingredient_info within graph_from_data_frame and aes(size = n) within geom_node_point.

widely_svdtop_nabsgeom_colreorder_withinscale_y_reorderedfacet_wrap
widyr

Use widely_svd from the widyr package to perform principle component analysis on the ingredients.

paste0
base

Use paste0 to concatenate PC and dimension in the facet panel titles.

Summary of screencast.