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>>=
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 for any suggestions in advance!
To increase the width of axes (both X-axis and Y-axis at the same time) using ggplot2 in R, we can use theme function with axis. line argument where we can set element_line argument to a larger value.
Scales in ggplot2 control the mapping from data to aesthetics. They take your data and turn it into something that you can see, like size, colour, position or shape. They also provide the tools that let you interpret the plot: the axes and legends.
The function geom_point() adds a layer of points to your plot, which creates a scatterplot. ggplot2 comes with many geom functions that each add a different type of layer to a plot.
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}
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With