R图中的填充控制 背景
我们(我的同事和我)正在使用一个简单的二维数据集来说明图形世界中当前事态的多样性。其思想是使用各种编程语言和绘图应用程序实现一致的结果 结果是而不是要求相同的。本上下文中的术语“一致性”与尺寸(10x10cm)和字体以及颜色和线型等物理尺寸有关 下面是两个例子:R图中的填充控制 背景,r,plot,R,Plot,我们(我的同事和我)正在使用一个简单的二维数据集来说明图形世界中当前事态的多样性。其思想是使用各种编程语言和绘图应用程序实现一致的结果 结果是而不是要求相同的。本上下文中的术语“一致性”与尺寸(10x10cm)和字体以及颜色和线型等物理尺寸有关 下面是两个例子: GNU图 R 问题: 与GNU版本相比,R绘图中有大量的填充(单击图像查看框),尤其是在图的顶部,我如何控制它 绘图命令中的设置是否为全局设置?具体来说,当我在绘图命令中设置pt=11(字体大小11pt)时,这是否适用于绘图
- GNU图
- R
绘图
命令中的设置是否为全局设置?具体来说,当我在绘图命令中设置pt=11
(字体大小11pt)时,这是否适用于绘图中的所有标签?我问的原因是,与GNU绘图相比,轴标签看起来肯定不是11磅
ggplot
等的花哨答案被视为作弊。我想让R自己处理这件事
例子
下面的示例是生成上述R图的文件。注意,输出是postscript,问题中显示的版本是“位图”版本
##----------------------------------------------------------------------------
##用途:将逗号分隔的文本文件导入R以进行打印
##x,y1,y2。。。。
##作者:。。。
##联系人:。。。(THW)
##组织:。。。
##许可证:。。。
##自:2016-12-31
##注:两小时工作
##用法:$R---slave--file=myfig.R
##:$ps2pdf-dEPSCrop myfig.ps
## ----------------------------------------------------------------------------
#读取数据
#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.8129694.01910467.36111302.39612204.04613177.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.4429082.0631402.0833907.1936612.1439532.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.0648470.0952336.8056511.9861020.2365888.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))
#设置打印环境
setEPS()#以某种方式修复了“.eps”输出
后记(“myfig.ps”,宽度=100.0/25.4,高度=100.0/25.4)
#精心设置新地块
xu range@G5W:你让我走上了正确的轨道,所以谢谢你!提供了控制R图边距的基础知识的详细概述
当我把我的原图打印在页边空白处的“线条”时,我得到以下结果:
表示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()
请注意,作为一种包驱动语言,R中的绝大多数绘图都是通过plot__ly
和ggplot2
等包完成的。排除它们可能会导致您的团队得出有偏见的结果。关于ps
另请注意,文档中说“点大小”的含义是特定于设备的”,您可以通过查看帮助页面?par
了解打印参数。对于这个问题,有两个参数需要查看mai和mar。
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()
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()