控制R图中的填充

时间:2017-01-03 15:13:14

标签: r plot

背景

我们(我的同事和我)正在使用一个简单的二维数据集来说明图形世界中当前事态的多样性。我们的想法是使用各种编程语言和绘图应用程序来实现一致的结果。

结果要求相同。在此上下文中,术语“一致”与物理尺寸相关,例如尺寸(10x10cm)和字体以及颜色和线型。

现在有两个例子:

  • GNU情节

enter image description here

  • - [R

enter image description here

问题

  1. 与GNU版本相比,R图中有很多填充(单击图像查看框),特别是在图的顶部,我该如何控制它? / LI>
  2. plot命令中的设置是全局的吗?具体来说,当我在pt=11命令中设置plot(字体大小为11pt)时,是否适用于图中的所有标签?我问的原因是,与GNU情节相比,轴标签看起来肯定不是11pt ......
  3. 注意:基于ggplot之类的花哨答案被视为作弊。我希望R本地处理这个问题。

    实施例

    以下示例是生成上述R图的文件。请注意,输出是postscript,问题中显示的版本是“位图”版本。

    ## ----------------------------------------------------------------------------
    ## Purpose     : Import comma separated text file to R for plotting
    ##               x, y1, y2, ....
    ## Author      : ...
    ## Contact     : ... (THW)
    ## Organization: ...
    ## License     : ...
    ## Since       : 2016-12-31
    ## Note        : Two hours of work
    ## Usage:        $ R ---slave --file=myfig.r
    ##             : $ ps2pdf -dEPSCrop myfig.ps
    ## ----------------------------------------------------------------------------
    
    # Read data
    #data = read.csv("../TXT/table.txt", header=FALSE, comment.char="%")
    
    data = data.frame(
      V     = c(0.9261187,0.8576959,0.7943282,0.7356423,0.6812921,0.6309573,
                0.5843414,0.5411695,0.5011872,0.4641589,0.4298662,0.3981072,
                0.3686945,0.3414549,0.3162278,0.2928645,0.2712273,0.2511886,
                0.2326305,0.2154435,0.1995262,0.1847850,0.1711328,0.1584893,
                0.1467799,0.1359356,0.1258925,0.1165914,0.1079775,0.1000000),
      P_T100= c(8977.812 ,9694.019 ,10467.361,11302.396,12204.046,13177.626,
                14228.873,15363.984,16589.648,17913.090,19342.110,20885.130,
                22551.245,24350.274,26292.821,28390.335,30655.178,33100.700,
                35741.314,38592.583,41671.313,44995.649,48585.184,52461.075,
                56646.165,61165.122,66044.580,71313.297,77002.327,83145.200),
      P_T300= c(26933.44 ,29082.06 ,31402.08 ,33907.19 ,36612.14 ,39532.88,
                42686.62 ,46091.95 ,49768.95 ,53739.27 ,58026.33 ,62655.39,
                67653.73 ,73050.82 ,78878.46 ,85171.00 ,91965.54 ,99302.10,
                107223.94,115777.75,125013.94,134986.95,145755.55,157383.22,
                169938.50,183495.37,198133.74,213939.89,231006.98,249435.60),
      P_T500= c(44889.06 ,48470.09 ,52336.80, 56511.98, 61020.23, 65888.13,
                71144.37 ,76819.92 ,82948.24, 89565.45, 96710.55,104425.65,
                112756.22,121751.37,131464.10,141951.67,153275.89,165503.50,
                178706.57,192962.92,208356.56,224978.24,242925.92,262305.37,
                283230.83,305825.61,330222.90,356566.49,385011.63,415726.00))
    
    
    # Set plotting environment
    setEPS()                                      # Somehow fixes the '.eps' output
    postscript("myfig.ps", width = 100.0/25.4, height = 100.0/25.4)
    
    # Meticulously set up a new plot
    x_range <- c(0, 1)
    y_range <- c(0, 500000) 
    
    
    # Generate a blank plot canvas
    plot( range(x_range), range(y_range), type="n", axes=FALSE, frame=FALSE, ps=11,
          xlim=x_range, ylim=y_range, xlab="Volume [m^3]", ylab="Pressure [Pa]")
    
    # Axes
    axis(1, at=c(0.0, 0.2, 0.4, 0.6, 0.8, 1.0) )
    axis(2, at=c(0,1,2,3,4,5)*100000 , las=1, labels=c(0,1,2,3,4,5))
    text(0.05 ,500000, expression("x10"^5), cex = 0.8)
    
    # Turn on grid
    grid(nx=5, ny=5, col = "lightgray", lty = "dotted")
    
    # Plot
    colors = c("blue", "red", "green")
    for (i in 1:length(colors) ) {
      lines(data[,1], data[,i+1], type="o", col=colors[i], pch=0, lty=1, cex=.6)
    }
    
    labels = c("T=100K", "T=300K", "T=500K")
    legend(0.4, 400000, labels, bty = "n", pch=c(0,0,0),
           lty=c(1,1,1), lwd=1, col=colors)
    
    dev.off()
    

1 个答案:

答案 0 :(得分:0)

@ G5W:你让我走在正确的轨道上,所以谢谢你! This blog提供了控制R图边缘的基础知识。

当我拍摄原始图并在边距中打印“线条”时,我得到以下结果:

enter image description here

表示R的默认设置。生成行号的代码存根标有## -----------------

data = data.frame(
  V     = c(0.9261187,0.8576959,0.7943282,0.7356423,0.6812921,0.6309573,
            0.5843414,0.5411695,0.5011872,0.4641589,0.4298662,0.3981072,
            0.3686945,0.3414549,0.3162278,0.2928645,0.2712273,0.2511886,
            0.2326305,0.2154435,0.1995262,0.1847850,0.1711328,0.1584893,
            0.1467799,0.1359356,0.1258925,0.1165914,0.1079775,0.1000000),
  P_T100= c(8977.812 ,9694.019 ,10467.361,11302.396,12204.046,13177.626,
            14228.873,15363.984,16589.648,17913.090,19342.110,20885.130,
            22551.245,24350.274,26292.821,28390.335,30655.178,33100.700,
            35741.314,38592.583,41671.313,44995.649,48585.184,52461.075,
            56646.165,61165.122,66044.580,71313.297,77002.327,83145.200),
  P_T300= c(26933.44 ,29082.06 ,31402.08 ,33907.19 ,36612.14 ,39532.88,
            42686.62 ,46091.95 ,49768.95 ,53739.27 ,58026.33 ,62655.39,
            67653.73 ,73050.82 ,78878.46 ,85171.00 ,91965.54 ,99302.10,
            107223.94,115777.75,125013.94,134986.95,145755.55,157383.22,
            169938.50,183495.37,198133.74,213939.89,231006.98,249435.60),
  P_T500= c(44889.06 ,48470.09 ,52336.80, 56511.98, 61020.23, 65888.13,
            71144.37 ,76819.92 ,82948.24, 89565.45, 96710.55,104425.65,
            112756.22,121751.37,131464.10,141951.67,153275.89,165503.50,
            178706.57,192962.92,208356.56,224978.24,242925.92,262305.37,
            283230.83,305825.61,330222.90,356566.49,385011.63,415726.00))


#  ================== P L O T -- P A R A M E T E R S ======================== %

xr = (0.35+1.0)/(0.35+1.0+0.1);
xl =  0.35     /(0.35+1.0+0.1);
yb =  0.80     /(0.80+5.0+0.4);
yt = (0.80+5.0)/(0.80+5.0+0.4);


x_range <- c(0, 1)
y_range <- c(0, 500000) 

x_tics <- c(0.0, 0.2, 0.4, 0.6, 0.8, 1.0)
y_tics <- c(0,1,2,3,4,5)


colors = c("blue", "green", "red")
labels = c("T=100K", "T=300K", "T=500K")

#  ============================ P L O T -- 1 ================================ %

# Set plotting environment
setEPS()                                      # Somehow fixes the '.eps' output
postscript("myfig.ps", width = 100.0/25.4, height = 100.0/25.4)

# Meticulously set up a new plot
x_range <- c(0, 1)
y_range <- c(0, 500000) 

# Generate a blank plot canvas
plot( range(x_range), range(y_range), type="n", axes=FALSE, frame=TRUE, ps=11,
      xlab="Volume [m^3]", ylab="Pressure [Pa]")

# Axes
axis(1, at=x_tics )
axis(2, at=y_tics*100000 , las=1, labels=y_tics)
text(0.05 ,500000, expression("x10"^5))

# Turn on grid
grid(nx=5, ny=5, col = "gray", lty = "dotted")

# Plot
for (i in 1:length(colors) ) {
  lines(data[,1], data[,i+1], type="o", col=colors[i], pch=0, lty=1, cex=.6)
}

legend(0.4, 400000, labels, bty = "n", pch=c(0,0,0),
       lty=c(1,1,1), lwd=1, col=colors)

## ----------------------------------------------------- Plot "lines" in margin 
for(j in 1:4) {
  for(i in 0:10) {
     mtext(as.character(i),side=j,line=i)
  }
}

dev.off()

结论是我们在边距中有太多行,需要调整函数par来调整参数mar,即边距中的行数,以删除填充(这是在我原来的问题的评论中提出的)。这样做的结果如下所示:

enter image description here

代码存根在最后的示例中的## ------ mar末尾标记得很好。

但是,我仍然不满意并希望将标签移动到第3行到第2行。这是使用最后脚本中的函数mtext完成的。结果如下所示:

enter image description here

最后,为了对边距的宽度进行微调,我使用par函数和变量“mai”。最终结果是:

enter image description here

代码存根包含产生最终图的三个更改,它们都使用模式标记:## ----------------------

data = data.frame(
  V     = c(0.9261187,0.8576959,0.7943282,0.7356423,0.6812921,0.6309573,
            0.5843414,0.5411695,0.5011872,0.4641589,0.4298662,0.3981072,
            0.3686945,0.3414549,0.3162278,0.2928645,0.2712273,0.2511886,
            0.2326305,0.2154435,0.1995262,0.1847850,0.1711328,0.1584893,
            0.1467799,0.1359356,0.1258925,0.1165914,0.1079775,0.1000000),
  P_T100= c(8977.812 ,9694.019 ,10467.361,11302.396,12204.046,13177.626,
            14228.873,15363.984,16589.648,17913.090,19342.110,20885.130,
            22551.245,24350.274,26292.821,28390.335,30655.178,33100.700,
            35741.314,38592.583,41671.313,44995.649,48585.184,52461.075,
            56646.165,61165.122,66044.580,71313.297,77002.327,83145.200),
  P_T300= c(26933.44 ,29082.06 ,31402.08 ,33907.19 ,36612.14 ,39532.88,
            42686.62 ,46091.95 ,49768.95 ,53739.27 ,58026.33 ,62655.39,
            67653.73 ,73050.82 ,78878.46 ,85171.00 ,91965.54 ,99302.10,
            107223.94,115777.75,125013.94,134986.95,145755.55,157383.22,
            169938.50,183495.37,198133.74,213939.89,231006.98,249435.60),
  P_T500= c(44889.06 ,48470.09 ,52336.80, 56511.98, 61020.23, 65888.13,
            71144.37 ,76819.92 ,82948.24, 89565.45, 96710.55,104425.65,
            112756.22,121751.37,131464.10,141951.67,153275.89,165503.50,
            178706.57,192962.92,208356.56,224978.24,242925.92,262305.37,
            283230.83,305825.61,330222.90,356566.49,385011.63,415726.00))


#  ================== P L O T -- P A R A M E T E R S ======================== %

xr = (0.35+1.0)/(0.35+1.0+0.1);
xl =  0.35     /(0.35+1.0+0.1);
yb =  0.80     /(0.80+5.0+0.4);
yt = (0.80+5.0)/(0.80+5.0+0.4);


x_range <- c(0, 1)
y_range <- c(0, 500000) 

x_tics <- c(0.0, 0.2, 0.4, 0.6, 0.8, 1.0)
y_tics <- c(0,1,2,3,4,5)


colors = c("blue", "green", "red")
labels = c("T=100K", "T=300K", "T=500K")

#  ============================ P L O T -- 4 ================================ %
# Set plotting environment
setEPS()                                      # Somehow fixes the '.eps' output
postscript("answer_04.ps", width = 100.0/25.4, height = 100.0/25.4)

## --------------------------------------------------------------- Adjust 'mar' 
#     ‘mar’ A numerical vector of the form ‘c(bottom, left, top, right)’
#          which gives the number of lines of margin to be specified on
#          the four sides of the plot.  The default is ‘c(5, 4, 4, 2) +
#          0.1’.
par(mar=c(3, 3, 1, 0))#
## --------------------------------------------------------------- Adjust 'mai' 
#     ‘mai’ A numerical vector of the form ‘c(bottom, left, top, right)’
#          which gives the margin size specified in inches.
par(mai = c(0.6,0.6,0.22,0.01))

# Generate empty canvas
plot.new()
plot.window(x_range, y_range)

# Axes
axis(side=1, at=c(0.0, 0.2, 0.4, 0.6, 0.8, 1.0))
axis(side=2, at=c(0,1,2,3,4,5)*100000 , las=1, labels=c(0,1,2,3,4,5))
mtext(side=3, line=0, expression("x10"^5), adj=0)
## ------------------------------------------------------ Move labels to line 2
mtext(side=1, line=2, "Volume [m^3]")
mtext(side=2, line=2, "Pressure [Pa]")

# Turn on grid
grid(nx=5, ny=5, col = "gray", lty = "dotted")

# Plot
for (i in 1:length(colors) ) {
  lines(data[,1], data[,i+1], type="o", col=colors[i], pch=0, lty=1, cex=.6)
}

legend(0.4, 400000, labels, bty = "n", pch=c(0,0,0),
       lty=c(1,1,1), lwd=1, col=colors)

dev.off()