# LaTeX figure cross reference label using RStudio and knitr [duplicate]

When using RStudio to write a .Rnw file, I find that when inserting a plot, the cross reference is not expected. For example, in section 5.3 I insert a plot using the following commands:

\begin{figure}[!htb]
\begin{center}\label{fig:testplot}
\includegraphics[scale=0.8]{testplot.pdf}
\caption{I'm caption}
\end{center}
\end{figure}


In the text, I write "In Figure~\ref{fig:testplot}" to reference this plot. However, after clicking Compile PDF, I find that what produced is "In Figure 5.3" --> it is the section number, not the desired figure number (it should print out Figure 4 since three figures already existed in the text). What went wrong?

Thank you very much!

-

## migrated from stackoverflow.comOct 16 '12 at 6:26

This question came from our site for professional and enthusiast programmers.

## marked as duplicate by David Carlisle, Heiko Oberdiek, lockstep, percusse, clemensOct 16 '12 at 9:13

Try to put the \label after the \includegraphics. –  Dror Oct 16 '12 at 8:00