Background

Basic Usage

> 1 + 2
[1] 3
> x <- c(1, 3, 5)
> x
[1] 1 3 5
> x + x
[1]  2  6 10
> 2 * x
[1]  2  6 10

A Simple Scatter Plot

with(faithful,
     plot(eruptions, waiting,
          xlab = "Eruption time (min)",
          ylab = "Waiting time to next eruption (min)"))

Fitting a Linear Regression

fit <- with(faithful, lm(waiting ~ eruptions))
fit
## 
## Call:
## lm(formula = waiting ~ eruptions)
## 
## Coefficients:
## (Intercept)    eruptions  
##       33.47        10.73

Adding the Regression Line to the Plot

with(faithful,
     plot(eruptions, waiting,
          xlab = "Eruption time (min)",
          ylab = "Waiting time to next eruption (min)"))
abline(coef(fit), col = "red", lwd = 3)

Packages and Package Libraries

A Useful Package: ggplot2

library(ggplot2)
p <- ggplot(faithful, aes(x = eruptions, y = waiting))
p + geom_point() + geom_smooth(method = "lm", se = FALSE)

Contrast to Point-and-Click Interfaces

R and Reproducability

Some R Introductions and Tutorials

R Markdown Tutorials