These exercises cover the sections of Plotting in R PlottingInR.

Solutions 2

Please load the dateset subset_GoT from "Got_dataset/subset_GoT.csv" and use ggplot2 to

  1. create a bar plot using subset_GoT$social_status and see the number of characters that are Highborn or Lowborn. We will plot this data for the rest of the questions in this section.
subset_GoT<-read.csv(file="Got_dataset/subset_GoT.csv")

head(subset_GoT)
##    id         name sex        religion            occupation social_status
## 1 100 Waymar Royce   M Unknown/Unclear Boiled leather collar       Lowborn
## 2 101 Gared Tuttle   M Unknown/Unclear Boiled leather collar       Lowborn
## 3 102         Will   M Unknown/Unclear Boiled leather collar       Lowborn
## 4 103         Irri   F  Great Stallion Boiled leather collar       Lowborn
## 5 104     Jon Snow   M        Old Gods Boiled leather collar      Highborn
## 6 105   Bran Stark   M        Old Gods           Silk collar      Highborn
##   allegiance_last allegiance_switched dth_flag exp_time_sec exp_time_hrs
## 1   Night's Watch                   N        1          342         0.10
## 2   Night's Watch                   N        1          405         0.11
## 3   Night's Watch                   N        1          692         0.19
## 4       Targaryen                   Y        1        48489        13.47
## 5   Night's Watch                   Y        0       230347        63.99
## 6           Stark                   N        0       230347        63.99
library(ggplot2)
ggplot(subset_GoT,aes(x=social_status)) + 
  geom_bar()

    1. assign colours according to whether the characters switched their allegiance (subset_GoT$allegiance_switched)
ggplot(subset_GoT,aes(x=social_status,fill=allegiance_switched)) + 
  geom_bar()

    1. add argument position=position_dodge() in the geom_bar() function and see what effect this has
ggplot(subset_GoT,aes(x=social_status,fill=allegiance_switched)) + 
  geom_bar(position=position_dodge())

  1. use the facet_wrap() function to separate the figure according to the character’s sex
ggplot(subset_GoT,aes(x=social_status,fill=allegiance_switched)) + 
  geom_bar()+facet_wrap(~sex)

  1. use the facet_grid() function to separate the figure according to the character’s sex and occupation
ggplot(subset_GoT,aes(x=social_status,fill=allegiance_switched)) + 
  geom_bar()+facet_grid(sex~occupation)

  1. use the facet_wrap() function to separate the figure according to the character’s sex and occupation, and compare the difference from your answer to (4)
ggplot(subset_GoT,aes(x=social_status,fill=allegiance_switched)) + 
  geom_bar()+facet_wrap(sex~occupation)

  1. save the final figure as “pdf” file
ggsave(file="final_figure.pdf")
## Saving 8 x 4 in image