Skip to content

Instantly share code, notes, and snippets.

@davidgohel
Created September 9, 2022 12:13
Show Gist options
  • Save davidgohel/eaebc7d00f63166976fd2b29f8cb0abf to your computer and use it in GitHub Desktop.
Save davidgohel/eaebc7d00f63166976fd2b29f8cb0abf to your computer and use it in GitHub Desktop.
demo of the new grid graphic output of flextable
#remotes::install_github("davidgohel/officer")
#remotes::install_github("davidgohel/flextable")
library(ggplot2)
library(flextable)
library(patchwork)
library(tidyverse)
main <- ggplot(data = diamonds, aes(x= price, y = x, color = color)) +
geom_point() +
theme_minimal()
myscale <- scales::col_numeric(domain = NULL, palette = "RdBu")
agg_data <- group_by(diamonds, color) %>%
summarise(across(x:z, list(avg = mean, sd = sd)))
sub <- agg_data %>%
flextable() %>%
separate_header() %>%
colformat_double(digits = 2) %>%
autofit() %>%
valign(valign = "center", part = "header") %>%
bg(bg = "#CCCCCC", part = "all") %>%
flextable::color(color = myscale, j = grep("avg$", colnames(agg_data), value = TRUE)) %>%
gen_grob(fit = "width", scaling = "min", autowidths = FALSE,
just = "center")
z <- main + inset_element(sub, 0.5, 0.15, .95, .5)+
theme(rect = element_rect(fill="transparent"))
ragg::agg_png(filename = "flextable.png", width = 10, height = 7,
res = 300, units = "in")
print(z)
dev.off()
@davidgohel
Copy link
Author

Expected result:

flextable

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment