plot.new() 中的错误:R 中的图边距太大 [英] Error in plot.new() : figure margins too large in R

查看:46
本文介绍了plot.new() 中的错误:R 中的图边距太大的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 R 的新手,但我已经用较小的数据集制作了许多相关图.但是,当我尝试绘制大型数据集 (2gb+) 时,我可以很好地生成绘图,但图例没有显示.有什么建议吗?或替代方案?

库(gplots)r.cor <- cor(r)布局(矩阵(c(1,1,1,1,1,1,1,1,2,2), 5, 2, byrow = TRUE))par(oma=c(5,7,1,1))cx <- rev(colorpanel(25,"黄色","黑色","蓝色"))腿 <- seq(min(r.cor,na.rm=T),max(r.cor,na.rm=T),length=10)image(r.cor,main="相关图正常/肿瘤数据",axes=F,col=cx)轴(1, at=seq(0,1,length=ncol(r.cor)), 标签=dimnames(r.cor)[[2]],cex.axis=0.9,las=2)轴(2,at=seq(0,1,length=ncol(r.cor)), 标签=dimnames(r.cor)[[2]],cex.axis=0.9,las=2)图像(as.matrix(leg),col=cx,axes=T)

<块引用>

plot.new() 中的错误:图形边距太大

tmp <- round(leg,2)轴(1,at=seq(0,1,length=length(leg)),labels=tmp,cex.axis=1)

解决方案

问题是由您的 layout() 调用创建的小图形区域 2 不够大,无法仅包含默认值边距,更不用说情节了.

更一般地,如果设备上绘图区域的大小不足以实际进行任何绘图,则会出现此错误.对于 OP 的情况,问题在于绘图设备太小,无法包含所有子图及其边距留出足够大的绘图区域来绘制.

如果绘图"选项卡太小而无法留出足够的空间来包含边距、绘图区域等,RStudio 用户可能会遇到此错误.这是因为该窗格的物理大小是图形设备的大小.这些不是独立的问题;RStudio 中的绘图窗格只是另一个绘图设备,例如 png()pdf()windows()X11().

解决方案包括:

  1. 减少边距的大小;这可能会有所帮助,尤其是当您尝试在同一设备上绘制多个图时(如 OP 的情况).

  2. 增加设备的物理尺寸,通过对设备的调用(例如 png()pdf() 等)或通过调整大小包含设备的窗口/窗格

  3. 减少图上文本的大小,因为这可以控制边距等的大小

缩小边距

在导致问题的行之前尝试:

par(mar = rep(2, 4))

然后绘制第二个图像

image(as.matrix(leg),col=cx,axes=T)

您需要在我展示的 par() 调用中调整边距的大小才能正确处理.

增加设备尺寸

您可能还需要增加要在其上绘图的实际设备的大小.

最后一个提示,在更改它们之前保存 par() 默认值,因此将现有的 par() 调用更改为:

op <- par(oma=c(5,7,1,1))

然后在绘图结束时做

par(op)

I'm new to R but I've made numerous correlation plots with smaller data sets. However, when I try to plot a large dataset (2gb+), I can produce the plot just fine, but the legend doesn't show up. Any advice? or alternatives?

library(gplots)
r.cor <- cor(r)
layout(matrix(c(1,1,1,1,1,1,1,1,2,2), 5, 2, byrow = TRUE))
par(oma=c(5,7,1,1))
cx <- rev(colorpanel(25,"yellow","black","blue"))
leg <- seq(min(r.cor,na.rm=T),max(r.cor,na.rm=T),length=10)
image(r.cor,main="Correlation plot Normal/Tumor data",axes=F,col=cx)
axis(1, at=seq(0,1,length=ncol(r.cor)), labels=dimnames(r.cor)[[2]], 
    cex.axis=0.9,las=2)
axis(2,at=seq(0,1,length=ncol(r.cor)), labels=dimnames(r.cor)[[2]],
     cex.axis=0.9,las=2)
image(as.matrix(leg),col=cx,axes=T)     

Error in plot.new() : figure margins too large

tmp <- round(leg,2)
axis(1,at=seq(0,1,length=length(leg)), labels=tmp,cex.axis=1)

解决方案

The problem is that the small figure region 2 created by your layout() call is not sufficiently large enough to contain just the default margins, let alone a plot.

More generally, you get this error if the size of the plotting region on the device is not large enough to actually do any plotting. For the OP's case the issue was having too small a plotting device to contain all the subplots and their margins and leave a large enough plotting region to draw in.

RStudio users can encounter this error if the Plot tab is too small to leave enough room to contain the margins, plotting region etc. This is because the physical size of that pane is the size of the graphics device. These are not independent issues; the plot pane in RStudio is just another plotting device, like png(), pdf(), windows(), and X11().

Solutions include:

  1. reducing the size of the margins; this might help especially if you are trying, as in the case of the OP, to draw several plots on the same device.

  2. increasing the physical dimensions of the device, either in the call to the device (e.g. png(), pdf(), etc) or by resizing the window / pane containing the device

  3. reducing the size of text on the plot as that can control the size of margins etc.

Reduce the size of the margins

Before the line causing the problem try:

par(mar = rep(2, 4))

then plot the second image

image(as.matrix(leg),col=cx,axes=T)

You'll need to play around with the size of the margins on the par() call I show to get this right.

Increase the size of the device

You may also need to increase the size of the actual device onto which you are plotting.

A final tip, save the par() defaults before changing them, so change your existing par() call to:

op <- par(oma=c(5,7,1,1))

then at the end of plotting do

par(op)

这篇关于plot.new() 中的错误:R 中的图边距太大的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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