Search code examples
rggplot2sweave

Change the size of ggplot2 plot in Sweave without making the text/numbers disproportionately large


I found this question about changing the size of a ggplot2 plot in Sweave. I added the Sweaveopts{width=3, height=3} and it does shrink the size of the plot, but it doesn't scale down the text. So in the end, all of the numbers on the axes overlap.

Is there a way to scale the entire ggplot2 plot in Sweave so that I don't have to manually scale every component in the original ggplot2 call? It seems like something I should be able to do, but I can't find it in the ggplot2 book or on the website. Thanks!

FWIW, here's my call in Sweave:

\SweaveOpts{width=3, height=3}
\begin{figure}
\begin{center}
<<fig=TRUE>>=
print(plot.m)
@
\end{center}
\caption{stuff}
\label{fig:stuff}
\end{figure}

And the call that generates the ggplot2 plot:

plot.m <- ggplot(temp, aes(date, spread)) + geom_bar(stat="identity") + scale_x_date(major="years", minor="months")

Solution

  • It's basically a Sweave FAQ. Google and you will find a gazillion hits.

    One approach is to just write the file to pdf (no scaling) and to then scale on the \includegraphics command. I just looked at a vignette I finished a couple of days ago where I wanted something approximately as wide as the page and I did:

    \begin{figure}[t!]
      \centering
    <<someLabel,fig=TRUE,width=8>>=
    ## some R code omitted
    print(dotplot(foo ~ bar | someFactor, group=someThing, 
                  data=someDF, layout=c(1,3),
                  xlab="some X label", ylab="",
                  key=simpleKey(text=c("A","B"), space="top")))
    @
      \caption{Some caption.}
      \label{fig:someLabel}
    \end{figure}
    

    some basically just one width dimension at the Sweave options level. I found that small values do not work well -- so try something bigger as eg 6 or 7 inches.