【问题标题】:Rmarkdown figure caption not printing correctlyRmarkdown 图形标题打印不正确
【发布时间】:2021-02-22 21:32:43
【问题描述】:

我目前正在使用 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}

【问题讨论】:

  • 您在寻找哪个数字标题?它正在打印您的代码块的名称。也许它是您的 .Rmd 文件中的其他内容(例如 YAML 标头)?
  • 也许你需要添加类似this的东西?
  • 感谢您的链接,但我是表格标题而不是数字,例如“表 1:NBA 前 3 分得分手”及其下方的表格
  • 它在另外两个表上工作正常,但其余的都不能工作
  • 我刚刚添加了截图以便更好地解释

标签: r r-markdown rstudio


【解决方案1】:

这似乎是一个代码块问题。试试这个:

```{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 标头中使用 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"))
```

-输出

【讨论】:

  • 我刚刚尝试了您的解决方案,但它们对我来说没有成功,它只是输出相同的问题。
  • 您能否提供一个最小的可重现示例?如果不知道.Rmd 文件的其他部分,就很难知道发生了什么。谢谢
  • 我刚刚在帖子中添加了我正在使用的所有内容。
  • 我找到了解决方案!所以它不喜欢{r top_3pt_scorers},我不得不把它改成{r top3ptscorers} 它似乎不喜欢下划线
  • 我明白了,很高兴听到!您可以编辑我的答案或发布一个新的答案来解释您的解决方案。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-02-04
  • 2020-05-28
  • 1970-01-01
  • 2013-02-08
  • 1970-01-01
  • 1970-01-01
  • 2022-01-23
相关资源
最近更新 更多