如何处理R中的很多图 [英] How to deal with a lot of plots in R

查看:75
本文介绍了如何处理R中的很多图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个for循环,可产生60个图。我想将所有这些图仅保存在一个文件中。
如果我设置 par(mfrow = c(10,6))它说: plot.new()中的错误:图形边距太大

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

我该怎么办?

我的代码如下:

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()


推荐答案

默认图,如循环,不能非常有效地利用空间。如果仅查看单个图,则可以看到它在轴和边缘之间以及图区域和轴文本之间都具有较大的边距。实际上,有很多占用空间的地方。

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.

第二,默认的pdf函数会创建7 x 7英寸的小页面。

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

因此,尝试在7 x 7英寸上绘制10 x 6或12 x 5的图形正试图挤压很多

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.

要使其成功,您必须查看 par 的margin-options。是 mar mai oma omi ,也许还有更多。使用命令查询文档

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

此外,您可以考虑不显示60个子图中的每一个的轴文本,刻度线,刻度线标签和标题,因为这也可以节省您的时间

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.

但是有人已经为您解决了一些麻烦。查看 格子 -包或 ggplot2

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.

但是还有另一个紧迫的问题:您要显示60个子绘图吗?

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

更新

看到您要做什么,这是 ggplot2 中的一个小例子。它使用jrnold的 ggthemes 中的Tufte-theme,将其复制到此处,然后在该行中稍作修改

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.

使用 ggplot2 正在将所有数据重组为data.frames。对于此任务, reshape2 plyr -packages可能很好用
对于您来说,我想您创建的函数子图既计算估计值,又创建图。这意味着您必须将函数拆分为计算估算值,然后将其返回到 data.frame ,然后可以整理并传递给 ggplot

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.

这篇关于如何处理R中的很多图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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