```{r, include = FALSE}
knitr::opts_chunk$set(fig.width=6, fig.height=4, fig.path = "fig/ggplot-")
```

# Plotting with ggplot2

We already saw some of R's built in plotting facilities with the function `plot`. A more recent and much more powerful plotting library is `ggplot2`. This implements ideas from a book called "The Grammar of Graphics". The syntax is a little strange, but there are plenty of examples in the [online documentation](http://ggplot2.tidyverse.org/reference/).

If `ggplot2` isn't already installed, we need to install it.

```{r, eval=FALSE}
install.packages("ggplot2")
```

We then need to load it.

```{r, warning=F}
library(ggplot2)
```

Producing a plot with ggplot2, we must give three things:

1. A data frame containing our data.
2. How the columns of the data frame can be translated into positions, colors, sizes, and shapes of graphical elements ("aesthetics").
3. The actual graphical elements to display ("geometric objects").


## Using ggplot2 with a data frame

We will be using data from [Gapminder](https://www.gapminder.org/) on life expectancy over time in different countries. Load it with:

```{r}
gap <- read.csv("r-intro-files/gapminder.csv")

head(gap)
```

Let's make our first ggplot.

```{r}
ggplot(gap, aes(x=year, y=lifeExp)) +
    geom_point()
```

The call to `ggplot` and `aes` sets up the basics of how we are going to represent the various columns of the data frame. `aes` defines the "aesthetics", which is how columns of the data frame map to graphical attributes such as x and y position, color, size, etc. We then literally add layers of graphics to this.

You may notice that `aes` does something very odd, since its bare arguments refer to columns of the data frame as though they were variables. R functions sometimes perform [magic tricks](http://adv-r.had.co.nz/Functions.html#lazy-evaluation) like this for the sake of allowing concise expressive code.

Further aesthetics can be used. Any aesthetic can be either numeric or categorical, an appropriate scale will be used.

```{r}
ggplot(gap, aes(x=year, y=lifeExp, color=continent, size=pop)) +
    geom_point()
```

> ### Challenge {.challenge}
>
> This R code will get the data from the year 2007:
>
> ```{r}
> gap2007 <- gap[ gap$year == 2007 ,]
> ```
>
> Create a ggplot of this with `gdpPercap` on the x-axis and `lifeExp` on the y-axis.
>


### Further geoms

To draw lines, we need to use a "group" aesthetic.

```{r}
ggplot(gap, aes(x=year, y=lifeExp, group=country, color=continent)) +
    geom_line()
```

A wide variety of geoms are available. Here we show Tukey box-plots. Note again the use of the "group" aesthetic, without this ggplot will just show one big box-plot.

```{r}
ggplot(gap, aes(x=year, y=lifeExp, group=year)) +
    geom_boxplot()
```

`geom_smooth` can be used to show trends.

```{r}
ggplot(gap, aes(x=year, y=lifeExp)) +
    geom_point() +
    geom_smooth()
```

Aesthetics can be specified globally in `ggplot`, or as the first argument to individual geoms. Here, the "group" is applied only to draw the lines, and "color" is used to produce multiple trend lines:

```{r}
ggplot(gap, aes(x=year, y=lifeExp)) +
    geom_line(aes(group=country)) +
    geom_smooth(aes(color=continent))
```


### Highlighting subsets

Geoms can be added that use a different data frame, using the `data=` argument.

```{r}
australia <- gap[ gap$country == "Australia" ,]

ggplot(gap, aes(x=year, y=lifeExp, group=country)) +
    geom_line() +
    geom_line(data=australia, color="red", size=2)
```

Notice also that the second `geom_line` has some further arguments controlling its appearance. These are **not** aesthetics, they are not a mapping of data to appearance, rather they are direct specification of the appearance. There isn't an associated scale as when color was an aesthetic.


### Fine-tuning a plot

Adding `labs` to a ggplot adjusts the labels given to the axes and legends. A plot title can also be specified.

```{r}
ggplot(gap, aes(x=year, y=lifeExp)) +
    geom_point() +
    labs(x="Year", y="Life expectancy", title="Gapminder")
```

Type `scale_` and press the tab key. You will see functions giving fine-grained controls over various scales (x, y, color, etc). Limits on the scale can be set, as well as transformations (eg log10), and breaks (labelled values).

Suppose we want our y-axis to start at zero.

```{r}
ggplot(gap, aes(x=year, y=lifeExp)) +
    geom_point() +
    scale_y_continuous(limits=c(0,100))
```

The `lims` function can also be used to set limits.

Very fine grained control is possible over the appearance of ggplots, see the ggplot2 documentation for details and further examples.


> ### Challenge {.challenge}
>
> Continuing with your scatter-plot of the 2007 data, add axis labels to your plot.
>
> Advanced: Give your x axis a log scale (see the documentation on `scale_x_continuous`, specifically the `trans` argument).
>


## Using ggplot2 with a matrix

Let's return to our first matrix example.

```{r}
dat <- read.csv(file="r-intro-files/pvc.csv", row.names=1)
mat <- as.matrix(dat)
```

`ggplot` only works with data frames, so we need to convert this matrix into data frame form, with one measurement in each row. We can convert to this "long" form with the `melt` function in the library `reshape2`.

```{r warning=F}
library(reshape2)
long <- melt(mat)
head(long)
colnames(long) <- c("resin","operator","value")
head(long)
```

```{r}
ggplot(long, aes(x=operator, y=value, group=resin, color=resin)) + 
    geom_line()
```

Notice how `ggplot` is able to use either numerical or categorical (factor) data as x and y coordinates.

We have shown the entire data set as an "interaction plot". We can directly see the whole story this data has to tell. When it is possible to plot an entire data set, this should be the first step before any summarizing and statistical testing. Even if there is too much data to plot in its entirety, it is always possible to plot a random subset.


## Faceting

Faceting lets us quickly produce a collection of small plots. The plots all have the same scales and the eye can easily compare them.

```{r}
ggplot(long, aes(x=operator, y=value)) + 
    geom_point() +
    facet_wrap(~ resin) 
```

```{r warning=F}
diabetes <- read.csv("r-intro-files/diabetes.csv")

ggplot(diabetes, aes(x=age, y=glyhb, color=location)) +
    geom_point() +
    facet_grid(frame ~ gender)
```


> ### Challenge {.challenge}
>
> Let's return again to your scatter-plot of the 2007 data.
>
> Adjust your plot to now show data from all years, with each year shown in a separate facet, using `facet_wrap(~ year)`.
>
> Advanced: Highlight Australia in your plot.
>


## Saving ggplots

Ggplots can be saved as we talked about earlier, but with one small twist to keep in mind. The act of plotting a ggplot is actually triggered when it is printed. In an interactive session we are automatically printing each value we calculate, but if you are using a for loop, or other R programming constructs, you might need to explcitly `print( )` the plot.

```{r, eval=FALSE}
# Plot created but not shown.
p <- ggplot(long, aes(x=operator, y=value)) + geom_point()

# Only when we try to look at the value p is it shown
p

# Alternatively, we can explicitly print it
print(p)

# To save to a file
ggsave("test.png", p)

# or
png("test.png")
print(p)
dev.off()
```


