```{r , include=FALSE}
# This is a good place to put libraries required for using the ggplot function
knitr::opts_chunk$set(echo = TRUE)
library(tidyverse)
```

# Introduction to RMarkdown and ggplot2

This is the default R Markdown document. Markdown is a simple formatting syntax for authoring HTML, PDF, and MS Word documents. For more details on using R Markdown see <http://rmarkdown.rstudio.com>.

When you click the **Knit** button a document will be generated that includes both content as well as the output of any embedded R code chunks within the document. You can embed an R code chunk like this:

For **our** class we will **Knit to HTML**.

For more assistance with RMarkdown, see Chapter 21 in *R for Data Science* and the RMarkdown cheat sheet at <https://evoldyn.gitlab.io/evomics-2018/ref-sheets/rmarkdown-cheatsheet-2.0.pdf>, which link is also found on the [course website](https://liyao880.github.io/stor320/).

# Overview of the Mammals Sleep Dataset from the Tidyverse

```{r}
msleep #Prints the data but takes up a lot of space
head(msleep,5) #Prints the first 5 rows
str(msleep) #Lists all variables and the type of variable
summary(msleep) #Provides summary statistics for all variables in dataset
summary(msleep$awake) #Provides summary statistics for the awake variable in dataset msleep
dim(msleep) #Outputs a Vector Giving the Number of Rows and Columns
unique(msleep$vore) #Lists all the unique values for a categorical variable Animals are Classified as Carnivore Omnivore, Herbivore, or Insectivore: "NA" references a missing response
which(is.na(msleep$vore)) #Returns the Observation index where missing values exist
msleep2=msleep[-which(is.na(msleep$vore)),] #Removes the 7 Observations that are missing a vore-specification
```
In this dataset, there are 83 observations and 11 variables.

# ggplot Discovery

## Barplot Examples
```{r}
ggplot(data=msleep2) +
  geom_bar(aes(x=vore))
ggplot(data=msleep2) +
  geom_bar(aes(x=vore),color="dimgrey",fill="deepskyblue1",size=2) + 
  xlab("Type of Vore") + ylab("Frequency") + 
  theme_classic()
```

## Histogram and Boxplot Examples
```{r}
ggplot(data=msleep2) + 
  geom_histogram(mapping=aes(x=sleep_total),bins=15,fill="deepskyblue1") +
  geom_histogram(mapping=aes(x=sleep_rem),bins=15,fill="white",alpha=0.5) + 
  labs(x="Sleep Total",y="Frequency",title="Overlayed Histograms") + theme_dark()
#Warning due to NA
```

```{r}
ggplot(data=msleep2) + 
  geom_boxplot(aes(x=vore,y=awake),fill=c("red","blue","green","purple")) + 
  xlab("Type of Vore") + ylab("Time Awake (Hrs)") + 
  theme_light()+ggtitle("Stratified Boxplots") + 
  scale_x_discrete(labels=c("Carnivore","Herbivore","Insectivore","Omnivore"))
```
```{r}
ggplot(data=msleep2) + 
  geom_boxplot(aes(x=vore,y=awake,color=conservation)) + 
  xlab("Type of Vore") + ylab("Time Awake (Hrs)") + 
  theme_light()+ggtitle("Stratified Stratified Boxplots") + 
  scale_x_discrete(limits=c("carni","herbi"),labels=c("Carnivore","Herbivore")) +
  guides(color=guide_legend(title="Conservation \nStatus")) + 
  theme_classic()
```
```{r}
ggplot(data=msleep2) + 
  geom_boxplot(aes(x=vore,y=awake)) + 
  facet_wrap(conservation~.) +
  xlab("Type of Vore") + ylab("Time Awake (Hrs)") + 
  theme_light()+ggtitle("Separated Stratified Boxplots") + 
  scale_x_discrete(limits=c("carni","herbi"),labels=c("Carnivore","Herbivore")) +
  theme_test()
```

## Heatmap Examples (Triple Variables)

```{r}
ggplot(data=msleep2,aes(x=vore,y=conservation)) + 
  geom_tile(aes(fill=sleep_total)) + 
  scale_fill_gradient(low="deepskyblue1",high="white")+
  theme_classic() + 
  scale_x_discrete(label=c("Carnivore","Herbivore","Insectivore","Omnivore")) +
  theme(axis.text.x=element_text(angle=45,vjust=0.5))+
  xlab("")+ylab("") + 
  ggtitle("Total Sleep for Combinations of Conservation Status and Diet")
```

The next example can be found at <https://ggplot2.tidyverse.org/reference/scale_brewer.html>. These examples are based on the classic Old Faithful data set. The data set provides the joint probability distribution of waiting time between eruptions and the duration of the eruptions. The original data set `faithful`contains sample data from monitoring the famous geyser Old Faithful. The data set `faithfuld` from **ggplot2** provides emperical joint density estimates for relationship between these two variables.

```{r}
#First Notice from Original Old Faithful Data Sets
ggplot(faithful) + 
  geom_point(aes(x=waiting,y=eruptions),col="black")

#Now we Construct a Heatmap Showing the 
v <- ggplot(faithfuld) +
  geom_tile(aes(waiting, eruptions, fill = density))
v

v2=v + scale_fill_distiller()
v2

v3=v+scale_fill_distiller(palette = "Spectral")
v3

v4=v3 + xlab("Time Between Eruptions (mins)") + ylab("Duration of Eruptions (mins)") + 
  ggtitle("Old Faithful") + labs(subtitle=expression(paste("Joint Density Function: ",italic("f(Waiting Time,Duration)"))))
v4
```

