---
title: "Bar Charts 2"
output: html_document
---

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

### Two ways of constructing a complicated bar chart

Last time, we constructed this chart for the `titanic` dataset:

```{r}
titanic <- read.csv("http://guerzhoy.princeton.edu/201s20/titanic.csv")
titanic$Pclass <- as.character(titanic$Pclass)
titanic$cSurvived <- ""
titanic$cSurvived[titanic$Survived == 0] <- "Died"
titanic$cSurvived[titanic$Survived == 1] <- "Survived"
t1 <- titanic %>% group_by(Pclass, cSurvived) %>% 
                  summarize(num = n()) %>% 
                  ungroup %>% 
                  mutate(p = num/sum(num))

ggplot(data = t1, mapping = aes(x = cSurvived)) + 
            geom_bar(stat = "identity", position = "identity", mapping = aes(y = p, fill = Pclass), alpha = 0.5 ) + xlab("")

ggplot(data = t1, mapping = aes(x = cSurvived)) + 
            geom_bar(stat = "identity", position = "stack", mapping = aes(y = p, fill = Pclass), ) + xlab("")

```



Here is another way to accomplish the same thing:

```{r}
ggplot(titanic) + 
  geom_bar(mapping = aes(x = cSurvived, y = ..count../nrow(titanic), fill = Pclass), stat = "count")
```

The default `stat` for `geom_bar` is `"count"`. `stat` transforms the data: it creates the special values `..count..` and `..prop..` that we can use. `stat` identity leaves the data as is. That is why we needed to pre-transform the data before displaying it.

Recall other possibilities with `stat = ..count..`:

```{r}
ggplot(titanic) + 
  geom_bar(mapping = aes(x = cSurvived, y = ..prop.., fill = Pclass), stat = "count")
```

```{r}
ggplot(titanic) + 
  geom_bar(mapping = aes(x = cSurvived, y = ..prop.., fill = Pclass, group = 1), stat = "count")
```

```{r}
ggplot(titanic) + 
  geom_bar(mapping = aes(x = cSurvived, y = ..prop.., fill = Sex, group = Sex), stat = "count")
```

```{r}
ggplot(titanic) + 
  geom_bar(mapping = aes(x = cSurvived, y = ..prop.., fill = Pclass, group = cSurvived), stat = "count")
```

```{r}
ggplot(titanic) + 
  geom_bar(mapping = aes(x = cSurvived, y = ..prop.., fill = Pclass, group = Pclass), stat = "count", position = "dodge")
```


```{r}
ggplot(titanic) + 
  geom_bar(mapping = aes(x = cSurvived, y = ..prop.., fill = Pclass, group = Pclass), stat = "count", position = "identity")
```

```{r}
ggplot(titanic) + 
  geom_bar(mapping = aes(x = cSurvived, y = ..prop.., fill = Pclass, group = Pclass), stat = "count", position = "identity", alpha = 0.6)
```


```{r}
titanic$Sex <- relevel(as.factor(titanic$Sex), ref = "male")
ggplot(titanic) + 
  geom_bar(mapping = aes(x = cSurvived, y = ..prop.., fill = Sex, group = Sex), stat = "count", position = "identity", alpha = 0.5)
```
