英文:
How to stop the same figure numbers appearing in my rmarkdown document
问题
I generated several figures using different kinds of commands such as tikz, rmarkdown chunk, ggplot2 like the following. Unfortunately, they all are generated with the same figure number as "Figure 1". Do you have any suggestion to resolve the issue?
Here in the following I generate a reproducible example. Please refer to the following codes for your reference.
---
title: "demo"
author:
output:
pdf_document:
number_sections: true
in_header: preamble.tex
keep_tex: yes
fontsize: 12pt
linestretch: 1.2
subparagraph: true
number_sections: true
geometry: margin=1in
header-includes:
- \usepackage{amsmath}
- \usepackage{amsthm}
- \usepackage{bbm}
- \numberwithin{equation}{section}
- \usepackage{makecell}
- \usepackage{indentfirst}
- \usepackage{geometry}
- \usepackage{graphicx}
- \usepackage{tikz}
- \AtEndPreamble{\usepackage{cleveref}}
---
```{r fig.align="center", fig.cap="\\label{fig:figure4}Trends", warning=FALSE, echo=FALSE, message=FALSE}
library(ggplot2)
options(scipen=999) # turn-off scientific notation like 1e+48
library(ggplot2)
theme_set(theme_bw()) # pre-set the bw theme.
data("midwest", package = "ggplot2")
gg <- ggplot(midwest, aes(x=area, y=poptotal)) +
geom_point(aes(col=state, size=popdensity)) +
geom_smooth(method="loess", se=FALSE) +
xlim(c(0, 0.1)) +
ylim(c(0, 500000)) +
labs(subtitle="Area Vs Population",
y="Population",
x="Area",
title="Scatterplot",
caption = "Source: midwest")
plot(gg)
```r
```{r fig.align="center", fig.cap="\\label{fig:figure1} figure 1 "}
library(datasets)
library(tseries)
data("AirPassengers")
passengers <- AirPassengers
plot(passengers, main = "Monthly Air Passenger Traffic")
```latex
\begin{figure}
\tikzset{every picture/.style={line width=0.75pt}} %set default line width to 0.75pt
\begin{tikzpicture}[x=0.75pt,y=0.75pt,yscale=-1,xscale=1]
\draw [fill={rgb, 255:red, 0; green, 0; blue, 0 } ,fill opacity=0.32 ] (66.5,28.97) -- (248.5,28.97) -- (248.5,137) -- (66.5,137) -- cycle ;
\draw [fill={rgb, 255:red, 0; green, 0; blue, 0 } ,fill opacity=0.25 ] (62.5,265.61) -- (250.25,265.61) -- (250.25,356.79) -- (62.5,356.79) -- cycle ;
\end{tikzpicture}
\caption{\footnotesize Flow Diagram 1}
\label{fig:figure2}
\end{figure}
\begin{figure}
\tikzset{every picture/.style={line width=0.75pt}}
\begin{tikzpicture[x=0.75pt,y=0.75pt,yscale=-1,xscale=1]
\draw [fill={rgb, 255:red, 0; green, 0; blue, 0 } ,fill opacity=0.32 ] (66.5,28.97) -- (248.5,28.97) -- (248.5,137) -- (66.5,137) -- cycle ;
\draw [fill={rgb, 255:red, 0; green, 0; blue, 0 } ,fill opacity=0.25 ] (62.5,265.61) -- (250.25,265.61) -- (250.25,356.79) -- (62.5,356.79) -- cycle ;
\end{tikzpicture}
\caption{\footnotesize Flow Diagram2}
\label{fig:figure3}
\end{figure}
Look at \cref{fig:figure1}, \cref{fig:figure2}, \cref{fig:figure3}, and \cref{fig:figure4}.
<details>
<summary>英文:</summary>
I generated several figures using different kinds of commands such as tikz, rmarkdown chunck, ggplot2 like the following. Unfortunately, they all are generated with the same figure number as "Figure 1". Do you have any suggestion to resolve the issue?
Here in the following I generate a reproducible example. Please refer to the following codes for your reference.
~~~
---
title: "demo"
author:
output:
pdf_document:
number_sections: true
in_header: preamble.tex
keep_tex: yes
fontsize: 12pt
linestretch: 1.2
subparagraph: true
number_sections: true
geometry: margin=1in
header-includes:
- \usepackage{amsmath}
- \usepackage{amsthm}
- \usepackage{bbm}
- \numberwithin{equation}{section}
- \usepackage{makecell}
- \usepackage{indentfirst}
- \usepackage{geometry}
- \usepackage{graphicx}
- \usepackage{tikz}
- \AtEndPreamble{\usepackage{cleveref}}
---
```{r fig.align="center", fig.cap="\\label{fig:figure4}Trends", warning=FALSE, echo=FALSE, message=FALSE}
library(ggplot2)
options(scipen=999) # turn-off scientific notation like 1e+48
library(ggplot2)
theme_set(theme_bw()) # pre-set the bw theme.
data("midwest", package = "ggplot2")
gg <- ggplot(midwest, aes(x=area, y=poptotal)) +
geom_point(aes(col=state, size=popdensity)) +
geom_smooth(method="loess", se=F) +
xlim(c(0, 0.1)) +
ylim(c(0, 500000)) +
labs(subtitle="Area Vs Population",
y="Population",
x="Area",
title="Scatterplot",
caption = "Source: midwest")
plot(gg)
library(datasets)
library(tseries)
data("AirPassengers")
passengers <- AirPassengers
plot(passengers, main = "Monthly Air Passenger Traffic")
\begin{figure}
\tikzset{every picture/.style={line width=0.75pt}} %set default line width to 0.75pt
\begin{tikzpicture}[x=0.75pt,y=0.75pt,yscale=-1,xscale=1]
\draw [fill={rgb, 255:red, 0; green, 0; blue, 0 } ,fill opacity=0.32 ] (66.5,28.97) -- (248.5,28.97) -- (248.5,137) -- (66.5,137) -- cycle ;
\draw [fill={rgb, 255:red, 0; green, 0; blue, 0 } ,fill opacity=0.25 ] (62.5,265.61) -- (250.25,265.61) -- (250.25,356.79) -- (62.5,356.79) -- cycle ;
\end{tikzpicture}
\caption{\footnotesize Flow Diagram 1}
\label{fig:figure2}
\end{figure}
\begin{figure}
\tikzset{every picture/.style={line width=0.75pt}}
\begin{tikzpicture}[x=0.75pt,y=0.75pt,yscale=-1,xscale=1]
\draw [fill={rgb, 255:red, 0; green, 0; blue, 0 } ,fill opacity=0.32 ] (66.5,28.97) -- (248.5,28.97) -- (248.5,137) -- (66.5,137) -- cycle ;
\draw [fill={rgb, 255:red, 0; green, 0; blue, 0 } ,fill opacity=0.25 ] (62.5,265.61) -- (250.25,265.61) -- (250.25,356.79) -- (62.5,356.79) -- cycle ;
\end{tikzpicture}
\caption{\footnotesize Flow Diagram2}
\label{fig:figure3}
\end{figure}
Look at \cref{fig:figure1}, \cref{fig:figure2}, \cref{fig:figure3} and \cref{fig:figure4}.
~~~
</details>
# 答案1
**得分**: 1
- 你缺少了`cleveref`包。
- `fig:figuare1`中有一个拼写错误。
- 根据https://stackoverflow.com/questions/33517026/error-trying-to-install-package-for-iris-dataset,没有`iris`库。
[![enter image description here][1]][1]
[1]: https://i.stack.imgur.com/6CO7X.png
<details>
<summary>英文:</summary>
- you are missing the `cleveref` package
- there is a typo in `fig:figuare1`
- and according to https://stackoverflow.com/questions/33517026/error-trying-to-install-package-for-iris-dataset , there is no `iris` library
----
---
title: "demo"
author:
output:
pdf_document:
number_sections: true
link-citations: yes
toc: false
biblio-style: apalike
in_header: preamble.tex
keep_tex: yes
fontsize: 12pt
linestretch: 1.2
subparagraph: true
number_sections: true
geometry: margin=1in
header-includes:
- \usepackage{amsmath}
- \usepackage{amsthm}
- \usepackage{bbm}
- \numberwithin{equation}{section}
- \usepackage{makecell}
- \usepackage{indentfirst}
- \usepackage{geometry}
- \usepackage{graphicx}
- \usepackage{tikz}
- \AtEndPreamble{\usepackage{cleveref}}
---
```{r fig.align="center", fig.cap="\\label{fig:figure1} figure 1 "}
library(datasets)
library(tseries)
data("AirPassengers")
passengers <- AirPassengers
plot(passengers, main = "Monthly Air Passenger Traffic")
```
\begin{figure}
\tikzset{every picture/.style={line width=0.75pt}} %set default line width to 0.75pt
\begin{tikzpicture}[x=0.75pt,y=0.75pt,yscale=-1,xscale=1]
\draw [fill={rgb, 255:red, 0; green, 0; blue, 0 } ,fill opacity=0.32 ] (66.5,28.97) -- (248.5,28.97) -- (248.5,137) -- (66.5,137) -- cycle ;
\draw [fill={rgb, 255:red, 0; green, 0; blue, 0 } ,fill opacity=0.25 ] (62.5,265.61) -- (250.25,265.61) -- (250.25,356.79) -- (62.5,356.79) -- cycle ;
\end{tikzpicture}
\caption{\footnotesize Flow Diagram 1}
\label{fig:figure2}
\end{figure}
\begin{figure}
\tikzset{every picture/.style={line width=0.75pt}}
\begin{tikzpicture}[x=0.75pt,y=0.75pt,yscale=-1,xscale=1]
\draw [fill={rgb, 255:red, 0; green, 0; blue, 0 } ,fill opacity=0.32 ] (66.5,28.97) -- (248.5,28.97) -- (248.5,137) -- (66.5,137) -- cycle ;
\draw [fill={rgb, 255:red, 0; green, 0; blue, 0 } ,fill opacity=0.25 ] (62.5,265.61) -- (250.25,265.61) -- (250.25,356.79) -- (62.5,356.79) -- cycle ;
\end{tikzpicture}
\caption{\footnotesize Flow Diagram2}
\label{fig:figure3}
\end{figure}
```{r fig.align="center", fig.cap="\\label{fig:figure4}Trends", warning=FALSE, echo=FALSE, message=FALSE}
library(ggplot2)
qplot(Sepal.Length, data = iris, size = Petal.Width)
```
Look at \cref{fig:figure1}, \cref{fig:figure2}, \cref{fig:figure3} and \cref{fig:figure4}.
[![enter image description here][1]][1]
[1]: https://i.stack.imgur.com/6CO7X.png
</details>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论