I have several images in a Sweave report that look like this:

enter image description here

They are created by a function with a code akin to this:

<<fig = T>>=
barplot(prop.table(table(x, y), margin = 2) * 100,
        border = F, xlab = '', ylab = '', las = 2)
@

I want to compress the y-axis, so my plot looks something like this (without the label distortion):

enter image description here

The only way I was able to get this was by using png() with a custom height parameter and then using the image file on LaTeX, but that kind of ruins the whole purpose of Sweave. How can I achieve this within barplot() (or with par() or some other elegant solution)?

有帮助吗?

解决方案

Sweave has chunk options width and height for the dimensions of the images. It won't control how big the plot is in the final rendered PDF as that is something LaTeX controls, but it does control the creation of the figures themselves.

From the Sweave Manual [pdf]:

Attention: One thing that gets easily confused are the width/height parameters of the R graphics devices and the corresponding arguments to the LaTeX \includegraphics command. The Sweave options width and height are passed to the R graphics devices, and hence affect the default size of the produced EPS and PDF files. They do not affect the size of figures in the document, by default they will always be 80% of the current text width. Use \setkeys{Gin} to modify figure sizes or use explicit \includegraphics commands in combination with Sweave option include=FALSE.

Also read

require("utils")
?RweaveLatex

which also has details.

For your quoted example, width and height are both 6, the default. So you could do something like

<<fig=TRUE, width=8, height=5>>=
barplot(prop.table(table(x, y), margin = 2) * 100,
        border = FALSE, xlab = '', ylab = '', las = 2)
@

To get the desired proportions.

[Please don't use F and T - you are asking for trouble!]

However, do note what the Manual or ?RweaveLatex say. Once in LaTeX by default the image will be included with a width equal to 0.8\textwidth. Hence you might also wish to set the width for each chunk explicitly to the size of the figure created, e.g.

\setkeys{Gin}{width=8in}
<<fig=TRUE, width=8, height=5>>=
barplot(prop.table(table(x, y), margin = 2) * 100,
        border = FALSE, xlab = '', ylab = '', las = 2)
@
%% reset if you want
\setkeys{Gin}{width=0.8\textwidth}

So you have to manage the two settings:

  1. The sweave chunk options width and height control creation of the EPS or PDF file (or both)
  2. \setkeys{Gin} controls the width of the included figure when processed using LaTeX.
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top