---
title: "Plot Basics"
author: "Statistical Computing, 36-350"
date: "Monday September 19, 2016"
---

Plotting in R
===

Base R has a set of powerful plotting tools. An overview:

- `plot()`: generic plotting function
- `points()`: add points to an existing plot
- `lines()`, `abline()`: add lines to an existing plot
- `text()`, `legend()`: add text to an existing plot
- `rect()`, `polygon()`: add shapes to an existing plot
- `hist()`, `image()`: histogram and heatmap
- `heat.colors()`, `topo.colors()`, etc: create a color vector
- `density()`: estimate density, which can be plotted
- `contour()`: draw contours, or add to existing plot

Scatter plot
===

To make a scatter plot of one variable versus another, use `plot()`

```{r}
n = 50
set.seed(0)
x = sort(runif(n, min=-2, max=2))
y = x^3 + rnorm(n)
plot(x, y)
```

Type
===

The `type` argument controls the type. Default is `p` for points; set it to `l` for lines; `b` for both; or `o` for both, overplotted

```{r}
plot(x, y, type="p")
plot(x, y, type="l")
plot(x, y, type="b")
plot(x, y, type="o")
```

Labels
===

The `main` argument controls the title; `xlab` and `ylab` are the x and y labels

```{r}
plot(x, y, main="A noisy cubic") # Note the default x and y labels
plot(x, y, main="A noisy cubic", xlab="My x var", ylab="My y var")
```

Point type 
===

Use the `pch` argument to control point type

```{r}
plot(x, y, pch=21) # Empty circles, default
plot(x, y, pch=20) # Filled circles, small
plot(x, y, pch=19) # Filled circles, normal
```

Line type
===

Use the `lty` argument to control the line type, and `lwd` to control the line width

```{r}
plot(x, y, type="l", lty=1) # Solid line, default
plot(x, y, type="l", lty=2) # Dashed line
plot(x, y, type="l", lty=3) # Dotted line
plot(x, y, type="l", lwd=3) # Solid line, 3 times as thick
```

Color
===

Use the `col` argument to control the color; can be 1 through 8 for basic colors, or a string for any of the 657 available named colors. The function `colors()` returns a string vector of the available colors

```{r}
plot(1:9, 1:9, pch=19, col=1) # Black, default
plot(1:9, 1:9, pch=19, col=2) # Red
plot(1:9, 1:9, pch=19, col=1:9) # Color vector
plot(1:9, 1:9, pch=19, col="mediumorchid")
```

Multiple plots
===

To set up a plotting grid of arbitrary dimension, use the `par()` function, with the argument `mfrow`. Note: this will affect all following plots! (Except in separate R Markdown code chunks ...)

```{r}
par(mfrow=c(2,2)) # Grid elements are filled by row
plot(x, y, main="Red cubic", pch=20, col="red")
plot(x, y, main="Blue cubic", pch=20, col="blue")
plot(rev(x), y, main="Flipped green", pch=20, col="green")
plot(rev(x), y, main="Flipped purple", pch=20, col="purple")
```

Margin
===

Default margins in R are large (and ugly); to change them, use the `par()` function, with the argument `mar`. Note: this will affect all following plots! (Except in separate R Markdown code chunks ...)

```{r}
par(mfrow=c(2,2), mar=c(4,4,2,0.5))
plot(x, y, main="Red cubic", pch=20, col="red")
plot(x, y, main="Blue cubic", pch=20, col="blue")
plot(rev(x), y, main="Flipped green", pch=20, col="green")
plot(rev(x), y, main="Flipped purple", pch=20, col="purple")
```

Saving plots
===

Use the `pdf()` function to save a pdf file of your plot, in your R working directory. Use `getwd()` to get the working directory, and `setwd()` to set it

```{r}
getwd() # This is where the pdf will be saved
pdf(file="noisy_cubics.pdf", height=7, width=7) # Height, width are in inches
par(mfrow=c(2,2), mar=c(4,4,2,0.5))
plot(x, y, main="Red cubic", pch=20, col="red")
plot(x, y, main="Blue cubic", pch=20, col="blue")
plot(rev(x), y, main="Flipped green", pch=20, col="green")
plot(rev(x), y, main="Flipped purple", pch=20, col="purple")
graphics.off()
```

Also, use the `jpg()` and `png()` functions to save jpg and png files