File: wrap_flextable.Rd

package info (click to toggle)
r-cran-flextable 0.9.11-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 2,296 kB
  • sloc: javascript: 28; sh: 15; makefile: 2
file content (190 lines) | stat: -rw-r--r-- 6,726 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
% Generated by roxygen2: do not edit by hand
% Please edit documentation in R/patchwork.R
\name{wrap_flextable}
\alias{wrap_flextable}
\title{Wrap a flextable for use with patchwork}
\usage{
wrap_flextable(
  x,
  panel = c("body", "full", "rows", "cols"),
  space = c("free", "free_x", "free_y", "fixed"),
  n_row_headers = 0L,
  flex_body = FALSE,
  flex_cols = FALSE,
  expand = 0.6,
  just = c("left", "right", "center")
)
}
\arguments{
\item{x}{A flextable object.}

\item{panel}{What portion of the table should be aligned with the panel
region? \code{"body"} means that header and footer will be placed outside the
panel region. \code{"full"} means that the whole table will be placed inside the
panel region. \code{"rows"} keeps all rows inside the panel but is otherwise
equivalent to \code{"body"}. \code{"cols"} places all columns within the panel region
but keeps column headers on top.}

\item{space}{How should the dimension of the table influence the final
composition? \code{"fixed"} means that the table width and height will set the
dimensions of the area it occupies. \code{"free"} means the table dimensions will
not influence the sizing. \code{"free_x"} and \code{"free_y"} allow freeing either
direction.}

\item{n_row_headers}{Number of leading columns to treat as row headers.
These columns will be placed outside the panel region and will not
participate in alignment with the plot axes.}

\item{flex_body}{If \code{TRUE}, the table body row heights become flexible:
the adjacent ggplot determines the panel height and the body rows
stretch equally to fill it. Header and footer keep their fixed size.
This is useful to align table rows with discrete bars or categories
in a neighbouring plot. Implies \code{free_y} for \code{space}.}

\item{flex_cols}{If \code{TRUE}, the data column widths (all columns after
\code{n_row_headers}) become flexible: the adjacent ggplot determines the
panel width and the data columns stretch to fill it. Row header
columns keep their fixed width. This is useful to align table columns
with discrete x-axis categories in a neighbouring plot.
Implies \code{free_x} for \code{space}.}

\item{expand}{Expansion value matching the ggplot discrete axis expansion
(\code{ggplot2::expansion(add = expand)}). Default is \code{0.6}, which is the
ggplot2 default for discrete axes. Only used when \code{flex_cols = TRUE}.}

\item{just}{Horizontal alignment of the table within its patchwork panel.
One of \code{"left"} (default), \code{"right"}, or \code{"center"}. Useful when the
table is narrower than the available panel width.
Ignored when \code{flex_cols = TRUE} (columns fill the panel).}
}
\value{
A patchwork-compliant object that can be combined with ggplot2 plots
using \code{+}, \code{|}, or \code{/} operators.
}
\description{
This function wraps a flextable as a patchwork-compliant patch, similar
to what \code{\link[patchwork:wrap_table]{patchwork::wrap_table()}} does for gt tables. It allows flextable
objects to be combined with ggplot2 plots in a patchwork layout, with
optional alignment of table headers and body with plot panel areas.

Note this is experimental and may change in the future.
}
\examples{
\dontshow{if (requireNamespace("patchwork", quietly = TRUE) && requireNamespace("ggplot2", quietly = TRUE) && requireNamespace("ragg", quietly = TRUE)) withAutoprint(\{ # examplesIf}
library(gdtools)
font_set_liberation()
library(ggplot2)
library(patchwork)

set_flextable_defaults(
  font.family = "Liberation Sans",
  font.size = 10,
  big.mark = "",
  border.color = "grey60"
)

# Adapted from <https://r-graph-gallery.com/web-dumbell-chart.html>

dataset <- data.frame(
  team = c(
    "FC Bayern Munchen", "SV Werder Bremen", "Borussia Dortmund",
    "VfB Stuttgart", "Borussia M'gladbach", "Hamburger SV",
    "Eintracht Frankfurt", "FC Schalke 04", "1. FC Koln",
    "Bayer 04 Leverkusen"
  ),
  matches = c(2000, 1992, 1924, 1924, 1898, 1866, 1856, 1832, 1754, 1524),
  won     = c(1206,  818,  881,  782,  763,  746,  683,  700,  674,  669),
  lost    = c( 363,  676,  563,  673,  636,  625,  693,  669,  628,  447)
)
dataset$win_pct  <- dataset$won  / dataset$matches * 100
dataset$loss_pct <- dataset$lost / dataset$matches * 100
dataset$team <- factor(dataset$team, levels = rev(dataset$team))

# -- dumbbell chart --
pal <- c(lost = "#EFAC00", won = "#28A87D")
df_long <- reshape(dataset, direction = "long",
  varying = list(c("loss_pct", "win_pct")),
  v.names = "pct", timevar = "type",
  times = c("lost", "won"), idvar = "team"
)

p <- ggplot(df_long, aes(x = pct / 100, y = team)) +
  stat_summary(
    geom = "linerange", fun.min = "min", fun.max = "max",
    linewidth = .7, color = "grey60"
  ) +
  geom_point(aes(fill = type), size = 4, shape = 21,
    stroke = .8, color = "white"
  ) +
  scale_x_continuous(
    labels = scales::percent,
    expand = expansion(add = c(.02, .02))
  ) +
  scale_y_discrete(name = NULL, guide = "none") +
  scale_fill_manual(
    values = pal,
    labels = c(lost = "Lost", won = "Won")
  ) +
  labs(x = NULL, fill = NULL) +
  theme_minimal(base_family = "Liberation Sans", base_size = 10) +
  theme(
    legend.position = "top",
    legend.justification = "left",
    panel.grid.minor = element_blank(),
    panel.grid.major.y = element_blank()
  )

# -- flextable --
ft_dat <- dataset[, c("matches", "win_pct", "loss_pct", "team")]
ft_dat$team <- as.character(ft_dat$team)

ft <- flextable(ft_dat)
ft <- border_remove(ft)
ft <- bold(ft, part = "header")
ft <- colformat_double(ft, j = c("win_pct", "loss_pct"),
  digits = 1, suffix = "\%"
)
ft <- set_header_labels(ft,
  team = "Team", matches = "GP",
  win_pct = "", loss_pct = ""
)
ft <- color(ft, color = "#28A87D", j = 2)
ft <- color(ft, color = "#EFAC00", j = 3)
ft <- bold(ft, bold = TRUE, j = 2:3)
ft <- italic(ft, italic = TRUE, j = 4)
ft <- align(ft, align = "right", part = "all")
ft <- autofit(ft)

\dontshow{
cap <- ragg::agg_capture(width = 7, height = 6, units = "in", res = 150)
grDevices::dev.control("enable")
}
print(
  wrap_flextable(ft, flex_body = TRUE, just = "right") +
    p + plot_layout(widths = c(1.1, 2))
)
\dontshow{
raster <- cap()
dev.off()
plot(as.raster(raster))
init_flextable_defaults()
}
\dontshow{\}) # examplesIf}
}
\seealso{
Other flextable print function: 
\code{\link{df_printer}()},
\code{\link{flextable_to_rmd}()},
\code{\link{gen_grob}()},
\code{\link{htmltools_value}()},
\code{\link{knit_print.flextable}()},
\code{\link{plot.flextable}()},
\code{\link{print.flextable}()},
\code{\link{save_as_docx}()},
\code{\link{save_as_html}()},
\code{\link{save_as_image}()},
\code{\link{save_as_pptx}()},
\code{\link{save_as_rtf}()},
\code{\link{to_html.flextable}()}
}
\concept{flextable print function}