ggplot2r-markdownfiguretikz

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 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)
```
    




```{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}

```

Look at \cref{fig:figure1}, \cref{fig:figure2}, \cref{fig:figure3} and \cref{fig:figure4}.


Solution


  • ---
    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