---
title: "Week 5 Lecture 1 histograms"

output: html_document
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
titanic <- read.csv("titanic.csv")
```

### Histograms and barchart with `ggplot`


```{r}
library(ggplot2)
p <- ggplot(data = titanic,
            mapping = aes(x = Survived))
p + geom_bar(aes(y = ..count..))
```



```{r}
titanic[, "cSurvived"] <- ""
titanic[titanic$Survived==1, "cSurvived"] <- "Survived"
titanic[titanic$Survived==0, "cSurvived"] <- "Died"


p <- ggplot(data = titanic,
            mapping = aes(x = cSurvived))
p + geom_bar(mapping = aes(y = ..count..), fill="tomato1") + xlab("")
```


```{r}
p <- ggplot(data = titanic,
            mapping = aes(x = cSurvived, fill = as.factor(Pclass)))
p + geom_bar(mapping = aes(y = ..count..)) + xlab("")
```


```{r}
p <- ggplot(data = titanic,
            mapping = aes(x = cSurvived, fill = as.factor(Pclass)))
p + geom_bar(position = "dodge",
             mapping = aes(y = ..prop.., group = Pclass)) + xlab("")
```

```{r}
p <- ggplot(data = titanic,
            mapping = aes(x = cSurvived, fill = Pclass))
p + geom_bar(position = "dodge",
             mapping = aes(y = ..prop.., group = Pclass)) + xlab("")
```

```{r}
p <- ggplot(data = titanic, 
            mapping = aes(x = Age))
p + geom_histogram(bins = 10)
```

```{r}
p <- ggplot(data = titanic, 
            mapping = aes(x = Age, fill = Sex))
p + geom_histogram(alpha = 0.4, bins = 10)
```


```{r}
p <- ggplot(data = titanic, 
            mapping = aes(x = Age, fill = Sex))
p + geom_histogram(alpha = 0.1, bins = 10, position = "identity")
```

