Generates a text table from a dataframe, perfectly aligns its X-axis to match a provided ggplot, and seamlessly stacks them vertically. Allow annotation of general "GEN" plots and more specific plots such as Pharmacokinetic ("PK") or Kaplan-Meier ("KM"). Supports both continuous and discrete X-axes.
df2gg_aligned(
df,
gg_plt,
type = c("GEN", "PK", "KM"),
y_labels = NULL,
title = NULL,
xlab = NULL,
show_xaxis = FALSE,
text_size = 3.5,
label_size = 10,
rel_height_plot = 0.75
)(data.frame)
The summary table to render.
(ggplot2)
The main plot to align to and stack on top of.
(character)
The type of plot/table.
Can be "GEN", "PK" (use the first column for Y-axis), or
"KM" (uses rownames for Y-axis).
Default is "GEN".
(character or expression)
Formatted labels override.
(character or NULL)
Optional title for the table.
(character or NULL)
Optional X-axis label for the table.
(logical)
Should the table display an X-axis line?
(numeric)
Size of the data values in the table.
(numeric)
Size of the axis and title text.
(numeric)
Vertical space for the main plot.
A combined cowplot object.
The original plots are stored in attr(result, "plotlist") as a named
list (main and table) for downstream data extraction.
if (FALSE) { # \dontrun{
# 1. Create a base plot with a continuous X-axis
p_base <- ggplot(mtcars, aes(x = mpg, y = disp)) +
geom_point() +
scale_x_continuous(limits = c(10, 35), breaks = c(10, 20, 30))
# 2. Create a mock summary table matching the X-axis breaks
mock_df <- data.frame(
`10` = c("15", "12"),
`20` = c("10", "8"),
`30` = c("5", "3"),
check.names = FALSE
)
rownames(mock_df) <- c("Group A", "Group B")
# 3. Align and stack the table under the plot using the "KM" engine
result <- df2gg_aligned(
df = mock_df,
gg_plt = p_base,
type = "KM",
title = "Subjects at Risk",
show_xaxis = TRUE
)
# 4. Extract the original plots and data
plist <- attr(result, "plotlist")
plist$main$data # original dataframe from the base plot
plist$table # the table ggplot object
} # }