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

白昼怎懂夜的黑 提交于 2019-12-19 04:32:09

问题


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

回答1:


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.



来源:https://stackoverflow.com/questions/4495528/change-the-size-of-ggplot2-plot-in-sweave-without-making-the-text-numbers-dispro

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!