【发布时间】:2021-03-02 07:55:44
【问题描述】:
在 R Markdown 中,我生成了一个包含多个图的报告。我需要在每个图的标题中添加“图##”,以便这些数字是连续的。
目前我的方法是使用变量fig,并在每次绘图后加1。
library(ggplot2);
fig = 1;
ggplot(mtcars)+
geom_point(aes(x = cyl,
y = disp))+
labs(title = paste0("Fig. ", fig, ". Cyl vs Disp"));
fig = fig + 1;
## Some extra code or chunk break
ggplot(iris)+
geom_point(aes(x = Sepal.Length,
y = Petal.Length))+
labs(title = paste0("Fig. ", fig, ". Sepal vs Petal"));
fig = fig + 1;
我的问题是我经常忘记在情节后添加fig = fig + 1;。有什么优雅的方法可以避免这种重复输入吗?
更新:reprex 已更新以更清楚地显示问题。
【问题讨论】:
标签: r ggplot2 r-markdown