---
title: "Graphics in R"
output:
  tufte::tufte_html: 
    tufte_features: ["fonts",  "italics"]
  tufte::tufte_handout: default
---

```{r setup, include=FALSE,echo=FALSE}
knitr::opts_chunk$set(echo = TRUE)
library(ggplot2)
library(dplyr)
library(readr)
library(tidyr)
library(gridExtra)
library(tufte)
library(GGally)

```


# A bit of backgroung on ggplot2

The package ```ggplot2``` is "an open source implementation of the grammar of graphics for R^[Wickham [The Layered Grammar of Graphics](http://vita.had.co.nz/papers/layered-grammar.pdf)]." and it is often the go-to tool for graphics in ```R```. There are a number of resources on how to use ```ggplot2```^[One place to start might be [Chapter 3](https://r4ds.had.co.nz/data-visualisation.html) of [R for data science](https://r4ds.had.co.nz/index.html), co-written by the author of ggplot; [Chapter 3](https://web.stanford.edu/class/bios221/book/03-chap.html) in S. Holmes & W. Huber "Modern Statistics for Modern Biology" gives an introduction to ```R``` graphics and ```ggplot```, reaching some fairly advance places]. To be totally honest with you, I do not think that ```ggplot2``` is perfect, but it is powerful and once you have a good command of it---having learned how to play with its functions---you can make really high quality graphs. The trick, as with any respectable tool, is to learn how to use it well, so that it enables you to do what you want, rather than being limited by its constraints.^[Laurel Stell's [webpage](http://web.stanford.edu/~lstell/) hosts materials from two short courses on graphics in R: [Intro to ggplot2](http://web.stanford.edu/~lstell/ggplot2Intro.pdf) and [Advanced graphics in R](http://web.stanford.edu/~lstell/AdvancedGraphics.pdf). You will find it useful that the ```.Rmd``` files used to create these presentations are available. These are good examples of what it means to master a tool.] 

   ```ggplot2``` is part of the ```Tidyverse```^[https://www.tidyverse.org], "an opinionated collection of R packages designed for data science." The packages are very useful, very popular^[H. Wickham received the [COPSS award](https://imstat.org/2019/09/02/copss-presidents-award-hadley-wickham/) in 2019], with rather difficult naming conventions and, in my opinion, not enough examples in the help entries. ```ggplot2```, with its layered structure, where different elements of the displays are combined with "+", allows you to obtain  sophisticated graphics with remarkably little effort. This is both a good and a bad thing. It is good to be able to get to the finishing line quickly. But "professional looking" graphics are not necessarily good graphics. You still have to think carefully about what variables your are plotting and which display you are using---a "good look" might make us think we have arrived, when we are still at the beginning. Another downside is the price one has to pay for having a program that "understands so quickly what you want": if what you want is not what the programs is written to understand, it might take a little arm-wrestling to re-direct succesfully. 

I personally love the magic that happens when you add ``` + facet_grid(...)``` to the current plot: ```ggplot2``` is great for *small multiples*. In contrast, I do not like the *default color scale*: unless you have very few items, it becomes very difficoult to separate them out^[This [website](http://www.stat.columbia.edu/~tzheng/files/Rcolor.pdf) gives a useful display of color options in R]. The default theme, which equippes every graph with a gray background, might be appropriate for screen viewing, but it is not effective for printing or slide displays, definitely lowering the *data-ink ratio*.
The following section is a quick intro to ```ggplot2``` to get you started.

# Exploring ggplot

In order to  understand how the plotting commands work, we are going to use one dataset that is available in R: ``mtcars``
 The data was extracted from the 1974 Motor Trend US magazine, and comprises fuel consumption and 10 aspects of automobile design and performance for 32 automobiles (1973–74 models). If you run the following chunk, a more precise description will appear in your help window.

```{r }
help(mtcars)
```

Let's take a look at the summaries of the data.
```{r}
head(mtcars)
summary(mtcars)
```

As you can see, the names of the variables is very abbreviated. Most importantly, all variables are interpreted as numeric, while some are not. To avoid confusions, let's change the variables that are not to be interpreted as real numbers to factors or ordered factors (the number of cylinder is a number, but it does not make sense to talk about a car with 6.188 cylinders)


Let's make things a little more meaningful
```{r}
mmtcars <- within(mtcars, {
   vs <- factor(vs, labels = c("V-shaped", "Straight"))
   am <- factor(am, labels = c("Automatic", "Manual"))
   cyl  <- ordered(cyl)
   gear <- ordered(gear)
   carb <- ordered(carb)
})

summary(mmtcars)
```

Let's also note that we have information on the car names (in the row names), but it is a bit hidden. To make this more visible, let's turn this into one factor in the dataframe

```{r}
mmtcars<-data.frame(row.names(mtcars),mmtcars)
names(mmtcars)[1]<-"Model"
mmtcars
```

## Plot creation and geometries in the grammar of graphics



The function ``ggplot()`` is used to construct a plot incrementally, using the ``+`` operator to add layers to the existing `ggplot` object. 

This is advantageous in that the code is explicit about which layers are added and the order in which they are added. The layers that we add are geometric objects, or "geoms". 

You can think of the call ``ggplot`` as taking out a piece of paper and of the call ``geom_something`` as drawing something on the piece of paper. For example, ``geom_bar`` draws a bar plot. 

The first argument in ``ggplot`` is the data that is going to be visualized. We also need to describe which among the variables in the dataset are used to create the visualization. We do this with ``aes()``, which identifies the variables and on which axes they will be displayed. 
This can be included in the initialization ``ggplot(data=..., aes(...))`` or in the geometric object ``geom(aes(...))``.

Once this basic plot is created, many layers of refinement can be added on top of it, using the operator ``+`` followed by appropriate commands.



So, let's start by telling ``R`` that we want to create a plot using the data in ``mmtcars``.

```{r }
  ppp<-ggplot(mmtcars) 
```
Note that nothing happens. Let's try to look at the object ``ppp``

```{r}
ppp
```
Indeed, not much different from pulling out a blank page with the intention of drawing on it. 

We are now going to look at some of the ``geometries`` available. Note that there are often options within these geometries that we will not explore. We will, however, often introduce some modifications to get a sense of the flexibility we have. 

```{r }
  ppp + geom_histogram(aes(x = wt))
```

We obtain a histogram. The bins are quite narrow, and, indeed, if you run this chunk you would get a warning message suggesting you to use a better bin choice. Here is an attempt.

```{r }
  ppp + geom_histogram(aes(x = wt),binwidth=0.5)
```

Here is another way of displaying the data relative to one **quantitative variable**. 

```{r }
  ppp + geom_density(aes(x = wt))
```
The information displayed is similar...we can think of the density plot as what we get from the histogram as the bin widths grow small, assuming that the function is smooth. 
In the spirit of learning how to add layers to our plot, lets' modify the axis labels. 

```{r }
  ppp + geom_density(aes(x = wt)) + labs(x="Weight")
```

Here is yet another plotting choice for the same type of information. In addition to the label, we also choose a different overall "style" for the plot, selecting a black and white theme that is much more appropriate for printing and projecting. We are going to try to add meaningful modifications to all the remaining plots. While to save space we are going to introduce them directly, the best way to see what they do is to run comment them out from the r-chunk (which you can do putting a ``#`` in front of the ``+ option``)

```{r}
  ppp + geom_dotplot(aes(x = wt)) + labs(x="Weight") + theme_bw()
```

The box plot, drawn with the geometry ``geom_boxplot`` is a very handy way of displaying the same information. Note that this time we add a title. Notice that we also modify the r chunk to change the size of the figure. The effect is best viewed in the knitted file, but roughtly, ``fig.width=1.5,fig.height=3`` act on the ratio of height to width and the size of plotting characters and fonts, while ``out.width=200`` specifies the actual size of the image

```{r ,fig.width=1.5,fig.height=3,out.width=200}
  ppp + geom_boxplot(aes(y = wt)) + labs(y="Weight",title="Boxplot") + theme_bw()
```


Let's now think about how to display a **qualitative variable**, as the number of cylinders.

```{r }
  ppp + geom_bar(aes(x=cyl)) + labs(x="# cylinders",title="Barplot") + theme_bw()
```
Now, let's look at some extra features we can add. We can ask for the coloring of the bars to reflect the proportions of the cars with manual vs automatic transmission. To do this, we modify the ``aestetics`` indicating that the bars have to be 'filled' with color that represent the variable 'am'. Note that this will automatically create a legend. We can further modify the ``lab`` elements to put a clearer title to the legend (try removing ``fill="Transmission"`` from ``labs()`` to see what happens)

```{r }
  ppp + geom_bar(aes(x=cyl, fill = am)) +labs(x="# cylinders",title="Barplot",fill="Transmission")  + theme_bw() 
```

Note that you can also change the colors that are used in the display (indicentally the choice of red and green is not a great one, given its lack of visibility for color blind people).
To do this you need to specify a different ```scale``` for the ```fill```.

```{r }
  ppp + geom_bar(aes(x=cyl, fill = am)) +labs(x="# cylinders",title="Barplot",fill="Transmission")  + theme_bw() +scale_fill_manual(values=c("dodgerblue2","gold"))
```

In other graphs, where the color displayed is determined by the the ```col``` variable in an ```aes()```, you will need to use ```scale_color_manual``` (see below for an example).

Sometimes it might be useful to disply also a quantitative variable with a bar plot. Typically this is the case when you want to emphatise the identity of the points. In the plot below we look at mpg per car model. Note that we rotate the labels for the names, so that they are legible. We also reduce a bit the size of the fonts (with the option ``base_size=8`` in ``theme_bw()``).

```{r,out.width=800}
ppp+geom_col(aes(x=Model,y=mpg,fill=am))+theme_bw(base_size=8)+labs(x="Car Model",y="Mile per galon",title="Column plot",fill="Transmission")+ theme(axis.text.x = element_text(angle = 90, hjust = 1,vjust=0.5)) 

```
In cases like these, however, an often better option is to simply rotate the plot

```{r,out.width=800}
ppp+geom_col(aes(x=Model,y=mpg,fill=am))+theme_bw(base_size=8)+labs(x="Car Model",y="Mile per galon",title="Column plot",fill="Transmission")+ coord_flip()

```


The  plot we most commonly use to  assess the relation between two quantitative variables is the scatter plot. To do this, we use the geometry ``geom_point``.

```{r }
  ppp + geom_point(aes(x = wt,y=mpg)) + labs(x="Weight",y="Miles per gallon", title="Fuel Efficiency") + theme_bw()
```

Note that we can change the color for the points or plotting symbols (without associating them to any variables). And let's also note how you can put a line or a smooth function through the data. 

```{r}
  ppp + geom_point(aes(x = wt,y=mpg),shape=4,col="red") + labs(x="Weight",y="Miles per gallon", title="Fuel Efficiency") + theme_bw() + geom_smooth(aes(x = wt,y=mpg), method = "lm")
```
 If we want shape and/or color of the points to represent an additional variable, we need to call this specification in the ``aes()`` function that identifies all the important variables for the plot. In the following plot, we use color to track the number of cylinders, and size to represent the horse power. We also make the points a bit transparent to make sure that readability is conserved when the points overlap (this happens using ```alpha```). Finally notice how in the ```r``` chunck specification we have elements that describe the aspect ratio and size of the graph. You should play with all of these and see how they change your outcome.
 
```{r, fig.width=7,fig.height=4,out.width=800}
  ppp + geom_point(aes(x=wt, y=mpg, size=hp, col=cyl),alpha = 0.7) +
  labs(x="Weight", col="# Cylinders", size="Horse Power", 
       title="Fuel Efficiency", subtitle="Source: mtcars data set") + theme_bw()
 
```
 
Note that you could use the ``geom_point`` also when one of the variables is a factor. 

```{r,out.width=700,fig.height=6, fig.width=4}
ppp+geom_point(aes(x=Model,y=mpg,col=am,size=wt,shape=cyl))+theme_bw(base_size=8)+labs(x="Car Model",y="Miles per galon",col="Transmission",size="Weight",shape="# cylinders")+ coord_flip()+ scale_color_manual(values=c("dodgerblue2","gold"))

```
 
We are now going to see how box plots come in handy to compare multiple distributions. We use ``geom_boxplot()`` and we are going to specify an ``x`` in the ``aestetics`` as well.

```{r }
  ppp + geom_boxplot(aes(x = cyl,y=mpg))+ labs(x="# cylinders",y="Miles per gallon", title="Fuel Efficiency") + theme_bw() 
```

Violin plots are a similar display,  which is essentially a density plot turned on its side, and reflected about the vertical axis.


```{r }
  ppp + geom_violin(aes(x = cyl,y=mpg))+ labs(x="# cylinders",y="Miles per gallon", title="Fuel Efficiency") + theme_bw() 
```

Sometimes, we might want to change direction of display. We can use ``coord_flip()`` for this.

```{r }
  ppp + geom_violin(aes(x = cyl,y=mpg))+ labs(x="# cylinders",y="Miles per gallon", title="Fuel Efficiency") + theme_bw() +coord_flip()
```


One of the most powerful feature of ``ggplot`` is the facility to create "small multiples" plots. To do this we use ``facet``.

In ``facet_grid``, the argument we use is a formula with the rows (of the tabular display) on the LHS and the columns (of the tabular display) on the RHS. A formula is separated in ``R`` with the tilde character ``~``.  A dot in the formula is used to indicate there should be no faceting on this dimension (either row or column). 
```{r }
  ppp + geom_histogram(aes(x = hp),binwidth=10)+ facet_grid(cyl ~ .) + labs(x="Horse Power", title="Exerimenting with Facets") + theme_bw()
```

Note how the following graph implements the idea of ``small multiples'', maximizing data ink and taking advantage of the gestalt principle of symmetry. 
```{r , fig.width=5,fig.height=5}
  ppp + geom_point(aes(x = hp,y=mpg))+ facet_grid(cyl ~ as.factor(am)) + labs(x="Horse Power",y="Miles per gallon", title="Fuel Efficiency, Facets version") + theme_bw()
```

To add on to this overview of tools available in R to look at the relations between variables in a small multiple fashion, it is useful to look at ``ggpairs``. Now, the figure that the command below creates might be a little overwhelming, but you do not necessarily have to use it for all the variables at the same time.

It is useful to see how it treats differently quantitative vs qualitative variables and how you can represent their dependence. Note that some variables, like cylinders are now coded as ordered, but one could argue for them to be quantitative, at least for this display.


```{r , fig.width=14,fig.height=14,warnings=FALSE,fig.fullwidth = TRUE,message = FALSE}
  ggpairs(mmtcars[,c(-1)])
```



For the sake of comparison, let's take a look at how the plot changes if we revert back to the original variable codings.
```{r , fig.width=14,fig.height=14,warnings=FALSE,fig.fullwidth = TRUE,message = FALSE}
  ggpairs(mtcars)
```

To have a little more visibility, we can look at a subset of variables

```{r , fig.width=6,fig.height=6,warnings=FALSE,message = FALSE}
  ggpairs(mmtcars[,c("mpg","cyl","disp","vs")])
```

Finally, you want to be aware that, while facets have some advantages in optimizing scales and symmetries, they are not the only way of putting multiple plots in the same graphical window.

You can use ``grid.arrange`` to place multiple plots within the same window. For a very basic example, let's put together a couple of graphs we already created. And you can find more examples [here](https://cran.r-project.org/web/packages/egg/vignettes/Ecosystem.html)

```{r }
p1<- ppp + geom_boxplot(aes(x = cyl,y=mpg))+ labs(x="# cylinders",y="Miles per gallon", title="Fuel Efficiency") + theme_bw() 
p2<-ppp + geom_histogram(aes(x = hp),binwidth=10)+ facet_grid(cyl ~ .) + labs(x="Horse Power", title="Exerimenting with Facets") + theme_bw()
grid.arrange(p1,p2,nrow=1)
```