如何用ggplot2控制图的尺寸/大小 [英] How to control the dimension / size of a plot with ggplot2

查看:10388
本文介绍了如何用ggplot2控制图的尺寸/大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用ggplot2(分别是qplot)来生成带有Sweave的报告。现在我需要一些帮助调整剧情的大小。

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 to include it.

\begin{figure}[htbp]
\begin{center}
<<fig=true,echo=false>>=
print(mygraph)
@
\caption{MyCaption}
\end{center}
\end{figure}

(如下图所示)绘图被压缩,但并未真正缩小。

If I add a width argument (like shown below) to plot is squeezed down, but not really scaled down.

<<fig=true,echo=false,width=3>>=

如果我使用ggsave(),我可以使用一个scale参数并影响生成的.pdf文件的大小。有没有办法影响剧情的维度而不保存(因为.pdf是由Sweave生成的)?有什么我需要添加到我的qplot代码?

If I use ggsave() instead, I could use a scale argument and influence the size of the resulting .pdf file. Is there a way to influence the dimensions of the plot without saving it (since the .pdf is generated by Sweave anyway) ? Is there anything I need to add to my qplot code?

mygraph=qplot(date,value,data=graph1,geom="line",colour=variable,xlab="",ylab="")
+ scale_y_continuous(limits = c(-0.3,0.3))

Thx提前给出任何建议!

Thx for any suggestions in advance!

推荐答案

ggplot2 内,在打印图形的代码块之前添加以下LaTeX代码。

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 y 是以英寸为单位的高度和宽度。

x and y are height and width in inches.

一个特定的纵横比你希望你的情节是,你可以用 opts() ggplot2 中设置。除非我有其他原因,否则我通常会按照Tufte的建议,尽量保持我的情节缩小到黄金比例。通常我有

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}

这篇关于如何用ggplot2控制图的尺寸/大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆