R Markdown引用图片报错[WARNING] Citeproc: citation Picture not found求助
R Markdown图片交叉引用失败的解决办法
1. 修正代码块格式
你的代码块开头语法错误,R Markdown代码块必须用三个反引号包裹,同时指定块类型和参数。另外注意:
- 块名前要加
r标识 - 确保加载
patchwork包(你用到了plot_layout函数) - 移除
geom_bar里多余的stat="identity"参数(默认stat="count"更适合你的需求)
修正后的代码块示例:
{r Picture, fig.asp=0.5, out.width= "100%", warning=FALSE, message=FALSE, fig.cap= "Student's grade count vs planning"} library(ggplot2) library(patchwork) # 第一个柱状图 ggplot(qn1frame, aes(x=hourly_plan, fill=category)) + geom_bar() + theme_bw(base_size = 12) + scale_fill_brewer(palette = "Set1") + labs(fill = "Grades", y = "Proportion", x = "ewuhrwe") # 第二个占比柱状图 ggplot(qn1frame, aes(x=hourly_plan, fill=category)) + geom_bar(position= "fill") + theme_bw(base_size = 12) + scale_fill_brewer(palette = "Set1") + labs(fill = "Grades", y = "Proportion", x = "Occupational exposure (yrs)") # 合并两张图并统一图例 (last_plot() + ggplot(qn1frame, aes(x=hourly_plan, fill=category)) + ...) + plot_layout(guides = 'collect') + plot_annotation(tag_levels = 'A')
2. 配置YAML头部启用交叉引用
交叉引用需要依赖特定的输出格式配置,根据你的输出类型选择:
PDF输出(原生支持交叉引用)
--- title: "你的文档标题" output: pdf_document: fig_caption: true number_sections: true link-citations: true ---
HTML/Word输出(需用bookdown扩展)
先安装bookdown包(install.packages("bookdown")),再修改YAML:
--- title: "你的文档标题" output: bookdown::html_document2: default # 或bookdown::word_document2 link-citations: true ---
3. 使用正确的引用语法
引用图片时必须加上fig:前缀,语法分两种:
- 原生PDF格式:
\@ref(fig:Picture) - bookdown格式:
@fig:Picture
示例:
Testing captions: Referring to Figure \@ref(fig:Picture).
4. 额外排查步骤
- 清理knit缓存:点击RStudio中「Knit」旁的下拉菜单,选择「Clean Knit」,避免缓存残留问题
- 检查
fig.cap参数:确保没有多余空格,且是合法字符串 - 确认包版本:更新
rmarkdown和bookdown到最新版本
内容的提问来源于stack exchange,提问作者user341191
相关产品推荐
相关产品推荐

