---
# System prepended metadata

title: Figure 4

---

# Figure 4

### Packages
library(tidyverse) #for most of the important functions
library(gt) #making tables
library(janitor) #wrangling
library(patchwork) #plots
library(dplyr)
library(gtExtras)

### Renaming data

data4 <- data %>% 
  rename(Gender = gender, 
         Age = age_categories,
         Has_child = Has_child,
         Employment_status = Working,
         Key_worker = Key_worker,
         Education = degree,
         Region = region,
         Total_outings = Going_out_total,
         groceries = Adhere_shop_groceries,
         shop_other = Adhere_shop_other,
         meetup = Adhere_meet_friends,
         symptoms = Sx_covid_nomissing,
         Thinks_have_had_COVID19 = Ever_covid) %>% 
  mutate(Thinks_have_had_COVID19 = case_when(Thinks_have_had_COVID19 == 0 ~ "No",
                                    Thinks_have_had_COVID19 == 1 ~ "Yes")) %>% 
  mutate(groceries = case_when(groceries == 0 ~ "One or fewer times in past week",
                               groceries == 1 ~ "Two or more times in past week")) %>% 
  mutate(shop_other = case_when(shop_other == 0 ~ "Not at all in past week",
                                shop_other == 1 ~ "1 or more days in past week")) %>% 
  mutate(meetup = case_when(meetup == 0 ~ "Not at all in past week",
                            meetup == 1 ~ "One or more times in past week")) %>% 
  mutate(symptoms = case_when(symptoms == 0 ~ "Did not correctly identify symptoms",
                              symptoms == 1 ~ "Correctly identified symptoms")) %>% 
  select(Thinks_have_had_COVID19, groceries, shop_other, meetup, symptoms)
  
### creating data frames

#data groceries

datagroceries <- data4 %>% 
  select(Thinks_have_had_COVID19, groceries) %>% 
  arrange(factor(groceries, levels = c("One or fewer time in past week", "Two or more in past week")))
          
tgroceries <- datagroceries %>% 
  tabyl(Thinks_have_had_COVID19, groceries)

gtgroceries <- gt(tgroceries)
datagroceries <- gtgroceries$"_data"

#data shop_other

datashop <- data4 %>% 
  select(Thinks_have_had_COVID19, shop_other) %>% 
  arrange(factor(shop_other, levels = c("Not at all in past week", "1 or more days in past week")))

tshop <- datashop %>% 
  tabyl(Thinks_have_had_COVID19, shop_other)

gtshop <- gt(tshop)
datashop <- gtshop$"_data"

#data meetup

datameetup <- data4 %>% 
  select(Thinks_have_had_COVID19, meetup) %>% 
  arrange(factor(meetup, levels = c("Not at all in past week", "1 or more days in past week")))

tmeetup <- datameetup %>% 
  tabyl(Thinks_have_had_COVID19, meetup)

gtmeetup <- gt(tmeetup)
datameetup <- gtmeetup$"_data"

#data symptoms

datasymptoms <- data4 %>% 
  select(Thinks_have_had_COVID19, symptoms) %>% 
  arrange(factor(symptoms, levels = c("Did not correctly identify symptoms", "Correctly identified symptoms")))

tsymptoms <- datasymptoms %>% 
  tabyl(Thinks_have_had_COVID19, symptoms) 

gtsymptoms <- gt(tsymptoms)
datasymptoms <- gtsymptoms$"_data"

 ### percentages
 
 perGroc <- with(data4, table(Thinks_have_had_COVID19,groceries))
pGroc <- prop.table(perGroc, 1)*100 

perShop <- with(data4, table(Thinks_have_had_COVID19, shop_other))
pShop <- prop.table(perShop, 1)*100

perMeet <- with(data4, table(Thinks_have_had_COVID19, meetup))
pMeet <- prop.table(perMeet, 1)*100

perSymp <- with(data4, table(Thinks_have_had_COVID19,symptoms))
pSymp <- prop.table(perSymp, 1)*100

### individual tables

#Shopping for groceries

tibGroc <- rbind(pGroc)
tibGroc <- as_tibble(tibGroc)

tibGroc <- tibGroc %>% 
  rename("Less than once" = "One or fewer times in past week",
         "Two or more" = "Two or more times in past week")

gtGroc <- gt(tibGroc)
tibGroc1 <- gtGroc$'_data'

listgroceries <- list(datagroceries)
tabgroceries <- rbindlist(listgroceries, use.names = FALSE)
tabgroceries1 <- cbind(tabgroceries, tibGroc1)

gttab1 <- gt(tabgroceries1) %>% 
  tab_spanner(
    label = "Shopping for groceries/pharmacy",
    columns = c(2:3)
  ) %>% 
  cols_label(
    "One or fewer times in past week" = "One or fewer days in the past weeks n=2389 (%)"
  ) %>% 
  cols_label(
    "Two or more times in past week" = "On two or more days in the last week n=3760 (%)"
  ) %>% 
  gt_merge_stack(col1 = "One or fewer times in past week", 
                 col2 = "Less than once"
  ) %>% 
  gt_merge_stack(col1 = "Two or more times in past week",
                 col2 = "Two or more")
  

#Shopping for other

tibshop <- rbind(pShop)
tibshop <- as_tibble(tibshop)

tibshop <- tibshop %>% 
  rename("Not at all" = "Not at all in past week",
         "One or more days" = "1 or more days in past week")

gtpShop <- gt(tibshop)
tibshop1 <- gtpShop$'_data'

listshop <- list(datashop)
tabshop <- rbindlist(listshop, use.names = FALSE)
tabshop1 <- cbind(tabshop, tibshop1)


gttab2 <- gt(tabshop1) %>% 
  tab_spanner(
    label = "Shopping for items other than groceries/pharmacy",
    columns = c(2:3)
  ) %>% 
  cols_label(
    "1 or more days in past week" = "One or more days in past week n=4316"
  ) %>% 
  cols_label(
    "Not at all in past week" = "Not at all in past week n=1833"
  ) %>% 
  cols_move(
    columns = "1 or more days in past week",
    after = "Not at all in past week"
  ) %>% 
  gt_merge_stack(col1 = "Not at all in past week",
                 col2 = "Not at all"
  ) %>% 
  gt_merge_stack(col1 = "1 or more days in past week",
                 col2 = "One or more days")


#Meetup

tibmeet <- rbind(pMeet)
tibmeet <- as_tibble(tibmeet)

tibmeet <- tibmeet %>% 
  rename("Not at all" = "One or more times in past week",
         "One or more days" = "Not at all in past week")

gtpMeet <- gt(tibmeet)
tibmeet1 <- gtpMeet$'_data'

listmeetup <- list(datameetup)
tabmeetup <- rbindlist(listmeetup, use.names = FALSE)
tabmeetup1 <- cbind(tabmeetup, tibmeet1)

gttab3 <- gt(tabmeetup1) %>% 
  tab_spanner(
    label = "Meeting up with friends of family",
    columns = c(2:3)
  ) %>% 
  cols_label(
    "Not at all in past week" = "One or more days in the last week n=878"
  ) %>% 
  cols_label(
    "One or more times in past week" = "Not at all in last week n=5271"
  ) %>% 
  gt_merge_stack(col1 = "One or more times in past week",
                 col2 = "Not at all"
  ) %>% 
  gt_merge_stack(col1 = "Not at all in past week",
                 col2 = "One or more days")


#Symptoms 

tibsymp <- rbind(pSymp)
tibsymp <- as_tibble(tibsymp)

tibsymp <- tibsymp %>% 
  rename("Did not identify" = "Did not correctly identify symptoms",
         "Correctly identified" = "Correctly identified symptoms")

gtpSymp <- gt(tibsymp)
tibsymp1 <- gtpSymp$'_data'

listsymptoms <- list(datasymptoms)
tabsymptoms <- rbindlist(listsymptoms, use.names = FALSE)
tabsymptoms1 <- cbind(tabsymptoms, tibsymp1)

gttab4 <- gt(tabsymptoms1) %>% 
  tab_spanner(
    label = "Correct identification of cough and fever",
    columns = c(2:3)
  ) %>% 
  cols_label(
    "Did not correctly identify symptoms" = "Did not correctly identify common symptoms n=2390"
  ) %>% 
  cols_label(
    "Correctly identified symptoms" = "Correctly identified common symptoms n=3632"
  ) %>% 
  gt_merge_stack(col1 = "Did not correctly identify symptoms",
                 col2 = "Did not identify"
  ) %>% 
  gt_merge_stack(col1 = "Correctly identified symptoms",
                 col2 = "Correctly identified")




# Figure 4

### Renaming data

data4 <- data %>% 
  rename(Gender = gender, 
         Age = age_categories,
         Has_child = Has_child,
         Employment_status = Working,
         Key_worker = Key_worker,
         Education = degree,
         Region = region,
         Total_outings = Going_out_total,
         groceries = Adhere_shop_groceries,
         shop_other = Adhere_shop_other,
         meetup = Adhere_meet_friends,
         symptoms = Sx_covid_nomissing,
         think_covid = Ever_covid) %>% 
  mutate(think_covid = case_when(think_covid == 0 ~ "No",
                                 think_covid == 1 ~ "Yes")) %>% 
  mutate(groceries = case_when(groceries == 0 ~ "On one or fewer days in the last week n = 2389",
                               groceries == 1 ~ "On two or more days in the last week n = 3760")) %>% 
  mutate(shop_other = case_when(shop_other == 0 ~ "Not at all in the last week n = 1833",
                                shop_other == 1 ~ "On one or more days in the last week n = 4316")) %>% 
  mutate(meetup = case_when(meetup == 0 ~ "Not at all in the last week n = 5271",
                            meetup == 1 ~ "On one or more days in the last week n = 878")) %>% 
  mutate(symptoms = case_when(symptoms == 0 ~ "Did not correctly identify common symptoms n = 2390",
                              symptoms == 1 ~ "Correctly identified common symptoms n = 3632")) %>% 
  select(think_covid, groceries, shop_other, meetup, symptoms)

### creating data frames

#data groceries

datagroceries <- data4 %>% 
  select(groceries,think_covid) %>% 
  group_by(think_covid) %>% 
  arrange(factor(groceries, levels = c("On one or fewer days in the last week n = 2389", "On two or more days in the last week n = 3760")))

tgroceries <- datagroceries %>% 
  tabyl(groceries, think_covid)

gtgroceries <- gt(tgroceries)
datagroceries <- gtgroceries$"_data"

#data shop_other

datashop <- data4 %>% 
  select(think_covid, shop_other) %>% 
  arrange(factor(shop_other, levels = c("Not at all in the last week n = 1833", "On one or more days in the last week n = 4316")))

tshop <- datashop %>% 
  tabyl(shop_other, think_covid)

gtshop <- gt(tshop)
datashop <- gtshop$"_data"

#data meetup

datameetup <- data4 %>% 
  select(think_covid, meetup) %>% 
  arrange(factor(meetup, levels = c("Not at all in the last week n = 5271", "On one or more days in the last week n = 878")))

tmeetup <- datameetup %>% 
  tabyl(meetup, think_covid)

gtmeetup <- gt(tmeetup)
datameetup <- gtmeetup$"_data"

#data symptoms

datasymptoms <- data4 %>% 
  select(think_covid, symptoms) %>% 
  arrange(factor(symptoms, levels = c("Did not correctly identify common symptoms n = 2390", "Correctly identified common symptoms n = 3632")))

tsymptoms <- datasymptoms %>% 
  tabyl(symptoms, think_covid) 

gtsymptoms <- gt(tsymptoms)
datasymptoms <- gtsymptoms$"_data"

### percentages

perGroc <- with(data4, table(think_covid,groceries))
pGroc <- prop.table(perGroc, 1)*100 

perShop <- with(data4, table(think_covid, shop_other))
pShop <- prop.table(perShop, 1)*100

perMeet <- with(data4, table(think_covid, meetup))
pMeet <- prop.table(perMeet, 1)*100

perSymp <- with(data4, table(think_covid,symptoms))
pSymp <- prop.table(perSymp, 1)*100

#Creating one list

listfull3 <- list(datagroceries,
                 datashop,
                 datameetup,
                 datasymptoms)
listfull3 <- rbindlist(listfull3, use.name=FALSE)
listfull3 <- as_tibble(listfull3)
listfull3 <- listfull3 

ptibble4 <- rbind(pGroc, pShop, pMeet, pSymp)
ptibble4 <- as_tibble(ptibble4)
ptibble4 <- data.frame(lapply(ptibble4, function(x) if(is.numeric(x)) round(x,1) else x))

#Combining percentages and numbers

tibble4 <- cbind(listfull3, ptibble4)

tibble4$nocovid <- paste0(tibble4$No, " (", tibble4$On.one.or.fewer.days.in.the.last.week.n...2389, ")")

tibble4$yescovid <- paste0(tibble4$Yes, " (", tibble4$On.two.or.more.days.in.the.last.week.n...3760, ")")

tibble4 <- subset(tibble4, select = c(groceries, nocovid, yescovid)) %>% 
  rename("Self reported behaviour" = groceries)

gtfull4 <- gt(tibble4) %>% 
  tab_spanner(
    label = "Think COVID-19",
    columns = c(2:3)
  ) %>%
  cols_label(
    "nocovid" = "Think have not had COVID-19"
  ) %>% 
  cols_label(
    "yescovid" = "Think have had COVID-19"
  ) %>% 
  tab_row_group(
    label = "Shopping for groceries/pharmacy",
    rows = 1:2
  ) %>% 
  tab_row_group(
    label = "Shopping for items other than groceries/pharmacy",
    rows = 3:4
  ) %>%
  tab_row_group(
    label = "Meeting up with friends or family",
    rows = 5:6
  ) %>%
  tab_row_group(
    label = "Correct identification of cough and fever",
    rows = 7:8
  ) %>%
  row_group_order(groups = c("Shopping for groceries/pharmacy", 
                             "Shopping for items other than groceries/pharmacy",
                             "Meeting up with friends or family",
                             "Correct identification of cough and fever"
                             )) %>% 
  tab_style(cell_text(weight = "bold"), 
            locations = cells_column_labels(columns = everything())) %>% 
  tab_style(cell_text(weight = "bold"),
            locations = cells_column_spanners()) %>% 
  tab_style(
    style = list(
      cell_text(weight = "bold"),
      cell_fill()),
    locations = cells_row_groups())