Caption above figure in html Rmarkdown
For HTML output, you may set the chunk option fig.topcaption = TRUE
to place captions above figures. Below is a minimal example (it works for both html_document
and bookdown's HTML output formats):
---
title: "Reprex"
output:
html_document: null
bookdown::html_document2: null
---
```{r, fig.cap='A caption.', fig.topcaption=TRUE}
plot(cars)
```
Caption above figure using knitr (LaTeX/PDF)
Try using hook:
<<include=FALSE>>=
f <- function(x, options) {
paste("\\end{kframe}\n",
"\\caption{", options$capT, "}\n",
hook_plot_tex(x, options),
"\n\\begin{kframe}", sep = "")
}
knit_hooks$set(plot = f)
@
\begin{figure}
<<a-plot, echo=TRUE, capT="cap, cap, and cap">>=
plot(1)
@
\end{figure}
Figures captions and labels in knitr
You can achieve this by including fig_caption: yes
in the header:
---
title: "Plotting"
output:
pdf_document:
fig_caption: yes
---
```{r figs, echo=FALSE, fig.width=7,fig.height=6,fig.cap="\\label{fig:figs}plotting example"}
par(mfrow=c(2,2))
plot(1:10, col=2)
plot(density(runif(100, 0.0, 1.0)))
plot(runif(100, 0.0, 1.0),type="l")
```
in Figure \ref{fig:figs} we see examples of plotting in R.
Note that the figure caption label should be included in the caption with a double backslash, as shown above.
How to make a figure caption in Rmarkdown?
Please see the documentation of R Markdown for PDF output, and in particular, look for fig_caption
. Figure captions are turned off by default in R Markdown, and you have to turn them on (fig_caption: true
). You can also find this setting from the gear button on the toolbar of RStudio IDE.
How to hold figure position with figure caption in pdf output of knitr?
As Yihui mentioned in his answer (Figure position in markdown when converting to PDF with knitr and pandoc), we cannot expect too much about formatting from mardown. To workaround this problem, just write some R scripts to replace htbp
to H
.
Compared with knit
from knitr package, I found render
from rmarkdown is better to export a tex
file. Just remember to add keep_tex: yes
in the yaml header of your rmarkdown file.
library(rmarkdown)
render('filepath.Rmd')
x <- readLines('filepath.tex')
pos <- grep('begin\\{figure\\}\\[htbp\\]', x)
x[pos] <- gsub('htbp', 'H', x[pos])
writeLines(x, 'filepath.tex')
tools::texi2pdf('filepath.tex', clean = TRUE) # gives foo.pdf
file.remove('filepath.tex')
Rmarkdown figure caption not printing correctly
It seems like a code chunk problem. Try this:
```{r top_3pt_scorers, echo = FALSE}
top_3pt_shooters <- NBAdf %>%
filter(PTS_TYPE == 3) %>%
group_by(PLAYER_NAME) %>%
dplyr::summarize(Threes_attempted = n(), Threes_made = sum(as.numeric(FGM)),
Three_point_total = sum(PTS, na.rm = TRUE))
knitr::kable(head(top_3pt_shooters[order(top_3pt_shooters$Threes_made, decreasing = TRUE), ]),
caption = "The top 3 point scorers in the NBA")
```
You may use fig_caption = TRUE
in your YAML header.
This may help you:
---
title: "Caption"
author: "bttomio"
date: "`r format(Sys.time(), '%d %B, %Y')`"
output:
pdf_document:
fig_caption: true
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = FALSE)
library(kableExtra)
```
## Caption with kable
```{r dt}
dt <- mtcars[1:5, 1:6]
kbl(dt, caption = "Demo table", booktabs = T) %>%
kable_styling(latex_options =c("striped", "hold_position"))
```
```{r dt2}
dt <- mtcars[1:5, 1:6]
kbl(dt, caption = "Same demo table", booktabs = T) %>%
kable_styling(latex_options =c("striped", "hold_position"))
```
-output
Related Topics
Get Name of X When Defining '(<-' Operator
How to Manipulate Null Elements in a Nested List
Getting Both Column Counts and Proportions in the Same Table in R
How Calculate Growth Rate in Long Format Data Frame
Trycatch with Parlapply (Parallel Package) in R
Warning: Unable to Access Index for Repository Https://Www.Stats.Ox.Ac.Uk/Pub/Rwin/Src/Contrib:
Find the Index of the Column in Data Frame That Contains the String as Value
Provide Shades Between Dates on X Axis
Remove White Space Between Plots and Table in Grid.Arrange
How to Plot the Linear Regression in R
How to Pass Data Between Functions in a Shiny App
Logistic Regression with Robust Clustered Standard Errors in R
R - How to Add Row Index to a Data Frame, Based on Combination of Factors
How to Collapse Sidebarpanel in Shiny App