---
title: "Data Visualization and Design"
subtitle: "Using ggplot2 <br> <br> <br> <br>"
author: "Omni Analytics Group"
output:
  xaringan::moon_reader:
    includes:
      after_body: insert-logo.html
    css: [default, default-fonts, classroom.css]
    lib_dir: libs
    nature:
      ratio: 16:9
      highlightStyle: github
      highlightLines: true
      countIncrementalSlides: false
---

```{r setup, include=FALSE}
options(htmltools.dir.version = FALSE)
knitr::opts_chunk$set(
	echo = TRUE,
	message = FALSE,
	warning = FALSE,
	cache = TRUE
)
```

```{r, echo=FALSE}
library(ggplot2)
library(ggsci)
library(ggthemes)
library(gridExtra)
library(grid)

grid_arrange_shared_legend <- function(..., ncol = length(list(...)), nrow = 1, position = c("bottom", "right")) {

  plots <- list(...)
  position <- match.arg(position)
  g <- ggplotGrob(plots[[1]] + theme(legend.position = position))$grobs
  legend <- g[[which(sapply(g, function(x) x$name) == "guide-box")]]
  lheight <- sum(legend$height)
  lwidth <- sum(legend$width)
  gl <- lapply(plots, function(x) x + theme(legend.position="none"))
  gl <- c(gl, ncol = ncol, nrow = nrow)

  combined <- switch(position,
                     "bottom" = arrangeGrob(do.call(arrangeGrob, gl),
                                            legend,
                                            ncol = 1,
                                            heights = unit.c(unit(1, "npc") - lheight, lheight)),
                     "right" = arrangeGrob(do.call(arrangeGrob, gl),
                                           legend,
                                           ncol = 2,
                                           widths = unit.c(unit(1, "npc") - lwidth, lwidth)))
  
  grid.newpage()
  grid.draw(combined)

  # return gtable invisibly
  invisible(combined)

}

df <- read.csv('data/data.csv')
punks<- read.csv("data/punks.csv")
cryptoart <- read.csv("data/crypto.csv")

```

class: center, middle

# POLISHING PLOTS

---
class: inverse, center, middle

# Visual Appearance

---

## Built-In Themes



```{r, echo=FALSE, fig.height=7, fig.width=10, message=FALSE, warning=FALSE}
p0 <- ggplot(df, aes(x = Wallet, y = Volatility, colour = Type)) + geom_point()

p1 <- p0 + labs(title = "Default")
p2 <- p0 + theme_classic() + labs(title = "theme_classic")
p3 <- p0 + theme_bw() + labs(title = "theme_bw()")
p4 <- p0 + theme_dark() + labs(title = "theme_dark")
p5 <- p0 + theme_void() + labs(title = "theme_void")
p6 <- p0 + theme_minimal() + labs(title = "theme_minimal")

grid.arrange(p1, p2, p3, p4, p5, p6, nrow = 2)
```

---

## Other Themes: ggthemes a ggplot2 extension

```{r, echo=FALSE, fig.height=7, fig.width=10}
library(ggthemes)


# Economist theme
# A theme that approximates the style of plots in The Economist magazine.
p1 <- p0 + theme_economist() + scale_colour_economist() + labs(title = "Economist")
p2 <- p0 + theme_stata() + scale_colour_stata()  + labs(title = "Strata")
p3 <- p0 + geom_smooth(method = "lm", se = FALSE) +
  scale_color_fivethirtyeight("cyl") +
  theme_fivethirtyeight() + labs(title = "FiveThirtyEight")
p4 <- p0 + theme_igray() + scale_colour_tableau()  + labs(title = "Tableau")
p5 <-p0 + geom_rangeframe() +
  theme_tufte() + 
  scale_x_continuous(breaks = extended_range_breaks()(mtcars$wt)) +
  scale_y_continuous(breaks = extended_range_breaks()(mtcars$mpg)) + labs(title = "Tufte")
p6 <- p0 + theme_wsj() + scale_colour_wsj("colors6", "") + labs(title = "Wall Street Journal")

 p7 <- p0 + 
  ggtitle("Highcharts") +
  theme_hc() +
  scale_colour_hc()

p8 <- p0 +
  ggtitle("Highcharts dark") +
  theme_hc(bgcolor = "darkunica") +
  scale_fill_hc("darkunica")

p9 <- p0 + theme_excel() + scale_colour_excel() + labs(title = "Excel 2003")


grid.arrange(p1, p2, p3, p4, p5, p6, p7, p8, p9, nrow = 3)
```



---

## Setting Themes

You can globally set a theme with the `theme_set()` function:

```{r, fig.width=10, fig.height=4}
theme_set(theme_bw())

ggplot(df, aes(x = Wallet, y = Volatility, colour = Type)) + geom_point()


```


---

## Elements in a theme

The function `theme()` is used to control non-data parts of the graph including:

- **Line elements**: axis lines, minor and major grid lines, plot panel border, axis ticks background color, etc.  
- **Text elements**: plot title, axis titles, legend title and text, axis tick mark labels, etc.  
- **Rectangle elements**: plot background, panel background, legend background, etc.  

There is a specific function to modify each of these three elements :

- `element_line()` to modify the line elements of the theme
- `element_text()` to modify the text elements
- `element_rect()` to change the appearance of the rectangle elements
- `element_blank()` to draw nothing and assign no space

**Note**: `rel()` is used to specify sizes relative to the parent, `margins()` is used to specify the margins of elements.

---


## Modifying a plot

```{r, fig.height=4, fig.width=10, eval = FALSE}
p1 <- ggplot(punks) + 
  geom_bar(aes(x = Type, colour = TypeSkin, fill = TypeSkin)) 

p2 <- p1 + theme_classic() + theme(
  ## modify plot background
  plot.background = element_rect(fill = "lightskyblue1",colour = "pink",size = 0.5, linetype = "longdash")
  )
```


```{r, fig.height=4, fig.width=10, echo = FALSE}
p1 <- ggplot(mpg) + 
  geom_bar(aes(x = class, colour = manufacturer, fill = manufacturer) ) 

p2 <- p1 + theme_classic() + theme(
  ## modify plot background
  plot.background = element_rect(fill = "lightskyblue1",colour = "pink",size = 0.5, linetype = "longdash")
  )
grid.arrange(p1, p2, nrow = 1)
```

---

## Plot Legends

```{r, fig.height=4, fig.width=10}
p3 <- p2 + theme(
  ### move and modify legend 
  legend.title = element_blank(),
  legend.position = "top", 
  legend.key = element_rect(fill = "lightskyblue1", color = "lightskyblue1"),
  legend.background = element_rect( fill = "lightskyblue1",color = "pink", size = 0.5,linetype = "longdash")
  )
```


```{r, fig.height=4, fig.width=10, echo = FALSE}
grid.arrange(p2, p3, nrow = 1)
```

---

## Modifying Axes

```{r, echo=TRUE, fig.height=3.5, fig.width=10}
p4 <- p3 + theme(
  ### remove axis ticks
  axis.ticks=element_blank(),
  ### modify axis lines
  axis.line.y = element_line(colour = "pink", size = 1, linetype = "dashed"),
  axis.line.x = element_line(colour = "pink", size = 1.2, linetype = "dashed"))
```

```{r, echo=FALSE, fig.height=3.5, fig.width=10}
grid.arrange(p3, p4, nrow = 1)
```


---

## Plot Labels

Can be modified in several ways:
- `labs()`, `xlab()`, `ylab()`, `ggtitle()`
- You can also set axis and legend labels in the individual scales (using the first argument, the name)
.
```{r, echo=TRUE, fig.height=3.5, fig.width=10}
p5 <- p4 + labs(x = "Class of car", 
                y = "",
                title = "Cars by class and manufacturer",
                subtitle = "With a custom theme!!")
```

```{r, echo=FALSE, fig.height=3.5, fig.width=10}
grid.arrange(p4, p5, nrow = 1)
```


---

## Zooming

```{r, echo=TRUE, fig.height=3.5, fig.width=10}
p <- ggplot(df, aes(x = Wallet, y = Volatility, colour = Type)) + geom_point()
p_zoom_in <- p + xlim(2, 4) + ylim(10, 25)
p_zoom_out <- p + xlim(0,7) + ylim(0, 45)
```

```{r, echo=FALSE, fig.height=3.5, fig.width=10}
p <- ggplot(df, aes(x = Wallet, y = Volatility, colour = Type)) + geom_point() + theme(legend.position = "none") + ggtitle("Default")+ scale_color_locuszoom()
p_zoom_in <- p + xlim(2, 4) + ylim(10, 25) + ggtitle("Zoomed in")
p_zoom_out <- p + xlim(0,7) + ylim(0, 45) + ggtitle("Zoomed out")
grid.arrange(p, p_zoom_in, p_zoom_out, nrow = 1)
```

---
class: inverse, center, middle

# Interactive graphics

---
## Plotly

```{r, fig.height=3.5, fig.width=10}
p <- ggplot(df, aes(x = Wallet, y = Volatility, colour = Type)) + geom_point() + scale_color_locuszoom()

library(plotly)
ggplotly(p)

```

---
class: inverse, center, middle

# Saving graphics


---

## Saving your Work

We can save the results of a plot to a file (as an image) using the `ggsave()` function:

```{r, eval=FALSE}
p1 <- ggplot(df, aes(x = Wallet, y = Volatility, colour = Type)) + geom_point()

ggsave("Volatilify_by_wallet.pdf", plot = p1)
```

<br><br>

<p align="center">

<img src="images/Cut_outs/Cut_out_08.png"  width="200px" height="160px">

</p>
---

## Your Turn

1. Create a scatterplot of End.Price versus Start.Price from the `cryptoart` data, colored by Platform
2. Use the black and white theme
3. Include an informative title
4. Move the legend to the bottom
5. Save your plot to a pdf file and open it in a pdf viewer.

```{r, fig.width=10, fig.height=4}
ggplot(data = cryptoart, aes(x = Start.Price, y = End.Price))
```

---

## Answers

```{r, echo=TRUE, fig.width=10, fig.height=4, eval = FALSE}
p1 <- ggplot(data = cryptoart, aes(x = Start.Price, y = End.Price, colour = Platform)) +
    geom_point() +
    ggtitle("End Price vs Start Price by Platform") +
    theme(
        legend.position = "bottom"
    )
p1

ggsave("yourturn.pdf", plot = p1)
```
