Creates a standalone ggplot2 table and overlays it onto a base plot
using cowplot at specified coordinates.
df2gg_floating(
df,
gg_plt,
x = 0.8,
y = 0.8,
w = 0.3,
h = 0.2,
colwidths = NULL,
font_size = 10,
col_labels = TRUE,
col_lab_fontface = "bold",
hline = TRUE,
bg_fill = NULL
)(data.frame)
The data frame to render.
(ggplot2)
The main plot to overlay the table onto.
(numeric)
X-coordinate for the box anchor position (0 to 1).
(numeric)
Y-coordinate for the box anchor position (0 to 1).
(numeric)
Width of the annotation box (0 to 1).
(numeric)
Height of the annotation box (0 to 1).
(numeric)
Numeric vector of relative column widths.
(numeric)
Base font size.
(logical)
Whether to display column labels (header).
(character)
String for the font face of column labels (e.g., "bold").
(logical)
Whether to draw a horizontal line below the column labels.
(character)
Optional color string for the plot background.
A cowplot object representing the combined plot and table.
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
p_base <- ggplot(mtcars, aes(x = wt, y = mpg)) +
geom_point() +
theme_classic()
# 2. Create a mock summary table
mock_df <- data.frame(
Statistic = c("N", "Median", "Mean"),
Value = c("32", "19.2", "20.1")
)
# 3. Float the table on the plot
result <- df2gg_floating(
df = mock_df,
gg_plt = p_base,
x = 0.75,
y = 0.75,
w = 0.35,
h = 0.25,
bg_fill = "white"
)
# 4. Extract the original plots and data
plist <- attr(result, "plotlist")
plist$main$data # original dataframe from the base plot
plist$table # the floating table ggplot object
} # }