
---
title: "Titanic - Combination of Inputs"
output: 
  flexdashboard::flex_dashboard:
    orientation: columns
    vertical_layout: fill
    logo: logo.png
    source_code: embed
    social: menu

runtime: shiny
---

```{r global, include=FALSE}
# load data in 'global' chunk so it can be shared by all users of the dashboard
library(flexdashboard)
library(tidyverse)
library(plotly)
library(knitr)
library(DT)
library(lubridate)
df <- read_csv('https://bryantstats.github.io/math421/data/titanic_final.csv')
df <- drop_na(df, Age)
df <- drop_na(df, Embarked)
df$date <- mdy(df$date)
df$Survived = factor(df$Survived)
df$Pclass = factor(df$Pclass)

titanic_sex <- unique(df$Sex)
```

{.sidebar}
=======================================================================

The sinking of the Titanic is one of the most infamous shipwrecks in history. While there was some element of luck involved in surviving, it seems some groups of people were more likely to survive than others.  Select the input for the plots. 


```{r}
sliderInput(inputId = "fare_input",
            label = h3("Fare Range"),
            min = min(df$Fare, na.rm=TRUE),
            max = max(df$Fare, na.rm=TRUE),
            value= c(50, 200))

selectInput(inputId = "sex_input", 
            label = h3("Gender"),
            choices = titanic_sex, 
            selected = 'male')

checkboxGroupInput(inputId = "embarked_input", 
                   label = h3("Embarked Location"),
                   choices = unique(df$Embarked), 
                   inline = TRUE,
                   selected = 'S')
```

Main Tab 1
=======================================================================

Column {data-width=500}
-----------------------------------------------------------------------

### Density Plot

```{r}
renderPlotly({
  p1 <- df %>% 
    filter(Fare>input$fare_input[1], Fare<input$fare_input[2]) %>% 
    filter(Sex==input$sex_input) %>% 
    filter(Embarked %in% input$embarked_input) %>% 
    ggplot(aes(x=Age, color=Survived))+
    geom_density()
  ggplotly(p1)
})
```

Column {data-width=500}
-----------------------------------------------------------------------

### Bar Plot

```{r}
renderPlotly({
  p2 <- df %>% 
    filter(Fare>input$fare_input[1], Fare<input$fare_input[2]) %>% 
    filter(Sex==input$sex_input) %>% 
    filter(Embarked %in% input$embarked_input) %>%  
    ggplot(aes(x=Pclass, fill=Survived))+
    geom_bar()
  ggplotly(p2)
})
```


Main Tab 2
=======================================================================

Column {data-width=500}
-----------------------------------------------------------------------

### Density Plot

```{r}
renderPlotly({
  p1 <- df %>% 
    filter(Fare>input$fare_input[1], Fare<input$fare_input[2]) %>% 
    filter(Sex==input$sex_input) %>% 
    filter(Embarked %in% input$embarked_input) %>% 
    ggplot(aes(x=Age, color=Survived))+
    geom_density()
  ggplotly(p1)
})
```

Column {data-width=500}
-----------------------------------------------------------------------

### What is Density Plot?

- A density plot is an alternative to Histogram used for visualizing the distribution of a continuous variable. The peaks of a Density Plot help to identify where values are concentrated over the interval of the continuous variable. Compared to Histograms, Density Plots are better at finding the distribution shape because they are re not affected by the number of bins used (each bar used in a typical histogram).

- For example, a Histogram with only 4 bins wouldn’t produce a distinguishable enough shape of distribution as a 30-bin Histogram would. However, with Density Plots, this isn’t an issue.

![](https://datavizcatalogue.com/methods/images/top_images/SVG/density_plot.svg)



Main Tab 3
=======================================================================

Column {data-width=500}
-----------------------------------------------------------------------

### Bar Plot

```{r}
renderPlotly({
  p2 <- df %>% 
    filter(Fare>input$fare_input[1], Fare<input$fare_input[2]) %>% 
    filter(Sex==input$sex_input) %>% 
    filter(Embarked %in% input$embarked_input) %>%  
    ggplot(aes(x=Pclass, fill=Survived))+
    geom_bar()
  ggplotly(p2)
})
```

Column {data-width=500}
-----------------------------------------------------------------------

### What is Bar Chart?

- There are two types of bar charts: geom_bar() and geom_col(). geom_bar() makes the height of the bar proportional to the number of cases in each group (or if the weight aesthetic is supplied, the sum of the weights). 

- If you want the heights of the bars to represent values in the data, use geom_col() instead. geom_bar() uses stat_count() by default: it counts the number of cases at each x position. geom_col() uses stat_identity(): it leaves the data as is.

```{r}
image_url <- "https://s3-us-west-2.amazonaws.com/scorestream-team-profile-pictures/274145/20181010232546_702_mascot300.png"
```
<center><img src="`r image_url`"></center>

