knitr::kable(
mtcars[1:6, 1:6], caption = 'A subset of mtcars.'
)
A subset of mtcars.
mpg | cyl | disp | hp | drat | wt | |
---|---|---|---|---|---|---|
Mazda RX4 | 21.0 | 6 | 160 | 110 | 3.90 | 2.620 |
Mazda RX4 Wag | 21.0 | 6 | 160 | 110 | 3.90 | 2.875 |
Datsun 710 | 22.8 | 4 | 108 | 93 | 3.85 | 2.320 |
Hornet 4 Drive | 21.4 | 6 | 258 | 110 | 3.08 | 3.215 |
Hornet Sportabout | 18.7 | 8 | 360 | 175 | 3.15 | 3.440 |
Valiant | 18.1 | 6 | 225 | 105 | 2.76 | 3.460 |
The package provides two custom hooks for figure placement. The first is marginfigure
. Set marginfigure = TRUE
in a chuck option to place a figure in the right margin. Optionally, specify the figure size and include a caption. Captions are passed as strings through fig.cap
in the chunk options.
library(ggplot2)
ggplot(mtcars, aes(y = mpg, x = wt)) + geom_point() + stat_smooth(method = "lm",
color = "black", fill = "gray")
This is a marginfigure
The html documents have the body set at a fixed width of 960px. Feel free to edit the css to suit your needs. Html output supports any of the built in Bootstrap themes. Be careful using the fluid grid system, it may break the output for narrow screens.
The second custom hook is fig.star
. Setting fig.star = TRUE
creates a full-width figure spanning the main body and the margin. The caption goes in the sidebar under the figure. These look pretty sweet! Specify the width and height for best results.
ggplot(faithful, aes(y = eruptions, x = waiting)) + geom_point() + stat_smooth(method = "loess",
, color = "black", fill = "gray")
Full-width figure
Finally, normal figures are plotted in the main body, with the captions in the margin. The only option necessary here is the caption itself.
ggplot(faithful, aes(x = eruptions)) + geom_histogram(binwidth = 0.1, fill = "gray") +
geom_hline(yintercept = seq(5, 30, 5), col = "white", lwd = 1)
Normal figure with caption in the margin