---
# System prepended metadata

title: figure 2

---

# figure 2

data2 <- data%>% 
  rename(immunity = q8haveimmunity,
         gshop = Adhere_shop_groceries,
         oshop = Adhere_shop_other,
         friends = Adhere_meet_friends,
         outings = Going_out_total,
         noworry = q9worry,
         rself = q10arisk,
         rothers = q10brisk,
         covidknow = Sx_covid_nomissing,
         think_covid = Ever_covid) %>% 
  select(immunity, 
         gshop, 
         oshop, 
         friends, 
         outings,
         noworry,
         rself,
         rothers,
         covidknow,
         think_covid) %>% 
  mutate(think_covid = case_when(think_covid == 0 ~ "Think have not had COVID-19",
                                         think_covid == 1 ~ "Think have had COVID-19")) 


#Making individual datasets

#Count for thinking have had/have not had COVID-19
covid <- data2 %>% 
  select(immunity, think_covid) %>% 
  group_by(think_covid) %>% 
  count() %>% 
  rename(covidtotal = n)

#immunity
dimmunity <- data2 %>% 
  select(immunity, think_covid) %>% 
  filter(immunity == "5") %>%
  mutate(immunity = case_when(immunity == 5 ~ "Strongly agree that they are immune")) %>% 
  group_by(think_covid, immunity) %>% 
  count() %>% 
  ungroup() %>% 
  rename("stronglyagree" = "n")

#trying to create third column for percentages

#left joining covid and dimmunity

dimmunity <- merge(x = dimmunity, y = covid, by = "think_covid") %>% 
  mutate(strongpercent = stronglyagree*100/covidtotal)

#groceryshopping percentage table

dgshop <- data2 %>% 
  select(gshop, think_covid) %>% 
  filter(gshop == 0) %>% 
  mutate(gshop = case_when(gshop == 0 ~ "Shopping for groceries/pharmacy two or more times")) %>% 
  group_by(think_covid, gshop) %>% 
  count() %>% 
  ungroup() %>% 
  rename("shopmorethan1" = "n")

dgshop <- merge(x = dgshop, y = covid, by = "think_covid") %>% 
  mutate(gshoppercent = shopmorethan1*100/covidtotal)

#othershopping

doshop <- data2 %>% 
  select(oshop, think_covid) %>% 
  filter(oshop == 0) %>% 
  mutate(oshop = case_when(oshop == 0 ~ "Shopping for items other than groceries/pharmacy")) %>% 
  group_by(think_covid, oshop) %>% 
  count() %>% 
  ungroup() %>% 
  rename("othershop" = "n")

doshop <- merge(x = doshop, y = covid, by = "think_covid") %>% 
  mutate(othershoppercent = othershop*100/covidtotal)

#friends

dfriends <- data2 %>% 
  select(friends, think_covid) %>% 
  filter(friends == 0) %>% 
  mutate(friends = case_when(friends == 0 ~ "Met up with friends/family they do not live with")) %>% 
  group_by(think_covid, friends) %>% 
  count() %>% 
  ungroup() %>% 
  rename("metfriends" = "n")

dfriends <- merge(x = dfriends, y = covid, by = "think_covid") %>% 
  mutate(metfriendspercent = metfriends*100/covidtotal)

#outings

doutings <- data2 %>% 
  select(outings, think_covid) %>%
  filter(outings > 7) %>% 
  mutate(outings = case_when(outings > 7 ~ "Out-of-home activity, 8 or more outings")) %>% 
  group_by(think_covid, outings) %>% 
  count() %>% 
  ungroup() %>% 
  rename("moreoutings" = "n")

doutings <- merge(x = doutings, y = covid, by = "think_covid") %>% 
  mutate(more8outingspercent = moreoutings*100/covidtotal)

#noworry

dnoworry <- data2 %>% 
  select(noworry, think_covid) %>% 
  filter(noworry == 1) %>% 
  mutate(noworry = case_when(noworry == 1 ~ "Not worried at all")) %>% 
  group_by(think_covid, noworry) %>% 
  count() %>% 
  ungroup() %>% 
  rename("notworry" = "n")

dnoworry <- merge(x = dnoworry, y = covid, by = "think_covid") %>% 
  mutate(notworrypercent = notworry*100/covidtotal)

#risktoself

drself <- data2 %>% 
  select(rself, think_covid) %>% 
  filter(rself == 1) %>% 
  mutate(rself = case_when(rself == 1 ~ "Perceive no risk at all to themselves")) %>% 
  group_by(think_covid, rself) %>% 
  count() %>% 
  ungroup() %>% 
  rename("noriskself" = "n")

drself <- merge(x = drself, y = covid, by = "think_covid") %>% 
  mutate(noriskselfpercent = noriskself*100/covidtotal)

#risktoothers

drothers <- data2 %>% 
  select(rothers, think_covid) %>% 
  filter(rothers == 1) %>% 
  mutate(rothers = case_when(rothers == 1 ~ "Perceive no risk at all to others")) %>% 
  group_by(think_covid, rothers) %>% 
  count() %>% 
  ungroup() %>% 
  rename("noriskothers" = "n")

drothers <- merge(x = drothers, y = covid, by = "think_covid") %>% 
  mutate(noriskotherspercent = noriskothers*100/covidtotal)

#covidknowledge

dcovidknow <- data2 %>% 
  select(covidknow, think_covid) %>% 
  filter(covidknow == 0) %>% 
  mutate(covidknow = case_when(covidknow == 0 ~ "Did not identify common symptoms")) %>% 
  group_by(think_covid, covidknow) %>% 
  count() %>% 
  ungroup() %>% 
  rename("idcovidsym" = "n")

dcovidknow <- merge(x = dcovidknow, y = covid, by = "think_covid") %>% 
  mutate(idcovidsympercent = idcovidsym*100/covidtotal)

#attempting to merge data into one tibble

listdata2 <- list(dcovidknow,
                  drothers,
                  drself,
                  dnoworry,
                  doutings,
                  dfriends,
                  doshop,
                  dgshop,
                  dimmunity)

tdata2<- rbindlist(listdata2, use.name=FALSE) 
tdata2<- subset(tdata2, select = -c(idcovidsym, covidtotal)) %>% 
  rename("groups"=covidknow) %>% 
  rename(Percentage = idcovidsympercent) %>% 
  group_by(think_covid)
  
#rearranging group variables


#attempting to convert table into plot

plotdata2 <- tdata2 %>% mutate(groups = fct_relevel(groups,
                                       "Strongly agree that they are immune",
                                       "Shopping for groceries/pharmacy two or more times",
                                       "Shopping for items other than groceries/pharmacy",
                                       "Met up with friends/family they do not live with",
                                       "Out-of-home activity, 8 or more outings",
                                       "Not worried at all",
                                       "Perceive no risk at all to themselves",
                                       "Perceive no risk at all to others",
                                       "Did not identify common symptoms")) 
plotdata2_new <- plotdata2
plotdata2_new$think_covid <- factor(plotdata2_new$think_covid,
                        levels = c("Think have not had COVID-19", "Think have had COVID-19" ))
  
  
plot2 <- ggplot(plotdata2_new, aes(fill=think_covid, 
                                y= Percentage,
                                x= groups)) +  
  geom_bar(position ='dodge', stat='identity', width=0.5) +
  scale_fill_grey()+ # scale_color_manual(labels = c("Think have not had COVID-19", "Think have had COVID-19"),
  #values = c("black", "grey")) 
  theme(axis.text.x = element_text(angle = 45, vjust = 0.9, hjust = 1),
        panel.background = element_rect(fill = "white", colour="white"),
        panel.grid.major.y = element_line(size = 0.5, colour="grey"),
        panel.grid.minor.y = element_line(size = 0.5, colour="grey"),
        panel.grid.minor.x = element_blank(),
        panel.grid.major.x = element_blank(),
        legend.position="bottom",
        legend.title=element_blank(),
        axis.title.x = element_blank(),
        axis.title.y = element_text(margin = margin(t = 0, r = 50, b = 0, l = 0))) +
  scale_y_continuous(limits = c(0,60), breaks = c(0,10,20,30,40,50,60))
