forcats
package## 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")
Let’s start by creating a blank graph with:
no mapped aesthetics
no geoms (hence blank)
“Passenger Class” on the x-axis
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)
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
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
The column you want to reorder the levels:
.f =
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!
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?