```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```

Welcome to the Correlations and basic visuals workshop - the 5th workshop in the Basic coding track! 

# Links to Files and Video Recording

The files for all tutorials can be downloaded from [SIPPS website](https://columbia-sipps.github.io/workshops/){target="_blank"}. The recordings from this session will also be posted on the same site. 

# Goals of this lesson

Students will learn:

1. Intro to the ggplot package and how to use it 
2. How to construct some common plots to inspect data: namely, histograms, bar plots, and scatter plots
3. How to compute (and visualize) the correlation between two variables

# Inspecting a dataset 

So far, we've seen how to clean, rename and filter data in a way that prepares it for further analyses (such as modeling). But one important step before we move into that is learning how to visualize your data. Plots are critical for helping you look at the data, allowing you to visually inspect for any outliers or errors, assess the shape of your data distributions, and evaluate whether the data looks "clean" or if needs any additional cleaning or transformations. 

First, let's load in the dataset `tooth`. This is one of the datasets that comes built in with `datasets` package in base R.

```{r}

# because this is a built-in dataset, we don't have to use "load" or any of the "read" functions
# we do have to store it in a variable though so we can manipulate it
```

Next, let's inspect the dataset to see what it contains. You can use the`?` operation before the name just like you can for functions (since this is a built-in dataset).

```{r}

# be sure to use the dataset name and not your variable name!
```

Now we see some description about the dataset. Essentially, it's an experiment in guinea pigs that is looking at the relationship between dose of vitamin C and growth of tooth.

Let's look at the data in more detail.

```{r}

```

```{r}

```

If you looked at the help using the `?` workshop, you'll find out that the `supp` column stands for supplement type - whether the guinea pig received orange juice (OJ) or ascorbic acid (VS). You will also see that dose could be one of three doses of OJ or VS - 0.5, 1 or 2 mg/day. 

You can also see this in full detail using the `View()` function which will open your dataframe in a new pop-up.

```{r}

```

Let's say the first thing we want to see is the range of the tooth length values and what the distribution looks like. The simplest way to do this is using the histogram function. 

```{r}

```

Ok, what do you notice looking at this plot? 

We can see that it ranges from 0 to approx 35 (we don't know if the max is 35, because that is just a bin from 30-35). We can see that it doesn't look perfectly normal.

We can also confirm the exact range of these values using the `range()` function.

```{r}

```

Now imagine we want to change the colors of our plot, and to change the number of bins we're using.

```{r}

```

Oops! This only has 2 breaks, which means there are only 2 bins. That's not what we intended to do. So let's try different values of breaks.

```{r}

```

That's better. Now let's add a title to the plot, and change what the x axis is called so it's more descriptive.

```{r}

```

One thing you might have noticed here is that each time we wanted to change one aspect of the visualization, we had to basically rewrite the same function, adding a lot more arguments. If you really wanted to customize all the aspects of a plot, you might be able to imagine how complicated this could get. Your function would get really long with so many more arguments. 

# Enter the `ggplot2` package of `tidyverse`

The first few code chunks will return results that look exactly like the plots above, but with ggplot, you can do A LOT more visually. This is what I ALWAYS use. Base R can be good when you are trying to get a quick and dirty visual of your data, but if you are looking to actually use any visuals in say a presentation or a paper, you want to use ggplot.

- you'll use the `ggplot()` command to start building your plot

- plots can be saved into variables with `a <- ggplot()`

- the general format of this function is `ggplot(data, aes(x = [x axis variable], y = [y axis variable])` 
    - x and y variables are always specified in this `aes()` subfunction
    - some types of plots may require additional `aes()` arguments, may only need an x variable, etc.

First things first,  let's load the package `tidyverse`

```{r}

# we could also load in just the ggplot2 package using library(ggplot2)
# however, it typically makes sense to load the entire tidyverse since we'll also be cleaning and analyzing our data
```

Now run this line of code:

```{r}

```
  
  What do you notice? 
  
  * first, axes are set up the way we'd expect, and seem to have sensible values
  * however, why is nothing on this graph yet? we haven't put any graphic actually on the axes yet!

**We need to tell the `ggplot()` call what kind of graphic to put on the axis!**

  * a lot of the time, the syntax we'll use to add elements to our plot is `geom_[something]`

## Histograms

So let's try building a histogram with `geom_histogram()`: 

```{r}

```

You'll see that it's much more easier to customize using ggplot (and as you will see later they are way prettier plots than base R).

```{r}

```

You can also add titles and axis labels just like we did above (but in a manner that's easier to read). You can use the function `labs()` to give the plot a title, label your axes, and more. 

```{r}

```
You can use the `bins` argument to change the number of bins (like breaks above). Try different values of bins and see how it changes your plot!

```{r}

```

## ggplot themes

As we've mentioned, ggplot allows a great deal of flexibility in terms of modifying the style and appearance of your plots. One obvious adjustment you might want to make to your plots is changing the colors, which we've already introduced.

However, beyond the basic, nameable colors (e.g., "red", "blue"), you can tell ggplot to use a huge number of built-in colors (see this page: http://sape.inf.usi.ch/sites/default/files/ggplot2-colour-names.png), or even enter a custom hex code (e.g., #ffffff = white).

```{r}

```

ggplot also includes many built-in **themes**, which change elements such as your plot's grid lines, borders, and so on. (You can also create and save your own custom themes, for even more flexibility!)

```{r}
# to easily see how themes work, let's start by saving our current plot into a variable (called myplot)

# now you can see the plot by calling the name of this variable

# let's try some themes!

```

Check out this link for more comprehensive info about all the themes ggplot has to offer: https://ggplot2.tidyverse.org/reference/ggtheme.html

# Bar plots

Now that we've learned a little more about plot customization, let's see what other kinds of visualizations we can make! Histograms are generally great for visualizing distributions of individual variables, but they're not great for telling us about the relationship between two (or more) variables.

For this kind of purpose, one of the most commonly-used types of plots is something you've probably seen before: a bar plot. Bar plots are great for looking at how values of one outcome variable (e.g., tooth length) differ as a function of a variable that can be split into discrete categories (e.g., dose levels).

```{r}
# our plot again needs to start with the ggplot() function
# but this time we'll use geom_bar() to build on top of it
# notice how we need to specify a y variable in the aes() function, since bar plots need both an x and y


# NOTE: we're using the function as.factor() around the dose variable to make sure ggplot treats it as 
# categorical, rather than continuous

# if we add a fill/color specification in the aes() function, this allows us to make our bars differ
# for different levels of our variable! for example:

```

Bar plots can also help us visualize how an outcome variable (y) differs based on two different categorical variables. Let's say we want to see how tooth length relates to both dosage and supplement type (VC or OJ).

All we need to do in our plot is choose one of these variables to go on our x-axis, and the other to be depicted in the *fill* of each bar.

```{r}

# note that there's no need to call as.factor() around the `supp` variable, since it's already categorical
# also note that we need the position = position_dodge() argument to make sure that our 2 bar types (one
# for OJ, one for VC aren't stacked on top of each other)
```

# Scatterplots

Scatterplots are very useful for looking at the relationship between two **continuous** variables. Our ToothGrowth dataset doesn't really have two of these, so let's load in another to look at these kinds of plots.

We'll load in the `mtcars` dataset, which tracks several different performance metrics for different kinds of cars.

```{r}

```

Let's say we're interested in looking at how miles per gallon (`mpg`) relates to the weight of the car (`wt`).

To bulid a scatterplot, we can use the `geom_point()` function.

```{r}

```

Unsurprisingly, we can do a lot of customization to this plot, if we want! Let's do a few things:

- change the limits of our y-axis, so the bottom of the plot starts at 0 mpg
- make the points a different color
- make the points a different size
- make the points slightly transperent
- change the theme

```{r}

# NOTE: the order that you add things (with the + sign) to a ggplot object typically doesn't matter!
# the only time it *does* matter is if you're building successive plot elements on top of each other
# for example, if you want to overlay individual data points on top of a bar plot, you want to
# add the geom_point() function *after* the geom_bar() function
```

## Quantifying our scatterplot with correlations

From the plot above, we can see that our two variables probably have a meaningful relationship to each other: cars that weigh more seem to have lower miles per gallon. But how do we actually quantify this effect? This is where **correlations** come in as a useful statistical tool.

Correlations, generally speaking, measure how strongly two (continuous) variables co-vary with each other. Positive correlations indicate that as one variable increases, so does the other. Negative correlations indicate that as one variable increases, the other decreases. All correlation values (also called r values) exist in a range between -1 and 1, with 0 indicating *no* reliable relationship between one variable and the other.

Obligatory warning that a significant correlation **does not** mean that one variable causes the other. In other words, while it could be that cars have lower MPG values **because** they are heavier, that doesn't necessarily need to be true. For example, maybe there's some other feature that certain cars have that leads to both more weight and lower efficiency.

Computing correlations in R, thankfully, is very easy using the `cor.test()` function.

```{r}
# look at the correlation between mpg and wt

# you can also compute the correlation value alone, without assessing significance, with the cor() function

```

We can see from the p-value here that this correlation is highly significant (p < 0.001), with a correlation (r) value of -0.87. This reaffirms what we saw in our scatterplot: as wt goes up, mpg goes down.

## Visualizing the line of best fit

When plotting correlations, many researchers will include a "line of best fit", which just refers to the straight line that best characterizes the relationship between your two variables, given your data. This line is technically calculated by running a linear regression analysis (not a correlation — although when looking at only two variables, the two analyses are highly related to each other).

You'll cover linear regression in more depth starting next week, but lucky for us, ggplot has a built in function that creates this line very easily!

```{r}

```

# More about ggplot

In a single workshop, we can only scratch the surface of what can be done with ggplot! There are many many more kinds of plots and customization options to explore, which you can choose based on your visualization needs. Always feel free to get in touch with your mentor(s) + instructor(s) if you have questions about what plots are best suited for your data, or if you have any other questions.

In the meantime, here are some initial resources to check out:

- ggplot cheat sheet: https://www.maths.usyd.edu.au/u/UG/SM/STAT3022/r/current/Misc/data-visualization-2.1.pdf
- ggplot gallery: https://www.r-graph-gallery.com/ggplot2-package.html
- more visualization examples: http://r-statistics.co/Top50-Ggplot2-Visualizations-MasterList-R-Code.html

# Data challenge!

Go to the file challenge.Rmd file (download from the SIPPS website) for your data challenge. Remember to use the help command in R (?<function name>) and google as necessary!
