---
title: "R Markdown Demo"
author: "Meijing_Liang"
date: "`r Sys.Date()`"
output: pdf_document
bibliography: S0048969722083152.bib
---

```{r setup, include = FALSE}
knitr::opts_chunk$set(knitr::opts_chunk$set(echo = TRUE, message = FALSE, warning = FALSE))
# Check if relevant packages have been installed; If not, then install packages
packages <- c("ggplot2", "hexbin")
if ( length(missing_pkgs <- setdiff(packages, rownames(installed.packages()))) > 0) { 
  message("Installing missing package(s): ", paste(missing_pkgs, collapse = ", "))
  install.packages(missing_pkgs,repos = "http://cran.us.r-project.org")}
# Load libraries
require(ggplot2)
require(hexbin)
```

```{r}
library(ggplot2)
library(hexbin)
```

## R Markdown

This is an 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:

# Add Code Chunks
## 1) The keyboard shortcut Cmd/Ctrl + Alt + I


## 2) The "Insert" button icon in the editor toolbar


## 3) Manually type the chunk delimiters ```{r} and ```


# Chunk Header

## Chunck Name
```{r, followed by an optional chunk name, followed by comma-seprated options} ```  
1. easy navigation between chunks using the drop-down code navigator  
2. setup cached chunks to avoid rerun  

[\textcolor{green}{R for Data Science}](https://r4ds.had.co.nz/r-markdown.html)

## Chunck Option
60+ customized options, arguments supplied to the chunk header.  

[\textcolor{green}{Learn more about chunk options at}](http://yihui.name/knitr/options)  

eval = FALSE prevents code from being evaluated.  
include = FALSE runs the code, but doesn’t show the code or results in the final document.   
echo = FALSE prevents code, but not the results from appearing in the finished file.   
message = FALSE or warning = FALSE prevents messages or warnings from appearing in the finished file.  
results = 'hide' hides printed output, fig.show = 'hide' hides plots.  
error = TRUE causes the render to continue even if code returns an error.  

### 1. include = FALSE 
```{r include = FALSE}
print("hello world!")
```

### 2. include = TRUE, echo = TRUE  
```{r include = TRUE, echo = TRUE}
print("hello world!")
```

### 3. include = TRUE, echo = FALSE  
```{r include = TRUE, echo = FALSE}
print("hello world!")
```

### Table
By default, R Markdown prints data frames and matrices as you’d see them in the console:
```{r}
mtcars[1:5, ]
```
If you prefer that data be displayed with additional formatting you can use the knitr::kable function. The code below generates Table 27.1.
```{r}
knitr::kable(
  mtcars[1:5, ], 
  caption = "A knitr kable."
)
```

## YAML Header
YAML stands for: it’s “yet another markup language”, which is designed for representing hierarchical data in a way that’s easy for humans to read and write. 

## Citations
you can add a citation [@ZANTIS2023161211].

## Including Plots
```{r include=FALSE}
# Load wine data
wine.dat <- read.csv('https://archive.ics.uci.edu/ml/machine-learning-databases/wine-quality/winequality-red.csv',
                     sep = ";", header = T)
# Change quality data type to factor
wine.dat$quality <- as.factor(wine.dat$quality)
# Examine data
summary(wine.dat)
head(wine.dat)
str(wine.dat)
```

### (a) \textcolor{teal}{hex plot - default}  
\textcolor{teal}{Construct a hex plot of wine density vs. alcohol, overplotting with a smoothing (loess) function. Label appropriately. Use default binwidths. Comment briefly on the quality of the plot.}  
\textcolor{red}{Answer: With the hex plot, the density of the observations is easy to be seen. The brighter red hexagons indicate higher frequency, while the darker red hexagons indicate lower frequency. With the default binwidth, there are many empty hexagons, and each hexagon is slightly distorted, so a more appropriate binwidth may be required.}

[\textcolor{green}{Using colours in LaTeX}](https://www.overleaf.com/learn/latex/Using_colours_in_LaTeX)  

```{r out.width = "50%", fig.cap="A nice image."}
# Hex plot
ggplot(wine.dat, aes(density, alcohol)) +
  geom_hex() +
  scale_fill_gradient(low = "green",high = "darkgreen") +
  geom_smooth(method = 'loess', formula = y ~ x, color = 'gold') +
  ggtitle('Wine Density vs. Alcohol Content') +
  xlab('Density (g/liter)') +
  ylab('Alcohol (% vol.)')
```

