How to control the dimension / size of a plot with ggplot2

前端 未结 2 450
没有蜡笔的小新
没有蜡笔的小新 2021-01-02 13:18

I am using ggplot2 (respectively qplot) to generate a report with Sweave. Now I need some help with the adjustment of the size of the plot. I use the following Sweave code t

相关标签:
2条回答
  • 2021-01-02 13:35

    Instead of doing this within ggplot2, add the following LaTeX code before the code chunk where you print the graph.

    \SweaveOpts{width=x, height=y}
    

    x and y are height and width in inches.

    If there is a particular aspect ratio you would like your plot to be, you can set this in ggplot2 with opts(). Unless I have some other reason, I usually try to keep my plots scaled to the golden ratio, per Tufte's suggestions. Usually I have

    ...
    SweaveOpts{width=8, height=5}
    ...
    <<label = "makeplot", echo = F>>=
      p <- ggplot(mpg, aes(displ, hwy)) + 
        geom_point()+
        opts(aspect.ratio = 2/(1+sqrt(5)) )
    @
    ...
    \begin{figure}[htbp]
    \begin{center}
    <<fig=true,echo=false>>=
      print(p)
    @
    \caption{MyCaption}
    \end{center}
    \end{figure}
    
    0 讨论(0)
  • 2021-01-02 13:37

    The Sweave options width and height influence the dimensions of the PDF file but not the size of the figures in the document. Put something like

    \setkeys{Gin}{width=0.4\textwidth}
    

    after \begin{document} to get smaller plots.

    Source: Sweave manual, sec. 4.1.2

    0 讨论(0)
提交回复
热议问题