R Basics Cheat Sheet



I reproduce some of the plots from Rstudio’s ggplot2 cheat sheet using Base R graphics. I didn’t try to pretty up these plots, but you should.

I use this dataset

The main functions that I generally use for plotting are

  • Plotting Functions
    • plot: Makes scatterplots, line plots, among other plots.
    • lines: Adds lines to an already-made plot.
    • par: Change plotting options.
    • hist: Makes a histogram.
    • boxplot: Makes a boxplot.
    • text: Adds text to an already-made plot.
    • legend: Adds a legend to an already-made plot.
    • mosaicplot: Makes a mosaic plot.
    • barplot: Makes a bar plot.
    • jitter: Adds a small value to data (so points don’t overlap on a plot).
    • rug: Adds a rugplot to an already-made plot.
    • polygon: Adds a shape to an already-made plot.
    • points: Adds a scatterplot to an already-made plot.
    • mtext: Adds text on the edges of an already-made plot.
  • Sometimes needed to transform data (or make new data) to make appropriate plots:
    • table: Builds frequency and two-way tables.
    • density: Calculates the density.
    • loess: Calculates a smooth line.
    • predict: Predicts new values based on a model.

All of the plotting functions have arguments that control the way the plot looks. You should read about these arguments. In particular, read carefully the help page ?plot.default. Useful ones are:

  • main: This controls the title.
  • xlab, ylab: These control the x and y axis labels.
  • col: This will control the color of the lines/points/areas.
  • cex: This will control the size of points.
  • pch: The type of point (circle, dot, triangle, etc…)
  • lwd: Line width.
  • lty: Line type (solid, dashed, dotted, etc…).

Discrete

In this article, I will review each of functions and information included in the cheat sheet with a little more detail. R Tool Inputs and Outputs. The most basic component of working with the R Tool is reading in and writing out data to and from the R Tool. This is done, intuitively, with the read.Alteryx and write.Alteryx functions. R For Dummies Cheat Sheet By Andrie de Vries, Joris Meys R is more than just a statistical programming language. It’s also a powerful tool for all kinds of data processing and manipulation, used by a community of programmers and users, academics, and practitioners. R Syntax Comparison:: CHEAT SHEET Even within one syntax, there are o'en variations that are equally valid. As a case study, let’s look at the ggplot2 syntax. Ggplot2 is the plotting package that lives within the tidyverse. If you read down this column, all the code here produces the same graphic. Quickplot ggplot.

Barplot

R basics cheat sheet printable

Different type of bar plot

Continuous X, Continuous Y

Scatterplot

Jitter points to account for overlaying points.

Add a rug plot

Add a Loess Smoother

Basics

Loess smoother with upper and lower 95% confidence bands

Loess smoother with upper and lower 95% confidence bands and that fancy shading from ggplot2.

Add text to a plot

Discrete X, Discrete Y

Mosaic Plot

Color code a scatterplot by a categorical variable and add a legend.

par sets the graphics options, where mfrow is the parameter controling the facets.

The first line sets the new options and saves the old options in the list old_options. The last line reinstates the old options.

This R Markdown site was created with workflowr

Obtaining R

R is available for Linux, MacOS, and Windows. Software can be downloaded from The Comprehensive R Archive Network (CRAN).

Startup

After R is downloaded and installed, simply find and launch R from your Applications folder.

Entering Commands

R is a command line driven program. The user enters commands at the prompt (> by default) and each command is executed one at a time.

The Workspace

R Basics Cheat Sheet

The workspace is your current R working environment and includes any user-defined objects (vectors, matrices, data frames, lists, functions). At the end of an R session, the user can save an image of the current workspace that is automatically reloaded the next time R is started.

Cheat

Graphic User Interfaces

Aside from the built in R console, RStudio is the most popular R code editor, and it interfaces with R for Windows, MacOS, and Linux platforms.

Operators in R

R Basics Cheat Sheet Template

R's binary and logical operators will look very familiar to programmers. Note that binary operators work on vectors and matrices as well as scalars.

Arithmetic Operators include:

OperatorDescription
+addition
-subtraction
*multiplication
/division
^ or ** exponentiation

Logical Operators include:

OperatorDescription
>greater than
>=greater than or equal to
exactly equal to
!=not equal to

Data Types

R has a wide variety of data types including scalars, vectors (numerical, character, logical), matrices, data frames, and lists.

Creating New Variables

Use the assignment operator <- to create new variables.

# An example of computing the mean with variables
mydata$sum <- mydata$x1 + mydata$x2
mydata$mean <- (mydata$x1 + mydata$x2)/2

Functions

Almost everything in R is done through functions. A function is a piece of code written to carry out a specified task; it may accept arguments or parameters (or not) and it may return one or more values (or not!). In R, a function is defined with the construct:

R basics cheat sheet template

function ( arglist ) {body}

The code in between the curly braces is the body of the function. Note that by using built-in functions, the only thing you need to worry about is how to effectively communicate the correct input arguments (arglist) and manage the return value/s (if any).

Importing Data

Importing data into R is fairly simple. R offers options to import many file types, from CSVs to databases.

For example, this is how to import a CSV into R.

Basic R Commands Cheat Sheet

# first row contains variable names, comma is separator
# assign the variable id to row names
# note the / instead of on mswindows systems
mydata <- read.table('c:/mydata.csv', header=TRUE,
sep=',', row.names='id')

Descriptive Statistics

R provides a wide range of functions for obtaining summary statistics. One way to get descriptive statistics is to use the sapply( ) function with a specified summary statistic.

R Basics Cheat Sheet Printable

Below is how to get the mean with the sapply( ) function:

# get means for variables in data frame mydata
# excluding missing values
sapply(mydata, mean, na.rm=TRUE)

Possible functions used in sapply include mean, sd, var, min, max, median, range, and quantile.

Plotting in R

In R, graphs are typically created interactively. Here is an example:

# Creating a Graph
attach(mtcars)
plot(wt, mpg)
abline(lm(mpg~wt))
title('Regression of MPG on Weight')

R Basics Cheat Sheet 2020

The plot( ) function opens a graph window and plots weight vs. miles per gallon. The next line of code adds a regression line to this graph. The final line adds a title.

Cheat Sheet For R

Packages

Packages are collections of R functions, data, and compiled code in a well-defined format. The directory where packages are stored is called the library. R comes with a standard set of packages. Others are available for download and installation. Once installed, they have to be loaded into the session to be used.

R Commands Cheat Sheet

.libPaths() # get library location
library() # see all packages installed
search() # see packages currently loaded

Getting Help

Once R is installed, there is a comprehensive built-in help system. At the program's command prompt you can use any of the following:

help.start() # general help
help(foo) # help about function foo
?foo # same thing
apropos('foo') # list all functions containing string foo
example(foo) # show an example of function foo

Going Further

R Basic Cheat Sheet

If you prefer an online interactive environment to learn R, this free R tutorial by DataCamp is a great way to get started.