14
 x<-seq(-3,3,by=0.1)
 y=0.5*x^3 
 ## Set png size to be slightly bigger than plot to take account of one line margin
 png( "~/myplot.png" , width = 600 , height = 2600 ,units="px",res=100)
 ## Set graphical parameters to control the size of the plot area and the margins
 ## Set size of plot area to 6 inches wide by 15 inches tall
 par( pin = c(6,26) )
 ## Remove margins around plot area
 par( mai = c(0,0,0,0) )
 ## Remove outer margins around plot area
 par( omi = c(0,0,0,0) )
 ## Set y-axis take use entire width of plot area (6 inches) and to have 7 tick marks (-3,-2,-1,0,1,2,3)
 par( xaxp = c(-3,3,7) )
 ## Set x-axis take use entire height of plot area (26 inches) and to have 27 tick marks (-13,-12,...,0,...11,12,13)
 par( yaxp = c(-13,13,27) )
 ## Create the plot
 plot( x , y , type = "l" , frame.plot = FALSE , axes = FALSE )
 axis( 1 , pos = 0 , at = seq( -3 , 3 , by = 1 ) , labels = seq( -3 , 3 , by = 1 ) )
 axis( 2 , pos = 0 , at = seq( -13 , 13 , by = 1 ) , labels = seq( -13 , 13 , by = 1 ) )
 text(0.5,5,expression(f(x)==frac(1,2)*x^3) )
 ## Turn PNG device off
 dev.off()

我们可以得到宽6英寸,高26英寸的图形,但是x轴或y轴的刻度单位不是1英寸;请看附件g1.png:

在此处输入图像描述

原因是 R 不会使用整个空间来设置 x 轴和 y 轴,R 保留的空间很小;请看附件g2.png:

在此处输入图像描述

我可以png( "~/myplot.png" , width = 610 , height = 2700 ,units="px",res=100),但是如何使刻度单位的物理长度仅为 1 英寸?

4

1 回答 1

13

要指定所需的确切设置,最好输出为 PDF。屏幕上的绘图在调整大小等时可能会发生变化。您需要设置各种图形参数来处理边距。图形中有一个很好的绘图区域轮廓,并在此处调整base适当的名称。此代码应该为您提供您所要求的内容:par

编辑

无论如何,PDF设备看起来都在外面保持一条线的边框。默认行高 ~ 0.2 英寸。因此,我们将宽度和高度增加 0.5 英寸,得到的间距正好为1 英寸。

这是我电脑上的测量值!! 在此处输入图像描述

## Set pdf size to be slightly bigger than plot to take account of one line margin
pdf( "~/myplot.pdf" , width = 6.5 , height = 15.5 )

## Set graphical parameters to control the size of the plot area and the margins
## Set size of plot area to 6 inches wide by 15 inches tall
par( pin = c(6,15) )

## Remove margins around plot area
par( mai = c(0,0,0,0) )

## Remove outer margins around plot area
par( omi = c(0,0,0,0) )

## Set y-axis take use entire width of plot area (6 inches) and to have 7 tick marks (-3,-2,-1,0,1,2,3)
par( yaxp = c(0,1,7) )

## Set x-axis take use entire height of plot area (15 inches) and to have 27 tick marks (-13,-12,...,0,...11,12,13)
par( xaxp = c(0,1,27) )

## Create the plot
plot( x , y , type = "l" , frame.plot = FALSE , axes = FALSE )
axis( 1 , pos = 0 , at = seq( -3 , 3 , by = 1 ) , labels = seq( -3 , 3 , by = 1 ) )
axis( 2 , pos = 0 , at = seq( -13 , 13 , by = 1 ) , labels = seq( -13 , 13 , by = 1 ) )
text(0.5,5,expression(f(x)==frac(1,2)*x^3) )

## Turn PDF device off
dev.off()

结果:

在此处输入图像描述

于 2013-04-30T12:17:08.307 回答