\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{felyne223} \pdfinfo{ /Title (r.pdf) /Creator (Cheatography) /Author (felyne223) /Subject (R 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}{39A318} \definecolor{LightBackground}{HTML}{F2F9F0} \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{R Cheat Sheet}}}} \\ \normalsize{by \textcolor{DarkBackground}{felyne223} via \textcolor{DarkBackground}{\uline{cheatography.com/146361/cs/31646/}}} \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}felyne223 \\ \uline{cheatography.com/felyne223} \\ \end{tabulary} \vfill \columnbreak \begin{tabulary}{5.8cm}{L} \SetRowColor{FootBackground} \mymulticolumn{1}{p{5.377cm}}{\bf\textcolor{white}{Cheat Sheet}} \\ \vspace{-2pt}Not Yet Published.\\ Updated 13th April, 2022.\\ 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*}{2} \begin{tabularx}{8.4cm}{x{1.76 cm} x{6.24 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{8.4cm}}{\bf\textcolor{white}{Data Structure}} \tn % Row 0 \SetRowColor{LightBackground} Vectors & Entries all types \tn % Row Count 1 (+ 1) % Row 1 \SetRowColor{white} Arrays & Multidimensional, all of the same type. A 2D array is a matrix. \tn % Row Count 4 (+ 3) % Row 2 \SetRowColor{LightBackground} Data frames & A list of vectors of the same length. These can be of different types. Each has a name. \tn % Row Count 7 (+ 3) % Row 3 \SetRowColor{white} Lists & Entries are completely general. Good for returning output of a function. `list(vec, num, char)` \tn % Row Count 11 (+ 4) \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{x{1.44 cm} x{6.56 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{8.4cm}}{\bf\textcolor{white}{Data Types}} \tn % Row 0 \SetRowColor{LightBackground} \seqsplit{Numeric} & `is.numeric(x)` to check if x is numeric \tn % Row Count 2 (+ 2) % Row 1 \SetRowColor{white} \seqsplit{Character} & `character(x) ` to check if x is character \tn % Row Count 4 (+ 2) % Row 2 \SetRowColor{LightBackground} \seqsplit{Logical} & `is.logical(x)` to check if x is logical \tn % Row Count 6 (+ 2) % Row 3 \SetRowColor{white} Factor & `is.factor(x)` to check if x is a factor. Factors are numeric. `factor(x)` coerce number x into factor. \tn % Row Count 10 (+ 4) \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{8.4cm}}{\bf\textcolor{white}{Creating Vectors}} \tn % Row 0 \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{`c(1, 2, 3)`} \tn % Row Count 1 (+ 1) % Row 1 \SetRowColor{white} \mymulticolumn{1}{x{8.4cm}}{`1:7`} \tn % Row Count 2 (+ 1) % Row 2 \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{`seq(from=1, to=10, by=.5)`} \tn % Row Count 3 (+ 1) % Row 3 \SetRowColor{white} \mymulticolumn{1}{x{8.4cm}}{`rep(1:5, each=3, time=2)`} \tn % Row Count 4 (+ 1) % Row 4 \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{`scan("filename")`} \tn % Row Count 5 (+ 1) \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{x{4.24 cm} x{3.76 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{8.4cm}}{\bf\textcolor{white}{Extracting Elements from Vectors}} \tn % Row 0 \SetRowColor{LightBackground} `x{[}c(2,17,4){]}` & By index \tn % Row Count 1 (+ 1) % Row 1 \SetRowColor{white} `x{[}-c(2,17,4){]}` & By excluding some indices \tn % Row Count 3 (+ 2) % Row 2 \SetRowColor{LightBackground} `x{[}x\textless{}3{]}` or `x{[}y=="female"{]}` & By logical statement \tn % Row Count 5 (+ 2) \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{x{4 cm} x{4 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{8.4cm}}{\bf\textcolor{white}{Vector Indices}} \tn % Row 0 \SetRowColor{LightBackground} `which.max(x)`, `which.min(x)`, `which(x\textless{}3)` & Extract index/indices of max, min, \textless{} 3 values in vector x \tn % Row Count 3 (+ 3) % Row 1 \SetRowColor{white} `order(x)` & Sort vector x \tn % Row Count 4 (+ 1) \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{x{4 cm} x{4 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{8.4cm}}{\bf\textcolor{white}{Read File}} \tn % Row 0 \SetRowColor{LightBackground} `scan(file="n.txt", what = "character", quote= " ")` & file = name, what = the type of data to be read, \tn % Row Count 3 (+ 3) % Row 1 \SetRowColor{white} \seqsplit{`read.csv(file="name.csv")`} & read csv file \tn % Row Count 5 (+ 2) % Row 2 \SetRowColor{LightBackground} \seqsplit{`readLines(file="name}.txt")` & read txt file line by line \tn % Row Count 7 (+ 2) \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{x{3.84 cm} x{4.16 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{8.4cm}}{\bf\textcolor{white}{Function}} \tn % Row 0 \SetRowColor{LightBackground} `sqr \textless{}- function(x) \{ return(x*x) \} ` & `sqr()` to call function \tn % Row Count 2 (+ 2) % Row 1 \SetRowColor{white} `if(x\textgreater{}3)\{return(x)\}` & if function \tn % Row Count 4 (+ 2) % Row 2 \SetRowColor{LightBackground} `invisible()` & Does the same as `return()` but does not print output to screen \tn % Row Count 8 (+ 4) % Row 3 \SetRowColor{white} `cat()` & Does the same as `print()` but is valid only for atomic types (logical, integer, real, complex, character) and names \tn % Row Count 14 (+ 6) % Row 4 \SetRowColor{LightBackground} `system.time()` & Output time taken to run a function. Output user, system, elapsed time. \tn % Row Count 18 (+ 4) \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{x{2.96 cm} x{5.04 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{8.4cm}}{\bf\textcolor{white}{List}} \tn % Row 0 \SetRowColor{LightBackground} `list\$sdev` & Extract element by name \tn % Row Count 1 (+ 1) % Row 1 \SetRowColor{white} `list{[}"sdev"{]}` & Extract element by name \tn % Row Count 2 (+ 1) % Row 2 \SetRowColor{LightBackground} `list{[}{[}1{]}{]}` & Extract element by index \tn % Row Count 3 (+ 1) \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{x{3.36 cm} x{4.64 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{8.4cm}}{\bf\textcolor{white}{Matrix}} \tn % Row 0 \SetRowColor{LightBackground} `matrix(1:8, nrow=4)` & Creates a matrix with 4 rows and 2 columns. 1:4 in first column, 5:8 in second column. \tn % Row Count 4 (+ 4) % Row 1 \SetRowColor{white} `cbind(1:4, 5:8)` & Creates a same matrix, as above. \tn % Row Count 6 (+ 2) % Row 2 \SetRowColor{LightBackground} `rownames(x) \textless{}- letters{[}1:4{]}` & Give row names \tn % Row Count 8 (+ 2) % Row 3 \SetRowColor{white} `colnames(x) \textless{}- letters{[}1:4{]}` & Give column names \tn % Row Count 10 (+ 2) % Row 4 \SetRowColor{LightBackground} `*` & Element-wise multiplication \tn % Row Count 12 (+ 2) % Row 5 \SetRowColor{white} `\%*\%` & Matrix multiplication \tn % Row Count 13 (+ 1) % Row 6 \SetRowColor{LightBackground} `solve(x)` & Inverse of a matrix x \tn % Row Count 14 (+ 1) % Row 7 \SetRowColor{white} \seqsplit{`as.matrix(dataframe)`} & Treats a all numeric data frame as a matrix \tn % Row Count 16 (+ 2) % Row 8 \SetRowColor{LightBackground} `apply(x, 2, mean)` & Performs an operation for all rows or columns. Margin = 2 performs operation on column, 1 on row. \tn % Row Count 21 (+ 5) % Row 9 \SetRowColor{white} `x{[}1,2{]}` & Extract element on row 1, col 2 of matrix x \tn % Row Count 23 (+ 2) % Row 10 \SetRowColor{LightBackground} `x{[},2{]}` & Extract elements on col 2 \tn % Row Count 25 (+ 2) % Row 11 \SetRowColor{white} `x{[},-2{]}` & Extract elements not on col 2 \tn % Row Count 27 (+ 2) \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{x{4 cm} x{4 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{8.4cm}}{\bf\textcolor{white}{Regular Expression}} \tn % Row 0 \SetRowColor{LightBackground} `grep("regexpr", vector)` & Return the indices of a vector that match a set of characters (or a pattern) \tn % Row Count 4 (+ 4) % Row 1 \SetRowColor{white} `grepl("regexpr", vector)` & Return TRUE or FASE for each element of a vector on the basis of whether it matches a set of characters \tn % Row Count 10 (+ 6) % Row 2 \SetRowColor{LightBackground} `regexpr("regexpr", vector)` & Tells you which elements match, where they match, and how long each match is. Matches the first occurrence of pattern in an element. \tn % Row Count 17 (+ 7) % Row 3 \SetRowColor{white} `gregexpr("regexpr", vector)` & Same as regexpr. Matches every occurrence of pattern in an element. \tn % Row Count 21 (+ 4) % Row 4 \SetRowColor{LightBackground} `gsub("regexpr", vector)` & String subs \tn % Row Count 23 (+ 2) % Row 5 \SetRowColor{white} `Curr.n` & Single wild card character e.g. `Curr.n` matches "Curran", "Curren" and "Currin" \tn % Row Count 27 (+ 4) % Row 6 \SetRowColor{LightBackground} `Curr(a|e|i)n` & Alternation. Matches "Curran", "Curren" and "Currin" \tn % Row Count 30 (+ 3) \end{tabularx} \par\addvspace{1.3em} \vfill \columnbreak \begin{tabularx}{8.4cm}{x{4 cm} x{4 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{8.4cm}}{\bf\textcolor{white}{Regular Expression (cont)}} \tn % Row 7 \SetRowColor{LightBackground} metacaracter & If a character is a regex metacharacter then it has a special meaning to the RegExp interpreter. `{[} {]}, {[}\textasciicircum{}{]}, \textbackslash{}, ?, *. +, \{,\}, \textasciicircum{}, \$, \textbackslash{}\textless{}, \textbackslash{}\textgreater{}, | and ()`. Escape done by preceding it with a double back slash `\textbackslash{}\textbackslash{}`. \tn % Row Count 11 (+ 11) % Row 8 \SetRowColor{white} `{[}a-9{]}` & Will match any digit from 0 to 9 \tn % Row Count 13 (+ 2) % Row 9 \SetRowColor{LightBackground} `{[}a-z{]}` & Will match any lower case letter from a to z \tn % Row Count 16 (+ 3) % Row 10 \SetRowColor{white} `{[}A-Z0-9{]}` & Will match uppercase letter from A to Z or any digit from 0 to 9 \tn % Row Count 20 (+ 4) % Row 11 \SetRowColor{LightBackground} `{[}:alpha:{]}` & Alphabetic (only letters) \tn % Row Count 22 (+ 2) % Row 12 \SetRowColor{white} `{[}:lower:{]}` & Lowercase letters \tn % Row Count 23 (+ 1) % Row 13 \SetRowColor{LightBackground} `{[}:upper:{]}` & Uppercase letters \tn % Row Count 24 (+ 1) % Row 14 \SetRowColor{white} `{[}:digit:{]}` & Digits \tn % Row Count 25 (+ 1) % Row 15 \SetRowColor{LightBackground} `{[}:alnum:{]}` & Alphanumeric (letters and digits) \tn % Row Count 27 (+ 2) % Row 16 \SetRowColor{white} `{[}:space:{]}` & White space \tn % Row Count 28 (+ 1) % Row 17 \SetRowColor{LightBackground} `{[}:punct:{]}` & Punctuation \tn % Row Count 29 (+ 1) % Row 18 \SetRowColor{white} `?` & Matches at most 1 time; optional string \tn % Row Count 31 (+ 2) \end{tabularx} \par\addvspace{1.3em} \vfill \columnbreak \begin{tabularx}{8.4cm}{x{4 cm} x{4 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{8.4cm}}{\bf\textcolor{white}{Regular Expression (cont)}} \tn % Row 19 \SetRowColor{LightBackground} `*` & Matches at least 0 times \tn % Row Count 2 (+ 2) % Row 20 \SetRowColor{white} `+` & Matches at least 1 time \tn % Row Count 4 (+ 2) % Row 21 \SetRowColor{LightBackground} `\{a,b\} ` & match from a to b' occurrences of the previous pattern \tn % Row Count 7 (+ 3) % Row 22 \SetRowColor{white} `\{a,\}` & match a or more occurrences of the previous pattern \tn % Row Count 10 (+ 3) % Row 23 \SetRowColor{LightBackground} `{[}CK{]} (u|a)r\{1,2\}(i|e)*n` & Looks for a pattern that matches C or K, matches u or a, r appears 1 to 2 times, matches i or e for zero or more occurrences, matches `n` \tn % Row Count 17 (+ 7) % Row 24 \SetRowColor{white} Metacaracter & If a character is a regex metacharacter then it has a special meaning to the RegExp interpreter. {[} {]}, {[} \textasciicircum{} {]}, \textbackslash{}, ?, *. +, \{,\}, \textasciicircum{}, \$, \textbackslash{}\textless{}, \textbackslash{}\textgreater{}, | and (). Escape done by preceding it with a double back slash \textbackslash{}\textbackslash{}. \tn % Row Count 28 (+ 11) % Row 25 \SetRowColor{LightBackground} Back Substitution & Use round brackets in regexp to capture the match of interest. Use \textbackslash{}\textbackslash{}1 \textbackslash{}\textbackslash{}2 ...\textbackslash{}\textbackslash{}n backreference operators to retrieve the information we matched. \tn % Row Count 36 (+ 8) \end{tabularx} \par\addvspace{1.3em} \vfill \columnbreak \begin{tabularx}{8.4cm}{x{4 cm} x{4 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{8.4cm}}{\bf\textcolor{white}{Regular Expression (cont)}} \tn % Row 26 \SetRowColor{LightBackground} `(\textasciicircum{}{[}0-9{]}{[}.{]}){[} {]}+({[}A-Za-z{]}+\$)` & Example use of round brackets in regexp. \textbackslash{}\textbackslash{}1 extracts information in first round bracket, \textbackslash{}\textbackslash{}2 extracts information in second round bracket. \tn % Row Count 7 (+ 7) % Row 27 \SetRowColor{white} `substr(string, start, stop)` & Extract substrings. `substr('abcdef', 2, 4)` returns `bcd`. \tn % Row Count 10 (+ 3) % Row 28 \SetRowColor{LightBackground} `paste(x, y, sep = ' ', collapse =' ' )` & `paste` elements x and y (more are allowed). sep = separator between corresponding sub-elements in x and y. Collapse = separator between x and y. \tn % Row Count 18 (+ 8) % Row 29 \SetRowColor{white} `strsplit(vector of strings, sep=' ')` & Separate strings in vector based on separator set in `sep` \tn % Row Count 21 (+ 3) \hhline{>{\arrayrulecolor{DarkBackground}}--} \SetRowColor{LightBackground} \mymulticolumn{2}{x{8.4cm}}{Regular expression provide a way of matching patterns in text.} \tn \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{x{3.92 cm} x{4.08 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{8.4cm}}{\bf\textcolor{white}{R plot}} \tn % Row 0 \SetRowColor{LightBackground} `par(mfrow=c(3,3))` & Set the plotting area to 3 * 3 array \tn % Row Count 2 (+ 2) % Row 1 \SetRowColor{white} `apply(matrix, 2, hist, xlim=c(-4, 4))` & for each column in matrix, plot histogram, x axis limit is -4 to 4 \tn % Row Count 6 (+ 4) % Row 2 \SetRowColor{LightBackground} `rnorm(n, mean=1, sd=1)` & random number generation following normal distribution \tn % Row Count 9 (+ 3) % Row 3 \SetRowColor{white} `lm(y\textasciitilde{}x, data=data)` & linear regression \tn % Row Count 11 (+ 2) % Row 4 \SetRowColor{LightBackground} `abline(lm(y\textasciitilde{}x))` & plot linear regression \tn % Row Count 13 (+ 2) % Row 5 \SetRowColor{white} `plot(x, y)` & plot points \tn % Row Count 14 (+ 1) % Row 6 \SetRowColor{LightBackground} `main`, `xlim`, & variables to be included in graphical functions. Title, x-axis range, \tn % Row Count 18 (+ 4) \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{x{4 cm} x{4 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{8.4cm}}{\bf\textcolor{white}{R graphics}} \tn % Row 0 \SetRowColor{LightBackground} Bitmap & Graphic format, pixelwise representation of your screen. If \textgreater{}1000 points/lines, use Bitmap format instead of Vector. Bitmap formats are bmp, png, jpg. \tn % Row Count 8 (+ 8) % Row 1 \SetRowColor{white} Vector & Graphic format, uses a set of basic plotting tools (point, line, etc) to describe a plot. Looks better, especially when you change devices/resolution. Vector foramts are pdf, eps, wmf. \tn % Row Count 18 (+ 10) % Row 2 \SetRowColor{LightBackground} \seqsplit{`pdf(filename="myplot}.pdf", width=5, height=5)` & Saving to pdf format. Many different commands (jpeg, png, postscript) depending on the output type you want. \tn % Row Count 24 (+ 6) % Row 3 \SetRowColor{white} Base R vs ggplot & Base R: You control everything, great power, great responsibility. ggplot: Nice looking defaults, can be tough if you want something unusual. \tn % Row Count 32 (+ 8) \end{tabularx} \par\addvspace{1.3em} \vfill \columnbreak \begin{tabularx}{8.4cm}{x{4 cm} x{4 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{8.4cm}}{\bf\textcolor{white}{R graphics (cont)}} \tn % Row 4 \SetRowColor{LightBackground} Base R - environment set up & `par(mfrow=c(2, 2), mex=0.5)` etc. \tn % Row Count 2 (+ 2) % Row 5 \SetRowColor{white} Base R - type of plot & scatterplot (plot), histogram (hist), boxplot, barplot, dotplot (stripchart) \tn % Row Count 6 (+ 4) % Row 6 \SetRowColor{LightBackground} Base R - graph bits & points, lines, legend, text, box, axis, abline, title, polygon, rect. \tn % Row Count 10 (+ 4) % Row 7 \SetRowColor{white} Base R - graph parameters & xlim, ylim, xlab, ylab, main, sub, pch, lty, lwd, col, axes, type \tn % Row Count 14 (+ 4) % Row 8 \SetRowColor{LightBackground} `library(ggplot2)` & import ggplot library \tn % Row Count 16 (+ 2) % Row 9 \SetRowColor{white} `p \textless{}- ggplot(df, aes(x=xvar, \seqsplit{y=yvar))+geom\_line()`} & Aesthetics are what you are going to plot, geoms are how you are going to plot it \tn % Row Count 21 (+ 5) % Row 10 \SetRowColor{LightBackground} ggplot - Scales & Use to change automatically chosen axis components. Can specify name, limits, labels, breaks (control tick marks) and na.value. \seqsplit{`p+scale\_color\_discrete()`} \tn % Row Count 29 (+ 8) % Row 11 \SetRowColor{white} ggplot `facet\_wrap(\textasciitilde{}var)` & put graphs of different groups into different panels. `p+facet\_wrap(\textasciitilde{}variable)` \tn % Row Count 33 (+ 4) \end{tabularx} \par\addvspace{1.3em} \vfill \columnbreak \begin{tabularx}{8.4cm}{x{4 cm} x{4 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{8.4cm}}{\bf\textcolor{white}{R graphics (cont)}} \tn % Row 12 \SetRowColor{LightBackground} ggplot `facet\_grid(var1\textasciitilde{}var2)` & good if we have multiple variables to facet on \tn % Row Count 3 (+ 3) % Row 13 \SetRowColor{white} ggplot \seqsplit{`library(patchwork)`} & Combine separate ggplots in a grid. Once called, `p1/p2` puts p1 above p2, `p1+p2` puts p1 next to p2 \tn % Row Count 9 (+ 6) % Row 14 \SetRowColor{LightBackground} ggplot `theme\_bw()` & Modify general appearance of the plot with themes. This changes plot background to white. \tn % Row Count 14 (+ 5) \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} % That's all folks \end{multicols*} \end{document}