Introduction to ggplot

Using ggplot is currently a popular way to create graphics. We’ll be using ggplot to illustrate relationships between variables in the titanic dataset we went over last lab. It isn’t necessary that you know ggplot, but its a tool available to you to make more complex and prettier plots.

To use the ggplot function you will need to install and load the ggplot2 package which can be done with the following code. You will want to uncomment the “install.packages(ggplot2)” and run that line of code. After you run the code, you can comment it out again as you only need to install the package once. You will need to load the package using “library(ggplot2) every time you open R, however.

#install.packages("ggplot2")
library(ggplot2)

Simple Bar Charts

titanic <- read.delim('https://raw.githubusercontent.com/IowaBiostat/data-sets/main/titanic/titanic.txt')

ggplot(titanic, aes(x = Class)) +
  geom_bar(position = "stack", fill="forest green") +
  ylab("Number of Individuals in each Class") +
  xlab("Class")

Stacked Bar Charts

ggplot(titanic, aes(x = Survived)) +
  geom_bar(position = "stack", fill="forest green") +
  ggtitle("Death and Survival Counts for Individuals on the Titanic") +
  theme(plot.title = element_text(hjust = 0.5))

ggplot(titanic, aes(x = Class)) +
  geom_bar(position = "stack", aes(fill = Survived))+
  xlab("Class")

Grouped bar charts using ‘facet_wrap’

ggplot(titanic, aes(x = Class)) +
  geom_bar(position = "stack", aes(fill = Survived)) +
  facet_wrap(c("Age", "Sex")) + 
  xlab("Class") 

ggplot(titanic, aes(x = Class)) +
  geom_bar(position = "stack", aes(fill = Survived)) +
  facet_wrap(c("Age", "Sex"), scales = "free") + # plots have different scales
  xlab("Class")