---
title: Creating Plots
author: YOUR NAME HERE
date: today
format: pdf
execute:
  fig-height: 4
  message: false
  warning: false
  error: true
---

# Creating Plots 

Visualizing your data is hands down the most important thing you can learn to do. Seeing is critical to understanding. There are two audiences in mind when creating data visualizations: 

1. **For your eyes only:** These are quick and dirty plots, without annotation. Meant to be looked at once or twice.
2. **To share with others:** These should have informative captions, axes labels, titles, colors as needed, etc. We'll see how to add these features throughout this course. 

The functions from the `ggplot2` package, along with derivatives such as `ggpubr` and `sjPlot`, automatically do a lot of this work for you. While all of these can be made with base R plotting functions, we are intentionally choosing to highlight function that create good quality plots with very little code and are quite extensible and flexible.


## The syntax of `ggplot`

The reason we use the functions in `ggplot2` is for consistency in the structure of it's arguments. Here is a bare bones generic plotting function: 

```r
ggplot(data, aes(x=x, y=y, col=col, fill=fill, group=group)) +  geom_THING() 
```

### Required arguments 

* `data`: What data set is this plot using? This is ALWAYS the first argument.
* `aes()`: This is the _aesthetics_ of the plot. What variable is on the x, and what is on the y? Do you want to color by another variable, perhaps fill some box by the value of another variable, or group by a variable. 
* `geom_THING()`: Every plot has to have a geometry. What is the shape of the thing you want to plot? Do you want to plot point? Use `geom_points()`. Want to connect those points with a line? Use `geom_lines()`. We will see many varieties in this lesson. 

```{r}
library(________); library(________)    
library(________);  library(________)
pen <- palmerpenguins::penguins
```

## One categorical variable

Both Nominal and Ordinal data types can be visualized using tables, barcharts or pie charts.

### Barchart

A Barchart or barplot takes these frequencies, and draws bars along the X-axis where the height of the bars is determined by the frequencies seen in the table. 

#### `ggplot` 

Using `ggplot2` with the `geom_bar()` geometry layer gives us actual wide bars, and better axis labels. 
```{r}
ggplot(pen, aes(x=species)) + ________
```

#### `sjPlot`

Using the `plot_frq` function from the `sjPlot` package builds on the `geom_bar()` type plot from `ggplot`, but adds frequncies and relative percentages on the plot. 
```{r}
plot_frq(pen, "________")
```

This single graph provides a lot of good information and is a recommended choice to use. 



### Pie charts

A pie chart is a circular statistical graphic which is divided into slices to illustrate percentages out of a whole. While pie charts are very widely used in the media and business, there are some major drawbacks in that "_humans are pretty bad at reading angles_" 

The approach is to pipe the results of a `table` to a `pie` using base R. But the results are kinda "meh". 

```{r}
________ |> pie()
```

Nicer pie charts using `ggplot2` or `ggpubr` functions require the data set to be pre-aggregated, and so we will come back to these approaches in a later lesson. 


## One continuous variable

We will examine the chonkiness of the penguin (`body_mass_g`) using several types of appropriate visualizations including histograms, density plots, boxplots and violin plots. 

### Histogram

Rather than showing the value of each observation, we prefer to think of the value as belonging to a _bin_. The height of the bars in a histogram display the frequency of values that fall into those of those bins. 

Since the x-axis is continuous the bars touch. This is unlike the barchart that has a categorical x-axis, and vertical bars that are separated.


#### `ggplot2`

Using the `ggplot2` package we can create a histogram by adding the layer `geom_histogram()`. 
```{r}
ggplot(pen, aes(x=________)) + ________
```

#### `ggpubr`

In contrast to `ggplot2`'s common starter code and different `geom`etries, the  `ggpubr` package uses specific functions for each type of plot. The `gghistogram` package makes a histogram very similar to the `ggplot2` default, but with a different theme applied (different appearance). Otherwise it's the same. 

```{r}
________(pen, x="________")
```

:::{.callout-important title = "Variables names in quotes"}
This is a feature of `ggpubr` functions - variable names are always in quotes. 
:::

### Density curves

To get a better idea of the true shape of the distribution we can "smooth" out the bins and create what's called a `density` plot or curve. Notice that the shape of this distribution curve is much... "wigglier" than the histogram may have implied. 

#### `ggplot2`

With `ggplot2` we use the `geom_density()` geometry to produce a nicer looking density plot with minimal additional code. 

```{r}
________(________, aes(x=________)) + ________
```

#### `ggpubr`

And the `ggdensity` function from the `ggpubr` package creates a very similar density plot with a different default theme. 

```{r}
________(________, x="________")
```

### Boxplots

Another very common way to visualize the distribution of a continuous variable is using a boxplot. Boxplots are useful for quickly identifying where the bulk of your data lie. R specifically draws a "modified" boxplot where values that are considered outliers are plotted as dots. 

#### `ggplot2`

With `ggplot` you can create either a horizontal or vertical boxplot by specifying your numeric variable to be on either `x` or `y` . Notice the middle of the box is centered on 0, this is just a placeholder. This axis has no inherent meaning. 

```{r}
#| layout-ncol: 2
________(pen, aes(x=________)) + ________ 
```

#### `ggpubr`
You can also make a boxplot using the `ggbpxplot` function from the `ggpubr` package, however it you must specify that the quantitative variable is on the `y` axis otherwise it yells at you. 

```{r}
________(pen, ________="body_mass_g")
```



## Two continuous variables

Visualizing the relationship between two continuous variables is done using a scatterplot. Let's compare the `flipper_length_mm` of a penguin to it's `body_mass_g`. 
#### `ggplot2`

With ggplot we specify both the x and y variables, and add `geom_point` geometry layer. 
```{r}
ggplot(pen, aes(x=________, y=________)) + ________
```

#### `ggpubr`

The `ggscatter` function creates a similar scatterplot. 
```{r}
________(________, x="________", y="________")
```

### Adding trend lines lines

Two most common trend lines added to a scatterplots are the "best fit" straight line and the "loess" (low-ess) smoother line. Adding a trend line to this plot using base R is a bit tricker, so we won't bother.   

#### `ggplot2`

A trend line can be added by adding a `geom_smooth()` layer.

```{r}
ggplot(pen, aes(x=flipper_length_mm, y=body_mass_g)) + 
  ________ + 
  ________
```

Here the point-wise confidence interval for this loess line is shown in grey. If you want to turn the confidence interval off, use `se=FALSE`. 

We can add another `geom_smooth()` layer for the `lm` (linear model) line in blue, and the loess line (by not specifying a method) in red.

```{r}
ggplot(pen, aes(x=flipper_length_mm, y=body_mass_g)) + 
  ________ + 
  geom_smooth(se=________, method="________", color="________") + 
  geom_smooth(se=________, color="________")
```


#### `ggpubr`

You can add _either_ a linear model line _or_ a loess line to a `ggscatter` using the `add=` argument. 
```{r}
#| layout-ncol: 2
________(________, x="________", y="________", add = "________")
```


## One continuous vs. one categorical 

The tactic here is to create an appropriate plot for a continuous variable, and then `fill` the geometric area or `color` the lines depending on the level of the categorical variable.  

### Histograms

Neither `fill`ing or `color`ing the histogram bars using `ggplot2` depending on the group work well due to the overlap. 


#### `ggplot2`

```{r}
ggplot(pen, aes(x=body_mass_g, 
                   ________=species)) + 
  geom_histogram()

```

```{r}
ggplot(pen, aes(x=body_mass_g, 
                   ________________)) + 
  geom_histogram()
```

#### `ggpubr`

The defaults for `gghistogram` automatically adjusts the transparency of the histogram bars to make the overlap a little less troublesome, but it doesn't always work well. 

```{r}
gghistogram(pen, x = "body_mass_g", ________)
```



### Density curves

Similar to histograms, you can `fill` or `color` the density curves depending on the group. 

#### `ggplot-fill`

It's still hard to see some groups due to the overlap, so we adjust the transparency by applying a value to `alpha` inside the `geom_density` layer. Alpha is a measure of transparency, from 0=clear to 1=opaque.

```{r}
ggplot(pen, aes(x=body_mass_g, ________)) + 
  ________
```

#### `ggplot-color`

You could also just color the lines and leave the fill alone. 
```{r}
ggplot(pen, aes(x=body_mass_g, ________)) + geom_density()
```

#### `ggpubr`
  
The `ggdensity` function also has `color` and `fill` options, where the transparency of the density plots are automatically handled. 
```{r}
#| layout-ncol: 2
________(________, x="________", color = "________") 
```



### Boxplots

To create grouped boxplots, put the continuous variable on one axis, and the categorical on the other axis. 

#### `ggplot2`

```{r}
#| layout-ncol: 2
ggplot(pen, aes(x=________, y=________)) + geom_boxplot() # left
ggplot(pen, aes(x=________, y=________)) + geom_boxplot() # right
```

If you want an additional color feature (and the corresponding legend), you can either `fill` or `color` the boxes by the same categorical variable. 

```{r}

```


#### `ggpubr`
Not much difference in the style between the `ggplot2` and `ggboxplot` versions. This method uses _slightly_ less code. 
```{r}
#| layout-ncol: 2
ggboxplot(pen, y="body_mass_g", ________ = "species") 
```



## Two categorical variables

Recall from earlier that frequency tables are a common way to summarize categorical variables, and that both the frequency and relative percent are important summary numbers. Those percentages are even more important when comparing the joint distribution of two categorical variables. 

Cross-tabs, cross-tabulations and two-way tables are different names for the same thing, and can be created by using the `table()` and `tbl_summary` functions. The values in each cell are the number of observations in that combination of characteristics. 

Let's explore the relationship between the penguins sex and species. 

### Frequency and proportion tables

#### base R

The first argument `species` specifies the levels that show on the rows, the second argument  `sex` specifies the columns. 

```{r}
________(pen$________, pen$________)
```

#### `tbl_summary`

To achieve the same ordering with species on the rows and sex on the columns, we `include="species"` and set `by = "sex"`. 
```{r}
________(pen, include = "________", ________)
```


There are 73 female Adelie penguins, and 61 male Gentoo penguins. 

### Proportions

By default, when we ask for proportions we get the **cell** proportions. That is, the percent out of _all_ penguins in that data set that have that combination of traits. The percents add up to 1 across the entire table. 


#### base R

```{r}
________(pen$species, pen$sex) |> ________
```

#### `tbl_summary`

Note that `gtsummary` tables tend to round pretty heavily. 

```{r}
________(________, include = "species", by = "sex", percent = "________")
```

21.9% of all penguins are Adelie females, 18.3% of all penguins are male Gentoo's. 

:::{.callout-note title = "Comparing percentages"}
More often than not, we want to compare percents of one group -- within each level of the other group. For example is the male to female ratio the same for each species? 
:::

### Row percents

To compare the distribution of sex (columns) within each of the sepecies (rows) we need row percentages. The percentages now add up to 1 across the rows and the comparison groups are each species. 
 

#### base R

Specify `margin=1` inside the `prop.table()`
```{r}
________(pen$species, pen$sex) |> ________  |> round(3) 
```

I added a `round` function to the end of this because noone needs that many decimal places. 

#### `tbl_summary`

```{r}
________(pen, include = "________", by = "________", ________)
```


50% _of Adelie penguins_ are male, but 51.3% _of Gentoo penguins_ are male. 


### Column percents

To compare the distribution of species (rows) within each of the columns (sex) we need column percentages. The percentages now add up to 1 down the columns and the comparison groups are male and female. 

#### base R

Specify `margin=2` in `prop.table()`
```{r}

```

#### `tbl_summary`

```{r}

```


44% _of female penguins_ are Adelie species, 36% _of male penguins_ are Gentoo. 


## Stacked bar charts

Sometimes pictures are better than tables, so let's try to apply the same `fill` tactic that we used in the last section. 

```{r}
ggplot(pen, aes(x=________, fill=________)) + ________
```

:::{.callout-important title = "Stacked barcharts are the ggplot2 default"}
Stacked barcharts are generally only useful when comparing percents out of a whole. To get the correct view you can add a `position = "fill"` argument to the `geom_bar()` layer. 
:::

```{r}
ggplot(pen, aes(x=species, fill=sex)) + 
  geom_bar(________) + 
  ylab("________")
```

### Side by side bar charts

#### `ggplot2`

Add the argument `position=dodge` inside the `geom_bar` layer to put the bars side by side. 
```{r}
ggplot(pen, aes(x=species, fill=sex)) + 
 ________
```

#### `sjPlot`

The `plot_xtab` function is the two-way table analogy to `plot_frq` to create a barchart with clear lables on the bars for the N and % (and NA values dropped). Note you have to use dollar sign notation here for the variables. 

```{r}
plot_xtab(x = ________, grp = ________)
```

By default this plots the vertical axis as percents, not counts, and it shows the marginal total for the variable that's on the x-axis. We can remove the total by setting `show.total` to `false`. 

```{r}
________(x = pen$species, grp = pen$sex, 
          show.total = "________")
```


As before, typically we aren't interested in comparing proportions out of the whole, but proportions out of one of the two margins (variables). 


### Comparing Percents

Our eyes make comparisons the best when the bars are physically close to each other. So generally you want to put the groups we want to compare _within_ on the x-axis, and have separate bars for each level of the variable we want to compare _across_. 

See each tab on how to compare the distribution of `sex` within each `species`. This corresponds to the **row** percents. 

#### `ggplot2`

We have to pre-aggregate the data to calculate the grouped percents before we create the plot. Notice how this changes the variable names. Put `species` (`Var1`) on the x-axis,  `fill` by `sex` (`Var2`), and `weight` the `Freq` variable. I also changed the y axis label here to make it more clear that the heights of the bars are a percent.
```{r}
pen.table <- _______(pen$species, pen$sex) |> _______

ggplot(_______, aes(x=_______, fill=_______, weight = _______)) + 
  geom_bar(position = "dodge")  + ylab("Proportion")
```


#### `sjPlot`

Put `species` on the x-axis and `grp` by `sex`, and specify `margin = "row"`. 
```{r}
plot_xtab(x = ________, grp = ________, show.total = "false", 
          ________)
```

:::{.callout-tip title = "👉 Your Turn" icon=false}
Modify the code above to create a plot to compare the distribution of species within each sex. 

**ggplot2 solution**
```{r}

```

**sjPlot solution**
```{r}

```
:::



