在RMarkdown中编织为HTML时,是否可以将标题移到图形上方?似乎可以在PDF中使用,即when knitting to PDF,但我不知道如何将其复制为HTML。我正在使用bookdown
对数字进行编号。
当我运行这样的内容时:
```{r fig.cap= "caption"}
df <- data.frame(letter = letters[1:5], value = 1)
ggplot(df, aes(as(factor(1), value, fill = letters))) +
geom_bar(stat = "identity")
```
标题显示在图的底部,但我想将其显示在图的上方。
答案 0 :(得分:6)
对于HTML输出,您可以设置块选项fig.topcaption = TRUE
,将标题放在图形上方。下面是一个最小的示例(它适用于html_document
和 bookdown 的HTML输出格式):
---
title: "Reprex"
output:
html_document: null
bookdown::html_document2: null
---
```{r, fig.cap='A caption.', fig.topcaption=TRUE}
plot(cars)
```
答案 1 :(得分:4)
您可以这样做,但是如果设置echo = TRUE
,则标题将显示在代码上方...
---
title: "Untitled"
author: "Stéphane Laurent"
date: "29 février 2020"
output: html_document
---
```{r setup, include=FALSE}
knitr::knit_hooks$set(htmlcap = function(before, options, envir) {
if(before) {
paste('<p class="caption">', options$htmlcap, "</p>",sep="")
}
})
```
```{r, echo = FALSE, htmlcap="Hello Dolly"}
library(ggplot2)
ggplot(diamonds,aes(price,carat)) + geom_point()
```
答案 2 :(得分:3)
这不是一个成功的答案,但是代码输出显示,通过包含Stéphane提出的解决方案可以抑制选择 bookdown 格式(否则将对数字进行编号)的选择。下面是dyrland建议的链接的解决方案。
---
title: "Untitled"
author: "Internet"
date: "29 février 2020"
output:
bookdown::html_document2
---
```{r setup2}
#https://stackoverflow.com/a/26743812/4927395
library(knitr)
knit_hooks$set(plot = function(x, options) {
paste('<figure><figcaption>', options$fig.cap, '</figcaption><img src="',
opts_knit$get('base.url'), paste(x, collapse = '.'),
'"></figure>',
sep = '')
}) #comment out to restore numbering
library(ggplot2)
```
```{r, echo = TRUE, fig.cap="Hello Dolly"}
ggplot(diamonds,aes(price,carat)) + geom_point()
```
解决方案可以在顶部同时包含编号和标题吗?
N.B原始问题的作者确实提到了 bookdown ,但没有提供完整的工作示例来证明这一点。很高兴编辑其他人认为更有用的原始问题。
编辑
Yihui在他的回答中显示fig.topcaption=TRUE
有一个简单的选择-谢谢!不幸的是,尽管该标题保留了正确的编号,但它仍然被推到了底部(对于可打印的数字)。下面的示例:
---
title: "Untitled"
author: "Internet"
date: "29 février 2020"
output:
bookdown::html_document2
---
```{r setup, message=FALSE, echo=FALSE}
library(knitr)
library(ggplot2)
library(plotly)
```
Here is a ggplot object with caption at the top as desired.
```{r, fig.cap="Hello ggplot", fig.topcaption=TRUE, message=FALSE, echo=FALSE}
ggplot(diamonds,aes(price,carat)) + geom_point()
```
Here is the previous ggplot converted to a plotly object with caption reverting to the bottom.
```{r, fig.cap="Hello plotly", fig.topcaption=TRUE, message=FALSE, echo=FALSE}
my_ggplot <- ggplot(diamonds,aes(price,carat)) + geom_point()
ggplotly(my_ggplot)
```
Caption reverts to bottom even if plotly object is not created from ggplot object
```{r, fig.cap="Hello plotly2", fig.topcaption=TRUE, message=FALSE, echo=FALSE}
plot_ly(
x=c(1,2,3),
y=c(5,6,7),
type='scatter',
mode='lines')
```
编辑2 使用CSS here
解决了字幕标题位置问题答案 3 :(得分:2)
将函数硬编码为函数从来都不是我的首选,但是它可以(为表创建另一个函数。)(此处的源代码:R Markdown HTML Number Figures)
现在,弄清楚如何引用这些...
---
title: "Untitled"
author: "Internet"
date: "29 fevrier 2020"
output:
bookdown::html_document2
---
```{r setup2}
#https://stackoverflow.com/a/26743812/4927395
library(knitr)
library(ggplot2)
capFigNo <- 1
capFig <- function(x){
x <- paste0("Figure ",capFigNo,": ",x)
capFigNo <<- capFigNo + 1
x
}
knit_hooks$set(plot = function(x, options) {
paste('<figure><figcaption>',
options$fig.cap,
'</figcaption><img src="',
opts_knit$get('base.url'),
paste(x, collapse = '.'),
'"></figure>',
sep = '')
}) #comment out to restore numbering
```
```{r echo = TRUE, fig.cap=capFig("Hello Dolly")}
#the trick is to wrap your caption in your number prefixing function.
ggplot(diamonds,aes(price,carat)) + geom_point()
```
```{r echo = TRUE, fig.cap=capFig("Hello Dolly2")}
ggplot(diamonds,aes(price,carat)) + geom_point()
```