SyntaxStudy
Sign Up
R Tables, Figures, and Cross-References
R Beginner 2 min read

Tables, Figures, and Cross-References

Presenting data in well-formatted tables is one of the most common tasks in R Markdown. The knitr::kable() function converts any data frame or matrix to a Markdown table with options for column alignment, number formatting, and captions. The kableExtra package extends kable with Bootstrap styling, striped rows, column highlights, spanning headers, and footnotes for HTML output, and equivalent LaTeX formatting for PDF output. The gt and flextable packages offer even richer table customisation with a grammar similar to ggplot2. Figures produced by ggplot2 or base R graphics are automatically captured and embedded in the output. Chunk options fig.cap provides a caption, fig.align = "center" centres the figure, out.width and out.height control the displayed size (in percentages or absolute units), and fig.path sets the directory where image files are saved. When producing multiple plots in one chunk, the fig.show option can be set to "hold" to collect all figures and display them at the end of the chunk. The bookdown package extends R Markdown with numbered figures, tables, equations, and cross-referencing. Labelling a chunk with a fig. prefix (e.g., ```{r fig-scatter, ...}```) allows you to reference the figure anywhere in the document with \@ref(fig:fig-scatter). Tables labelled with a tab. prefix work similarly. This cross-referencing system is essential for long-form documents, theses, and journal articles where consistent numbering is required.
Example
# --- knitr::kable example chunk ---
# ```{r summary-table}
# library(knitr)
# library(kableExtra)
# library(dplyr)
# data(mtcars)
#
# summary_tbl <- mtcars |>
#   group_by(cyl) |>
#   summarise(
#     n        = n(),
#     avg_mpg  = round(mean(mpg), 1),
#     avg_hp   = round(mean(hp), 1),
#     avg_wt   = round(mean(wt), 2)
#   )
#
# kable(summary_tbl,
#       caption = "Summary statistics by cylinder count",
#       col.names = c("Cylinders","Count","Avg MPG","Avg HP","Avg Weight")) |>
#   kable_styling(bootstrap_options = c("striped", "hover"),
#                 full_width = FALSE) |>
#   column_spec(1, bold = TRUE) |>
#   footnote(general = "Weight in thousands of pounds.")
# ```

# --- Figure with caption and bookdown cross-reference ---
# ```{r fig-mpg-wt, fig.cap="MPG vs Weight", fig.width=6, fig.height=4}
# library(ggplot2)
# ggplot(mtcars, aes(x = wt, y = mpg, colour = factor(cyl))) +
#   geom_point(size = 3) +
#   geom_smooth(method = "lm", se = FALSE) +
#   labs(x = "Weight (1000 lbs)", y = "Miles per gallon",
#        colour = "Cylinders") +
#   theme_minimal()
# ```
# As shown in Figure \@ref(fig:fig-mpg-wt), heavier cars have lower MPG.

# --- Equation (LaTeX math in text) ---
# The linear model is:
# $$\hat{y} = \beta_0 + \beta_1 x_1 + \beta_2 x_2 + \varepsilon$$

# --- Inline code ---
# The dataset contains `r nrow(mtcars)` observations
# across `r ncol(mtcars)` variables.
# The heaviest car weighs `r max(mtcars$wt)` thousand pounds.