Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace gridExtra 'image' table with {flextable} table #133

Open
wants to merge 8 commits into
base: development
Choose a base branch
from
Open
70 changes: 27 additions & 43 deletions General Health/3. General Health Outputs.R
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
library(cowplot)
library(gridExtra)
library(png)
library(flextable)
library(officer)

# Determine locality (for testing only)
# LOCALITY <- "Inverness"
Expand Down Expand Up @@ -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,
Expand All @@ -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,
Expand Down Expand Up @@ -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
Expand Down