% Graphics in R
% Jared P. Lander;Gilt Group
% January 23, 2013

```{r settings,include=FALSE}
#options(width=50)
opts_chunk$set(cache=TRUE,comment=NA,fig.path="figures/",fig.cap="")
```

# The Data
## Diamonds
```{r show-diamonds-data}
require(ggplot2)
data(diamonds)
head(diamonds)
```

## Football
```{r show-football-data}
load("~/football/data/allGames.rdata")
head(allGames[, -11])
```

## Game Descriptions
```{r game-desc}
head(allGames$description, 4)
```

# Base Graphics
## Histogram
```{r base-hist,fig.show='hold'}
hist(diamonds$carat)
hist(diamonds$price)
```

## Scatterplot
```{r base-scatter}
plot(price ~ carat, data=diamonds)
```

## Boxplot
```{r base-box}
boxplot(diamonds$carat)
```

## Boxplot Groups
```{r base-box-mult}
boxplot(carat ~ cut, data=diamonds)
```

# ggplot2
## Histogram
```{r gg-hist,fig.show='hold',message=FALSE}
ggplot(diamonds, aes(x=carat)) + geom_histogram()
ggplot(diamonds, aes(x=price)) + geom_histogram()
```

## Density
```{r gg-density,fig.show='hold'}
ggplot(diamonds, aes(x=price)) + geom_density(fill="grey50", color="grey50")
ggplot(diamonds, aes(x=price)) + geom_density(aes(color=cut, fill=cut), alpha=1/2)
```

## Scatterplot
```{r gg-scatter}
p <- ggplot(diamonds, aes(x=carat, y=price))
p + geom_point()
```

## Log Transformation
```{r gg-scatter-log}
ggplot(diamonds, aes(x=log(carat), y=log(price))) + geom_point()
```

## Scatter with Color
```{r gg-scatter-color}
p + geom_point(aes(color=color))
```

## Scatter Wrapping
```{r gg-scatter-color-wrap}
p + geom_point(aes(color=color)) + facet_wrap(~ cut)
```

## Scatter Wrapping
```{r gg-scatter-color-grid}
p + geom_point(aes(color=color)) + facet_grid(clarity~ cut)
```

## Boxplots
```{r gg-box}
ggplot(diamonds, aes(y=price, x=1)) + geom_boxplot()
```

## Boxplot by Color
```{r gg-box-cut}
ggplot(diamonds, aes(y=price, x=cut)) + geom_boxplot()
```

## Violin Plot
```{r gg-violin,fig.show='hold'}
ggplot(diamonds, aes(y=price, x=cut)) + geom_violin()
ggplot(diamonds, aes(y=price, x=cut)) + geom_point() + geom_violin()
```

## Reshaped Football Data
```{r -load-giants}
load("~/football/data/playMelt.rdata")
head(playMelt)
tail(playMelt)
```

## Play Selection
```{r run-play}
ggplot(playMelt, aes(x=season, y=Percent, group=Play, color=Play)) + geom_line() + facet_wrap(~down, ncol=1, scales="free_y") + ggtitle("Type of Play by Down") + labs(x="Season") + geom_vline(xintercept=c(2007, 2011), color="grey", linetype=2)
```