library(readxl)
library(tidyverse)
library(gt)
library(janitor)
library(ggplot2)
library(forcats)


d <- read_excel("data.xlsx")
d <- d %>%
  clean_names() %>%
  mutate (sheltered = as.numeric(sheltered),
          unsheltered = as.numeric(unsheltered))

head(d)
## # A tibble: 6 x 10
##   category             sheltered transitional unsheltered total x2021_winter_pit
##   <chr>                    <dbl>        <dbl>       <dbl> <dbl> <chr>           
## 1 Total Number of Hou~       126            0          44   170 237             
## 2 Total Number of Per~       152            0          46   198 276             
## 3 Number of Children ~        22            0           0    22 37              
## 4 Number of Young Adu~         5            0           1     6 16              
## 5 Number of Adults (O~       125            0          45   170 221             
## 6 Number of Persons w~         0            0           0     0 2               
## # ... with 4 more variables: percent_change <dbl>, x8 <lgl>, x9 <lgl>,
## #   x10 <lgl>

Summary Table By Month

This creates a table with 69 rows, which is pretty hard to make sense of

d_filter <- d %>%
  select(-x8, -x9, -x10, -transitional) %>%
  slice(1:5, 7:9, 13:14, 16:18, 20, 22, 24:29)

gt_d <- gt(d_filter) %>%
  tab_header(title = "Summary of PIT Data") %>%
  fmt_number(column = percent_change, decimals = 2) %>%
  cols_align(align = "center") %>%
  cols_label(category = "",
             sheltered = "Sheltered",
             unsheltered = "Unsheltered",
             percent_change = "% Change",
             x2021_winter_pit = "2021 Winter PIT",
             total = "Total"
             ) %>%
  tab_row_group(label = "Chronic Homeless", rows = 17:18) %>%
  tab_row_group(label = "Veterans", rows = 19:21)%>%
  tab_row_group(label = "Ethnicity & Race", rows = 9:16) %>%
  tab_row_group(label = "Age", rows = 3:5) %>%
  tab_row_group(label = "Gender", rows =  6:8) %>%
  tab_row_group(label = "Totals", rows = 1:2) %>%
  tab_spanner(label = "2021 Summer PIT", columns = c(sheltered, unsheltered, total))
gt_d
Summary of PIT Data
2021 Summer PIT 2021 Winter PIT % Change
Sheltered Unsheltered Total
Totals
Total Number of Households 126 44 170 237 −0.28
Total Number of Persons (Adults and Children) 152 46 198 276 −0.28
Gender
Female 65 6 71 100 −0.29
Male 85 38 123 174 −0.29
Transgender 2 0 2 1 0.00
Age
Number of Children (Under Age 18) 22 0 22 37 −0.41
Number of Young Adults (Age 18-24) 5 1 6 16 −0.62
Number of Adults (Over Age 24) 125 45 170 221 −0.23
Ethnicity & Race
Non-Hispanic/Non-Latino 143 44 187 261 −0.28
Hispanic/Latino 4 2 6 10 −0.40
Missing / Non-HUD 1 0 1 5 −0.80
White 89 25 114 157 −0.27
Black or African-American 57 12 69 93 −0.26
American Indian or Alaska Native 1 0 1 4 −0.75
Multiple Races 3 0 3 19 −0.84
Missing / Non-HUD 2 0 2 3 −0.33
Veterans
Total Number of Households 22 4 26 30 −0.13
Total Number of Persons 22 4 26 32 −0.19
Total Number of Veterans 22 4 26 30 −0.13
Chronic Homeless
Total Number of Households 28 0 28 44 −0.36
Total Number of Persons 28 0 28 44 −0.36
# gtsave(data = gt_d, filename = "sum_table.rtf")
d_plot <- d_filter %>%
  select(category, sheltered, unsheltered, total) %>%
  mutate(Sheltered = sheltered,
         Unsheltered = unsheltered,
         Total = total) %>%
  slice(2)

d_long <- d_plot %>%
  pivot_longer(cols = c(Sheltered, Unsheltered, Total))%>%
  mutate(name = factor(name, levels = c(
                        "Sheltered", "Unsheltered", "Total")))

total_plot <- ggplot(data = d_long, aes(x = name, y = value, fill = name)) +
  geom_col() +
  geom_text(aes(label = value), vjust = 1.5)+
  labs(title = "Total Number of Adults and Children",
       y = "Number of Individuals",
       x = "") +
  theme_minimal()+
  theme(legend.position = "none",
        plot.title = element_text(hjust = 0.5))
print(total_plot)

# ggsave(plot = total_plot, filename = "total_plot.jpg" )