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

## Plotting in R, continued

Data visualization is a powerful tool for understanding your data and teasing out relationships.  This lab will illustrate how to visualize the distribution of variables as well as associations between variables using base `R` and the `tidyverse` plotting package.

For this lab, we will use two important packages:
- `dplyr` to manage and manipulate our data
- `ggplot2` to actually produce the visualizations

To see all of the cool data visualization that you can do in R, visit the R Graph Gallery:
https://www.r-graph-gallery.com/

```{r, warning = F, message = F}
#install.packages("ggplot2")
#install.packages('palmerpenguins')
library(ggplot2)
library(dplyr)
library(palmerpenguins)
```

The data for this lab can be accessed through a package called `palmerpenguins`, which contains data on penguins' species, island, and body dimensions. This dataset will not actually appear in the global environment when you load the `palmerpenguins` library, but it is still accessible. 

We will start by examining some descriptive statistics for each variable using the `summary()` command.

```{r}
summary(penguins)
```

```{r}
plot(penguins$flipper_length_mm,
     penguins$body_mass_g,
     xlab = "Flipper length (mm)",
     ylab = "Body Mass (g)",
     main = "",
     pch = 2, col = 'red')

pdf('WeightvsHeight.pdf', height = 4, width = 4)
plot(penguins$flipper_length_mm,
     penguins$body_mass_g,
     xlab = "Flipper length (mm)",
     ylab = "Body Mass (g)",
     main = "",
     pch = 2, col = 'red')
dev.off()

#install.packages('scales', dependencies = T)
library(scales)
max(penguins$flipper_length_mm)
max(penguins$flipper_length_mm, na.rm = TRUE)

xlimits <- c(min(penguins$flipper_length_mm,
                  na.rm = TRUE)-1,
              max(penguins$flipper_length_mm,
                  na.rm = TRUE)+1)
ylimits <- c(min(penguins$body_mass_g,
                  na.rm = TRUE)-1,
              max(penguins$body_mass_g,
                  na.rm = TRUE)+1)

plot(penguins$flipper_length_mm[penguins$species == "Adelie"],
     penguins$body_mass_g[penguins$species == "Adelie"],
     xlim = xlimits,
     ylim = ylimits,
     xlab = "Flipper length (mm)",
     ylab = "Body Mass (g)",
     pch = 2, col = 'navyblue')
points(penguins$flipper_length_mm[penguins$species == "Chinstrap"],
       penguins$body_mass_g[penguins$species == "Chinstrap"],
       pch = 19, col = 'deeppink',
       lwd = 2)
points(penguins$flipper_length_mm[penguins$species == "Gentoo"],
       penguins$body_mass_g[penguins$species == "Gentoo"],
       pch = 21, col = 'deepskyblue',
       bg = alpha('deepskyblue', 0.5))
legend('topleft', pch = c(2,19,19), 
       lwd = c(1,2,1), lty = c(NA, NA,NA),
       col = c("navyblue",
               "deeppink",
               alpha("deepskyblue",.5)),
       legend = c("Adelie",
                  "Chinstrap",
                  "Gentoo"), bty = 'n')

par(mfrow = c(1,3))
plot(penguins$flipper_length_mm[penguins$species == "Adelie"],
     penguins$body_mass_g[penguins$species == "Adelie"],
     xlim = xlimits,
     ylim = ylimits,
     xlab = "Flipper length (mm)",
     ylab = "Body Mass (g)",
     main = "Adelie",
     pch = 19, col = 'navyblue')
plot(penguins$flipper_length_mm[penguins$species == "Chinstrap"],
     penguins$body_mass_g[penguins$species == "Chinstrap"],
     xlim = xlimits,
     ylim = ylimits,
     xlab = "Flipper length (mm)",
     ylab = "Body Mass (g)",
     main = "Chinstrap",
     pch = 19, col = 'deeppink')
plot(penguins$flipper_length_mm[penguins$species == "Gentoo"],
     penguins$body_mass_g[penguins$species == "Gentoo"],
     xlim = xlimits,
     ylim = ylimits,
     xlab = "Flipper length (mm)",
     ylab = "Body Mass (g)",
     main = 'Gentoo',
     pch = 19, col = 'deepskyblue')

titles <- c("Adelie", "Chinstrap", "Gentoo")
colors <- c("navyblue", "deeppink", "deepskyblue")

pdf('MassvsFlipper_species.pdf', height = 4, width = 4)
for(i in 1:3){
  par(mfrow = c(1,1))
  plot(penguins$flipper_length_mm[penguins$species == titles[i]],
     penguins$body_mass_g[penguins$species == titles[i]],
     xlim = xlimits,
     ylim = ylimits,
     xlab = "Flipper length (mm)",
     ylab = "Body Mass (g)",
     main = titles[i],
     pch = 2, col = colors[i])
}
dev.off()
```
# Practice 2

  1. What is the mean bill height and depth in this dataset?
  2. If 50mm is long, how many long-billed penguins are in the total dataset (not just females only)?
  3. Create a new variable in the dataset to represent the ratio of bill length to depth.
  4. Plot your new variable in a boxplot and change the color of the inside of the boxplot. HINT: see `?boxplot`.
