3

I have a for loop which produces 60 plots. I would like to save all this plots in only one file. If I set par(mfrow=c(10,6)) it says : Error in plot.new() : figure margins too large

What can I do?

My code is as follows:

pdf(file="figure.pdf")
par(mfrow=c(10,6))
for(i in 1:60){
  x=rnorm(100)
  y=rnorm(100)
  plot(x,y)
}
dev.off()
4

5 に答える 5

4

Output the plots to a pdf:

X = matrix(rnorm(60*100), ncol=60)
Y = matrix(rnorm(60*100), ncol=60)

pdf(file="fileName.pdf")
  for(j in 1:60){
    plot(X[,j], Y[,j])
  }
dev.off()
于 2013-05-22T17:16:22.280 に答える
4

Your default plot, as stated in the loop, does not use the space very effectively. If you look at just a single plot, you can see it has large margins, both between axis and edge and plot area and axis text. Effectively, there is a lot of space-hogging.

Secondly, the default pdf-function creates small pages, 7 by 7 inches. That is not a large sheet to plot on.

Trying to plot a 10 x 6 or 12 x 5 on 7 by 7 inches is therefore trying to squeeze in a lot of whitespace on very little space.

For it to succeed, you must look into the margin-options of par which is mar, mai, oma and omi, and probably some more. Consult the documentation with the command

?par

In addition to this, you could consider not displaying axis-text, tick-marks, tick-labels and titles for every one of the 60 sub-plots, as this too will save you space.

But somebody has already gone through some of this trouble for you. Look into the lattice-package or ggplot2, which has some excellent methods for making table-like subplots.

But there is another pressing issue: What are you trying to display with 60 subplots?


Update

Seeing what you are trying to do, here is a small example of faceting in ggplot2. It uses the Tufte-theme from jrnold's ggthemes, which is copied into here and then modified slightly in the line after the function.

library(ggplot2)
library(scales)

#### Setup the `theme` for the plot, i.e. the appearance of background, lines, margins, etc. of the plot.
##   This function returns a theme-object, which ggplot2 uses to control the appearance.
theme_tufte <- function(ticks=TRUE, base_family="serif", base_size=11) {
  ret <- theme_bw(base_family=base_family, base_size=base_size) +
    theme(
      legend.background = element_blank(),
      legend.key        = element_blank(),
      panel.background  = element_blank(),
      panel.border      = element_blank(),
      strip.background  = element_blank(),
      plot.background   = element_blank(),
      axis.line         = element_blank(),
      panel.grid = element_blank())
  if (!ticks) {
    ret <- ret + theme(axis.ticks = element_blank())
  }
  ret
}

## Here I modify the theme returned from the function,
theme <- theme_tufte() + theme(panel.margin=unit(c(0,0,0,0), 'lines'),     panel.border=element_rect(colour='grey', fill=NA))
## and instruct ggplot2 to use this theme as default.
theme_set(theme)

#### Some data generation.
size = 60*30
data <- data.frame(x=runif(size), y=rexp(size)+rnorm(size), mdl=sample(60,size, replace=TRUE))

#### Main plotting routine.
ggplot(data, aes(x,y, group=mdl)) ## base state of the plot to be used on all "layers", i.e. which data to use and which mappings to use (x should use x-variable, y should use the y-variable
  + geom_point()                  ## a layer that renders data as points, creates the scatterplot
  + stat_quantile(formula=y~x)    ## another layer that adds some statistics, in this case the 25%, 50% and 75% quantile lines.
  + facet_wrap(~ mdl, ncol=6)     ## Without this, all the groups would be displayed in one large plot; this breaks it up according to the `mdl`-variable.

Faceting with <code>ggplot2</code>.

The usual challenge in using ggplot2 is restructuring all your data into data.frames. For this task, the reshape2 and plyr-packages might be of good use. For you, I would imagine that your function that creates the subplot both calculates the estimation and creates the plot. This means that you have to split the function into calculating the estimation, returning it to a data.frame, which you then can collate and pass to ggplot.

于 2013-05-22T17:58:32.390 に答える
0

を使用することもできますknitr。これはすぐに基本グラフィックスに変換されませんでした (そして、今すぐ実行する必要があります) が、使用ggplotは簡単です。

\documentclass{article}

\begin{document}

<<echo = FALSE, fig.keep='high', fig.height=3, fig.width=4>>=
require(ggplot2)
for (i in 1:10) print(ggplot(mtcars, aes(x = disp, y = mpg)) + geom_point())
@


\end{document}

上記のコードは、すべてのグラフを含む素敵な複数ページの PDF を生成します。

于 2013-05-22T23:37:23.853 に答える