diff --git a/General Health/3. General Health Outputs.R b/General Health/3. General Health Outputs.R index a371007..a549d8d 100755 --- a/General Health/3. General Health Outputs.R +++ b/General Health/3. General Health Outputs.R @@ -13,6 +13,8 @@ library(cowplot) library(gridExtra) library(png) +library(flextable) +library(officer) # Determine locality (for testing only) # LOCALITY <- "Inverness" @@ -240,7 +242,7 @@ avg_life_exp_latest_male <- ifelse( locality_missing, NA_real_, filter( - life_exp, + life_exp, sex == "Male", year == latest_year_life_exp_loc, area_name == LOCALITY, @@ -254,7 +256,7 @@ avg_life_exp_latest_fem <- ifelse( locality_missing, NA_real_, filter( - life_exp, + life_exp, sex == "Female", year == latest_year_life_exp_loc, area_name == LOCALITY, @@ -894,49 +896,31 @@ loc.ltc.table <- str_wrap(glue("{LOCALITY} Locality"), width = if_else(n_loc < 5 hscp.ltc.table <- str_wrap(glue("{HSCP} HSCP"), width = 25) - -ltc_loc_col <- tableGrob(top5ltc_loc[, 1], - cols = loc.ltc.table, - rows = 1:5, - theme = ttheme_default( - core = list(bg_params = list(fill = top5ltc_loc$colours), fg_params = list(col = "white", fontface = 2, fontsize = 11)), - colhead = list(bg_params = list(fill = "white"), fg_params = list(fontface = 3, fontsize = 11)) - ) -) -ltc_hscp_col <- tableGrob(top5ltc_hscp[, 1], - cols = hscp.ltc.table, - rows = NULL, - theme = ttheme_default( - core = list(bg_params = list(fill = top5ltc_hscp$colours), fg_params = list(col = "white", fontface = 2, fontsize = 11)), - colhead = list(bg_params = list(fill = "white"), fg_params = list(fontface = 3, fontsize = 11)) - ) -) -ltc_scot_col <- tableGrob(top5ltc_scot[, 1], - cols = "Scotland", - rows = NULL, - theme = ttheme_default( - core = list(bg_params = list(fill = top5ltc_scot$colours), fg_params = list(col = "white", fontface = 2, fontsize = 11)), - colhead = list(bg_params = list(fill = "white"), fg_params = list(fontface = 3, fontsize = 11)) - ) -) - -## Combine columns -top5ltc_all_table <- as_gtable(gtable_combine(ltc_loc_col, ltc_hscp_col, ltc_scot_col)) - -title <- ggdraw() + - draw_label(str_wrap( - glue("Top 5 most prevalent Physical Long-Term Conditions {latest_year_ltc}"), - width = 65 - ), size = 11, fontface = "bold") - -top5_ltc_table <- plot_grid(title, top5ltc_all_table, nrow = 2, rel_heights = c(0.1, 1.2)) - +# Top5 LTC table as a table (instead of image) - almost there... +top5_ltc_table <- bind_cols( + select(top5ltc_loc, {{ loc.ltc.table }} := Prevalence), + select(top5ltc_hscp, {{ hscp.ltc.table }} := Prevalence), + select(top5ltc_scot, "Scotland" = Prevalence) +) |> + flextable(cwidth = 2) %>% + add_header_lines( + values = str_wrap(glue("Top 5 most prevalent Physical Long-Term Conditions {latest_year_ltc}"), width = 65) + ) %>% + bg(j = 1, bg = top5ltc_loc$colours) %>% + bg(j = 2, bg = top5ltc_hscp$colours) %>% + bg(j = 3, bg = top5ltc_scot$colours) %>% + fontsize(size = 16, part = "header") %>% + fontsize(size = 12, part = "body") %>% + font(fontname = "Arial", part = "all") %>% + color(color = "white", part = "body") %>% + bold(part = "header") %>% + italic(part = "header") %>% + border(border = fp_border(color = "white", width = 5), part = "body") rm( - ltc_cols, ltc_loc_col, ltc_hscp_col, ltc_scot_col, - ltc_pops_total_loc, ltc_pops_total_hscp, - loc.ltc.table, hscp.ltc.table, - top5ltc_loc, top5ltc_hscp, top5ltc_scot, top5ltc_all_table, title + ltc_cols, ltc_pops_total_loc, ltc_pops_total_hscp, + loc.ltc.table, loc.ltc.table.wrapped, hscp.ltc.table, hscp.ltc.table.wrapped, + top5ltc_loc, top5ltc_hscp, top5ltc_scot ) ## Objects for text