\documentclass[10pt,a4paper]{article} % Packages \usepackage{fancyhdr} % For header and footer \usepackage{multicol} % Allows multicols in tables \usepackage{tabularx} % Intelligent column widths \usepackage{tabulary} % Used in header and footer \usepackage{hhline} % Border under tables \usepackage{graphicx} % For images \usepackage{xcolor} % For hex colours %\usepackage[utf8x]{inputenc} % For unicode character support \usepackage[T1]{fontenc} % Without this we get weird character replacements \usepackage{colortbl} % For coloured tables \usepackage{setspace} % For line height \usepackage{lastpage} % Needed for total page number \usepackage{seqsplit} % Splits long words. %\usepackage{opensans} % Can't make this work so far. Shame. Would be lovely. \usepackage[normalem]{ulem} % For underlining links % Most of the following are not required for the majority % of cheat sheets but are needed for some symbol support. \usepackage{amsmath} % Symbols \usepackage{MnSymbol} % Symbols \usepackage{wasysym} % Symbols %\usepackage[english,german,french,spanish,italian]{babel} % Languages % Document Info \author{seleven} \pdfinfo{ /Title (ggalluvial.pdf) /Creator (Cheatography) /Author (seleven) /Subject (ggalluvial Cheat Sheet) } % Lengths and widths \addtolength{\textwidth}{6cm} \addtolength{\textheight}{-1cm} \addtolength{\hoffset}{-3cm} \addtolength{\voffset}{-2cm} \setlength{\tabcolsep}{0.2cm} % Space between columns \setlength{\headsep}{-12pt} % Reduce space between header and content \setlength{\headheight}{85pt} % If less, LaTeX automatically increases it \renewcommand{\footrulewidth}{0pt} % Remove footer line \renewcommand{\headrulewidth}{0pt} % Remove header line \renewcommand{\seqinsert}{\ifmmode\allowbreak\else\-\fi} % Hyphens in seqsplit % This two commands together give roughly % the right line height in the tables \renewcommand{\arraystretch}{1.3} \onehalfspacing % Commands \newcommand{\SetRowColor}[1]{\noalign{\gdef\RowColorName{#1}}\rowcolor{\RowColorName}} % Shortcut for row colour \newcommand{\mymulticolumn}[3]{\multicolumn{#1}{>{\columncolor{\RowColorName}}#2}{#3}} % For coloured multi-cols \newcolumntype{x}[1]{>{\raggedright}p{#1}} % New column types for ragged-right paragraph columns \newcommand{\tn}{\tabularnewline} % Required as custom column type in use % Font and Colours \definecolor{HeadBackground}{HTML}{333333} \definecolor{FootBackground}{HTML}{666666} \definecolor{TextColor}{HTML}{333333} \definecolor{DarkBackground}{HTML}{0A232B} \definecolor{LightBackground}{HTML}{F7F8F8} \renewcommand{\familydefault}{\sfdefault} \color{TextColor} % Header and Footer \pagestyle{fancy} \fancyhead{} % Set header to blank \fancyfoot{} % Set footer to blank \fancyhead[L]{ \noindent \begin{multicols}{3} \begin{tabulary}{5.8cm}{C} \SetRowColor{DarkBackground} \vspace{-7pt} {\parbox{\dimexpr\textwidth-2\fboxsep\relax}{\noindent \hspace*{-6pt}\includegraphics[width=5.8cm]{/web/www.cheatography.com/public/images/cheatography_logo.pdf}} } \end{tabulary} \columnbreak \begin{tabulary}{11cm}{L} \vspace{-2pt}\large{\bf{\textcolor{DarkBackground}{\textrm{ggalluvial Cheat Sheet}}}} \\ \normalsize{by \textcolor{DarkBackground}{seleven} via \textcolor{DarkBackground}{\uline{cheatography.com/139867/cs/29584/}}} \end{tabulary} \end{multicols}} \fancyfoot[L]{ \footnotesize \noindent \begin{multicols}{3} \begin{tabulary}{5.8cm}{LL} \SetRowColor{FootBackground} \mymulticolumn{2}{p{5.377cm}}{\bf\textcolor{white}{Cheatographer}} \\ \vspace{-2pt}seleven \\ \uline{cheatography.com/seleven} \\ \end{tabulary} \vfill \columnbreak \begin{tabulary}{5.8cm}{L} \SetRowColor{FootBackground} \mymulticolumn{1}{p{5.377cm}}{\bf\textcolor{white}{Cheat Sheet}} \\ \vspace{-2pt}Published 29th October, 2021.\\ Updated 29th October, 2021.\\ Page {\thepage} of \pageref{LastPage}. \end{tabulary} \vfill \columnbreak \begin{tabulary}{5.8cm}{L} \SetRowColor{FootBackground} \mymulticolumn{1}{p{5.377cm}}{\bf\textcolor{white}{Sponsor}} \\ \SetRowColor{white} \vspace{-5pt} %\includegraphics[width=48px,height=48px]{dave.jpeg} Measure your website readability!\\ www.readability-score.com \end{tabulary} \end{multicols}} \begin{document} \raggedright \raggedcolumns % Set font size to small. Switch to any value % from this page to resize cheat sheet text: % www.emerson.emory.edu/services/latex/latex_169.html \footnotesize % Small font. \begin{multicols*}{3} \begin{tabularx}{5.377cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{5.377cm}}{\bf\textcolor{white}{{\bf{GGALLUVIAL -{}-{}-{}- Cheatsheet}}}} \tn % Row 0 \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{{\bf{Introduction to ggalluvial}}} \tn % Row Count 1 (+ 1) % Row 1 \SetRowColor{white} \mymulticolumn{1}{x{5.377cm}}{The {\bf{ggalluvial}} package is a ggplot2 extension for producing alluvial plots. \{\{nl\}\}{\bf{Alluvial plots}} use variable-width ribbons and stacked bar plots to represent multi-dimensional or repeated-measures data with categorical or ordinal variables. \{\{nl\}\} There are two types of alluvial format: \{\{nl\}\}{\bf{Alluvial (Wide) Format}} \& {\bf{Lodes (Long) Format }}} \tn % Row Count 9 (+ 8) % Row 2 \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{{\bf{More information}}} \tn % Row Count 10 (+ 1) % Row 3 \SetRowColor{white} \mymulticolumn{1}{x{5.377cm}}{\seqsplit{https://cran.r-project.org/web/packages/ggalluvial/ggalluvial.pdf}} \tn % Row Count 12 (+ 2) \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{x{1.74195 cm} x{3.23505 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{5.377cm}}{\bf\textcolor{white}{Five Essential Components}} \tn % Row 0 \SetRowColor{LightBackground} 1. AIXS\{\{nobreak\}\} & A dimension (variable) along which the data are vertically grouped at a fixed horizontal position. \tn % Row Count 4 (+ 4) % Row 1 \SetRowColor{white} 2. ALLUVIUM\{\{nobreak\}\} & Horizontal (x-) splines called alluvia span the width of the plot. \tn % Row Count 7 (+ 3) % Row 2 \SetRowColor{LightBackground} 3. STRATUM \{\{nobreak\}\} & The groups at each axis are depicted as opaque blocks called strata. \tn % Row Count 10 (+ 3) % Row 3 \SetRowColor{white} 4. LODE\{\{nobreak\}\} & The alluvia intersect the strata at lodes. \tn % Row Count 12 (+ 2) % Row 4 \SetRowColor{LightBackground} 5. FLOW\{\{nobreak\}\} & The segments of the alluvia between pairs of adjacent axes are flows. \tn % Row Count 15 (+ 3) \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{x{1.94103 cm} x{3.03597 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{5.377cm}}{\bf\textcolor{white}{Basic Alluvial {\bf{Wide}} Format}} \tn % Row 0 \SetRowColor{LightBackground} {\bf{Discription}}\{\{nobreak\}\} & {\bf{Example}} \tn % Row Count 2 (+ 2) % Row 1 \SetRowColor{white} load packages\{\{nobreak\}\} & library(ggalluvial) \tn % Row Count 4 (+ 2) % Row 2 \SetRowColor{LightBackground} basic ggplot\{\{nobreak\}\} & gg \textless{}- ggplot(as.data.frame\{\{nl\}\}(UCBAdmissions),\{\{nl\}\}aes(y = Freq, axis1 = Gender,\{\{nl\}\} axis2 = Dept, axis3 = Admit)) \tn % Row Count 9 (+ 5) % Row 3 \SetRowColor{white} add alluvium\{\{nobreak\}\} & + geom\_alluvium() \tn % Row Count 11 (+ 2) % Row 4 \SetRowColor{LightBackground} add stratum\{\{nobreak\}\} & + geom\_stratum() \tn % Row Count 13 (+ 2) % Row 5 \SetRowColor{white} add text\{\{nobreak\}\} & + geom\_text(stat = "stratum", aes(label = \seqsplit{paste(after\_stat(stratum))))} \tn % Row Count 17 (+ 4) % Row 6 \SetRowColor{LightBackground} add title\{\{nobreak\}\} & + ggtitle("UC Berkeley admissions and rejections") \tn % Row Count 20 (+ 3) \hhline{>{\arrayrulecolor{DarkBackground}}--} \SetRowColor{LightBackground} \mymulticolumn{2}{x{5.377cm}}{The dataset {\bf{"UCBAdmissions"}} is an aggregate data on applicants to graduate school at Berkeley for the six largest departments in 1973 classified by admission and sex. \newline It is a {\bf{3-dimensional array}} resulting from cross-tabulating 4526 observations on 3 variables. \newline {\bf{No}} ~ {\bf{Name}} ~ {\bf{Levels}} \newline 1 ~ Admit ~Admitted, Rejected \newline 2 ~ Gender ~ Male, Female \newline 3 ~ Dept ~ A, B, C, D, E, F} \tn \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{5.377cm}}{\bf\textcolor{white}{Graph of Wide Format}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{p{5.377cm}}{\vspace{1px}\centerline{\includegraphics[width=5.1cm]{/web/www.cheatography.com/public/uploads/seleven_1635411071_WechatIMG321.jpeg}}} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{x{2.43873 cm} x{2.53827 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{5.377cm}}{\bf\textcolor{white}{Change Color}} \tn % Row 0 \SetRowColor{LightBackground} the border\{\{nobreak\}\} \{\{nl\}\}of alluvium & geom\_alluvium(color="red")\{\{nobreak\}\} \tn % Row Count 3 (+ 3) % Row 1 \SetRowColor{white} the border \{\{nobreak\}\}\{\{nl\}\}of stratum & geom\_stratum(color="blue")\{\{nobreak\}\} \tn % Row Count 5 (+ 2) % Row 2 \SetRowColor{LightBackground} the fill of alluvium & geom\_alluvium(aes(fill=Dept))\{\{nobreak\}\}\{\{nl\}\}geom\_alluvium(aes(fill=Gender))\{\{nl\}\}geom\_stratum(aes(fill=Admit)) \tn % Row Count 11 (+ 6) % Row 3 \SetRowColor{white} the fill of stratum & geom\_stratum(aes(fill=Dept))\{\{nobreak\}\}\{\{nl\}\}geom\_stratum(aes(fill=Gender))\{\{nl\}\}geom\_stratum(aes(fill=Admit)) \tn % Row Count 17 (+ 6) \hhline{>{\arrayrulecolor{DarkBackground}}--} \SetRowColor{LightBackground} \mymulticolumn{2}{x{5.377cm}}{Setting the different fills of alluvium and stratum can help analysts easily analyze the data from different aspects.} \tn \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{5.377cm}}{\bf\textcolor{white}{Graph after changing Color \& Fill by Dept}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{p{5.377cm}}{\vspace{1px}\centerline{\includegraphics[width=5.1cm]{/web/www.cheatography.com/public/uploads/seleven_1635411096_WechatIMG322.jpeg}}} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{If using {\bf{"fill=Dept"}}, it means we are using colors grouped by each department. \newline It can help analyst to see the {\bf{formation}} of each department: how many males and females in each department. \newline Also it shows how many people in each department are admitted and rejected.} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{5.377cm}}{\bf\textcolor{white}{Graph after changing Color \& Fill by Gender}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{p{5.377cm}}{\vspace{1px}\centerline{\includegraphics[width=5.1cm]{/web/www.cheatography.com/public/uploads/seleven_1635411421_WechatIMG325.jpeg}}} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{If using {\bf{"fill=Gender"}}, it means we are using colors grouped by different gender. \newline It can help analyst to see how many males and females apply for each department and finally admitted or rejected.} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{5.377cm}}{\bf\textcolor{white}{Graph after changing Color \& Fill by Admit}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{p{5.377cm}}{\vspace{1px}\centerline{\includegraphics[width=5.1cm]{/web/www.cheatography.com/public/uploads/seleven_1635411443_WechatIMG326.jpeg}}} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{If using {\bf{"fill=Admit"}}, it means we are using colors grouped by admitted or rejected. \newline It can help analyst to see the {\bf{formation}} admitted students: how many admitted students are from each department and of different gender.} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{x{2.18988 cm} x{2.78712 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{5.377cm}}{\bf\textcolor{white}{Change Width}} \tn % Row 0 \SetRowColor{LightBackground} the width of alluvium\{\{nobreak\}\} & \seqsplit{geom\_alluvium(color="red"},\{\{nl\}\}aes(fill=Dept),width=1/12) \tn % Row Count 3 (+ 3) % Row 1 \SetRowColor{white} the width of stratum\{\{nobreak\}\} & \seqsplit{geom\_stratum(color="blue"},\{\{nl\}\}aes(fill=Dept),width=1/12) \tn % Row Count 6 (+ 3) \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{5.377cm}}{\bf\textcolor{white}{Graph after changing Width}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{p{5.377cm}}{\vspace{1px}\centerline{\includegraphics[width=5.1cm]{/web/www.cheatography.com/public/uploads/seleven_1635428418_WechatIMG327.jpeg}}} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{5.377cm}}{\bf\textcolor{white}{Flip Coordinates}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{p{5.377cm}}{\vspace{1px}\centerline{\includegraphics[width=5.1cm]{/web/www.cheatography.com/public/uploads/seleven_1635429922_WechatIMG328.jpeg}}} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{Adding {\bf{coord\_flip()}} \newline Flip cartesian coordinates so that horizontal becomes vertical, and vertical, horizontal. This is primarily useful for converting geoms and statistics which display y conditional on x, to x conditional on y.} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{5.377cm}}{\bf\textcolor{white}{Adding lode}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{p{5.377cm}}{\vspace{1px}\centerline{\includegraphics[width=5.1cm]{/web/www.cheatography.com/public/uploads/seleven_1635474647_WechatIMG330.jpeg}}} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{Adding {\bf{geom\_lode()}}} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{5.377cm}}{\bf\textcolor{white}{geom\_alluvium vs geom\_flow}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{p{5.377cm}}{\vspace{1px}\centerline{\includegraphics[width=5.1cm]{/web/www.cheatography.com/public/uploads/seleven_1635475350_WechatIMG331.jpeg}}} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{The graph is using {\bf{geom\_flow}}. \newline We can see the difference between {\bf{geom\_alluvium}} and {\bf{geom\_flow}}. \newline After we use "flow", all males apply for department A came together, which is also the same as other departments. It makes the graph much clearer than before since there is less cross alluviums between each axises.} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{x{2.09034 cm} x{2.88666 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{5.377cm}}{\bf\textcolor{white}{More coding help}} \tn % Row 0 \SetRowColor{LightBackground} {\bf{Description}} & {\bf{Example}} \tn % Row Count 1 (+ 1) % Row 1 \SetRowColor{white} Adding the names of each axis & \seqsplit{+scale\_x\_discrete(limits} = c("Gender", "Dept","Admit")) \tn % Row Count 4 (+ 3) % Row 2 \SetRowColor{LightBackground} Changing the fill of stratum & \seqsplit{+scale\_fill\_brewer(type} = "qual", palette = "Set1") \tn % Row Count 7 (+ 3) \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{x{2.04057 cm} x{2.93643 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{5.377cm}}{\bf\textcolor{white}{Basic Lodes (Long) Format}} \tn % Row 0 \SetRowColor{LightBackground} {\bf{Description}} & {\bf{Example}} \tn % Row Count 1 (+ 1) % Row 1 \SetRowColor{white} Convert data to Lodes format & \seqsplit{to\_lodes\_form(as.data.frame(UCBAdmissions)},axes = 1:3,id = "Cohort") \tn % Row Count 4 (+ 3) % Row 2 \SetRowColor{LightBackground} load data & data(majors) \{\{nl\}\} majors\$curriculum \textless{}- \seqsplit{as.factor(majors\$curriculum)} \tn % Row Count 7 (+ 3) % Row 3 \SetRowColor{white} basic ggplot\{\{nobreak\}\} & ggplot(majors,aes(x = semester, stratum = curriculum, alluvium = student,fill = curriculum, label = curriculum)) \tn % Row Count 12 (+ 5) % Row 4 \SetRowColor{LightBackground} add flow & +geom\_flow(stat = "alluvium", lode.guidance = "frontback",color = "darkgray") + \tn % Row Count 16 (+ 4) % Row 5 \SetRowColor{white} add stratum & +geom\_stratum() \tn % Row Count 17 (+ 1) % Row 6 \SetRowColor{LightBackground} add title & +ggtitle("student curricula across several semesters") \tn % Row Count 20 (+ 3) \hhline{>{\arrayrulecolor{DarkBackground}}--} \SetRowColor{LightBackground} \mymulticolumn{2}{x{5.377cm}}{{\bf{The long format requires an additional indexing column that links the rows corresponding to a common cohort.}} \newline The data follows the major curricula of 10 students across 8 academic semesters. Missing values indicate undeclared majors. \newline A data frame with 80 rows and 3 variables: \newline 1. student: student identifier \newline 2. semester: character tag for odd-numbered semesters \newline 3. curriculum: declared major program} \tn \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{5.377cm}}{\bf\textcolor{white}{Graph of Lodes Format}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{p{5.377cm}}{\vspace{1px}\centerline{\includegraphics[width=5.1cm]{/web/www.cheatography.com/public/uploads/seleven_1635477830_WechatIMG335.jpeg}}} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{This graph clearly shows a set of students' academic curricula over the course of several semesters. \{\{nl\}\}The lode format gives us the option to aggregate the flows between adjacent axes, which may be appropriate when the transitions between adjacent axes are of primary importance.} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} % That's all folks \end{multicols*} \end{document}