✅ (Solutions) Lab 07

Author
Published

19 July 2023

Task 01

Task 01

# Recreate plot_df
plot_df <- 
  df %>% 
  filter(ward_name == 'Holborn and Covent Garden' |
         ward_name == 'Bloomsbury') %>%
  group_by(epoch, ward_name) %>% 
  count()


# Data and Mapping
g <- ggplot(plot_df, aes(x=epoch, y=n, colour=ward_name, linetype=ward_name))
  
# Geoms
# I collected a brownish red shade from https://www.color-hex.com
g <- g + geom_line(size=0.8)

# Customise scales
g <- g + 
  #It took me A LOT of trial and error to get this scale_x_date right
  scale_x_date(date_breaks = "1 year",
               date_labels = "%Y",
               limits=c(lubridate::ymd("2014-12-20"), NA)) +
  scale_colour_manual(name="Ward", 
                      values=c("Holborn and Covent Garden"="#d95f02",
                               "Bloomsbury"="#7570b3")) +
  scale_linetype_manual(name="Ward", 
                        values=c("Holborn and Covent Garden"="dashed",
                                 "Bloomsbury"="dotdash"))

# Titles
g <- g + labs(title="Crime patterns (ups and downs) follow a similar trend in both wards",
              subtitle = "But crimes in 'Bloomsbury' seem to have increased considerably since around mid-2021",
              y="Number of Records per Day",
              x="Date")

# Themes
g <- g + 
  theme_minimal() +
  theme(axis.title = element_text(size=rel(1.3)),
        axis.text = element_text(size=rel(1.2)),
        axis.title.x = element_text(margin=margin(t=15)),
        axis.title.y = element_text(margin=margin(r=15)),
        plot.title = element_text(size=rel(1.7)),
        legend.position = c(0.21, 0.8))


g
Task 02

Task 02

plot_df <- 
  df %>% 
  group_by(epoch, ward_name) %>% 
  count() %>% 
  mutate(highlight_ward=if_else(ward_name == 'Holborn and Covent Garden' | ward_name == 'Bloomsbury',
                                ward_name, 'Other'))

# Data and Mapping
g <- ggplot(plot_df, 
            aes(x=epoch, y=n, 
                color=highlight_ward, 
                linewidth=highlight_ward,
                linetype=highlight_ward,
                alpha=highlight_ward,
                group=ward_name))
  
# Geoms
g <- g + geom_line()

# Customise scales
g <- g + 
  #It took me A LOT of trial and error to get this scale_x_date right
  scale_x_date(date_breaks = "1 year",
               date_labels = "%Y",
               limits=c(lubridate::ymd("2014-12-20"), NA)) +
  scale_colour_manual(name="Ward", 
                      values=c("Holborn and Covent Garden"="#d95f02",
                               "Bloomsbury"="#7570b3",
                               "Other"="#d7d7d7")) +
  scale_linetype_manual(name="Ward", 
                        values=c("Holborn and Covent Garden"="dashed",
                                 "Bloomsbury"="dotdash",
                                 "Other"="solid")) +
  scale_linewidth_manual(name="Ward",
                         values=c("Holborn and Covent Garden"=0.8,
                                 "Bloomsbury"=0.8,
                                 "Other"=0.5)) +
  scale_alpha_manual(name="Ward",
                     values=c("Holborn and Covent Garden"=1,
                              "Bloomsbury"=1,
                              "Other"=0.8))
  

# Titles
g <- g + labs(title="'Holborn and Covent Garden' and 'Bloomsbury' have always led the records",
              subtitle = "Maybe those are the most populous/touristy areas of Camden?",
              y="Number of Records per Day",
              x="Date")

# Themes
g <- g + 
  theme_minimal() +
  theme(axis.title = element_text(size=rel(1.3)),
        axis.text = element_text(size=rel(1.2)),
        axis.title.x = element_text(margin=margin(t=15)),
        axis.title.y = element_text(margin=margin(r=15)),
        plot.title = element_text(size=rel(1.7)),
        legend.position = c(0.21, 0.8))

g
Task 03

🏡 Bonus Task

I doubt that anyone will actually do this in time but here’s the code to help you guide them. No need to give them any solutions


plot_df <- 
  df %>% 
  group_by(epoch_month=lubridate::floor_date(epoch, unit="month"),
           has_outcome=if_else(is.na(outcome_date), "NO", "YES")) %>% 
  count() 

g <- ggplot(plot_df, aes(x=epoch_month, y=n, fill=has_outcome, color=has_outcome))

g <- g + geom_col(alpha=0.9, stroke=0.4, width=17)

# Colours from https://colorbrewer2.org/#type=diverging&scheme=RdYlBu&n=3
g <- g +
  #It took me A LOT of trial and error to get this scale_x_date right
  scale_x_date(date_breaks = "1 year",
               date_labels = "%Y",
               limits=c(lubridate::ymd("2014-12-20"), NA)) +
  scale_fill_manual(name="Case has an outcome?", 
                      values=c("YES"="#ededed",
                               "NO"="#fc8d59")) +
  scale_colour_manual(name="Case has an outcome?", 
                      values=c("YES"="#ededed",
                               "NO"="#e27e50")) 


g <- g + labs(title="On average ~20-25% of cases do not have an outcome",
              subtitle = "Are they unsolved or is it just a reporting issue?",
              y="Number of Records per Day",
              x="Date")

# Themes
g <- g + 
  theme_minimal() +
  theme(axis.title = element_text(size=rel(1.3)),
        axis.text = element_text(size=rel(1.2)),
        axis.title.x = element_text(margin=margin(t=15)),
        axis.title.y = element_text(margin=margin(r=15)),
        plot.title = element_text(size=rel(1.7)),
        legend.position = c(0.21, 0.9),
        legend.direction = "horizontal")

g