Rmarkdown 图形标题打印不正确

Rmarkdown figure caption not printing correctly

我目前正在使用 Rstudio 和 运行 此代码:

{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")

但我得到了这个输出:

而我的理想输出是:

我需要更改 kable 参数中的某些内容吗?

解决方案:

为了解决这个问题,我只需要更改以下代码:

{r top_3pt_scorers, echo = FALSE}

{r top3ptscorers, echo = FALSE}

好像是代码块问题。试试这个:

```{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")
```

您可以在 YAML header 中使用 fig_caption = TRUE

这可能对您有帮助:

---
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"))
```

-输出