Set Up Your Project and Load Libraries

## Set the default size of figures and default to printing the R code
knitr::opts_chunk$set(fig.align = "center",
                      echo = F,
                      include = T)  

## Load the libraries we will be using
pacman::p_load(tidyverse, skimr)

## Changing the default theme to black/white instead of grey
theme_set(theme_bw())

## Read in the titanic.csv file and save it as t_df. 
# Include stringsAsFactors = T in read.csv() to change all the strings to factors: 
t_df <- 
  read.csv("https://raw.githubusercontent.com/Shammalamala/DS-2870-Data-Sets/main/titanic.csv")

Completely Blank Graph

Let’s start by creating a blank graph with:

  1. no mapped aesthetics

  2. no geoms (hence blank)

  3. “Passenger Class” on the x-axis

  4. Any future bars to sit on the x-axis

save it as gg_blank

Let’s start by just adding geom_bar() with class mapped to the x-axis

By default, it will place the groups in alphabetical order.

If you want to change the order to which groups appear first in the data (like we did in the previous videos), we can map the x-axis to fct_inorder(class)

Order based on bar height

Unsummarized data

If instead you want to order the bars from highest to lowest (sometimes called a Pareto chart), you can use fct_infreq() instead

How do we get from lowest to highest? We combine fct_infreq() with fct_rev() (rev = reverse), which will reverse the order of the groups of the factor

Reordering based on bar height with summarized data

What do we do if we have summarized data, like what we have below?

##    class   n
## 1   Crew 885
## 2  First 325
## 3 Second 285
## 4  Third 705

If you want the order of the levels to depend on a second column, (like with our example), you can use fct_reorder(). It requires 2 arguments

  1. The column you want to reorder the levels: .f =

  2. The column you want to order the levels (from smallest to largest): .x =

Since we’re using summarized data, make sure to specify y = and use the correct geom_!

If you want to go from highest to lowest, you can add .desc = T inside fct_reorder(). Try it out above!

Improving the graph without using scale_fill_manual()

If we wanted to improve the graph below without adding scale_fill_manual() to manually pick the columns, which which of the functions that we’ve seen here can you use to make “Alive” a blue-ish color and “Dead” a red-ish color?