Reputation: 1456
We (my colleagues and I) are using a simple 2-D dataset in order to illustrate the diversity in the current state of affairs in the world of graphing. The idea is to achieve consistent results using a variety of programming languages and plotting applications.
The result is not required to be identical. Term "consistent" in this context is related to physical sizes such as dimension (10x10cm) and font as well as colours and line-types.
Now follows two examples:
plot
command global? Specifically, when I set pt=11
(font size 11pt) in the plot
command does that apply to all labels in the plot? The reason I ask is that it sure does not look like the axis labels are 11pt compared with the GNU plot...Note: Fancy answers based on ggplot
and the like is considered cheating. I want R to handle this natively.
The example below is the file which produced the R plot above. Note that the output is postscript, the version displayed in the question is a "bitmapped" version.
## ----------------------------------------------------------------------------
## 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()
Upvotes: 1
Views: 2928
Reputation: 1456
@G5W: You put me on the right track here, so thank you for that! This blog provides a decent overview of the basics in controlling the margins of an R-plot.
When I take my original plot and print the "lines" in the margin I get the following result:
which represents the default settings from R. The code stub producing the line numbers is marked with ## -----------------
.
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()
The conclusion is that we have too many lines in the margin and need to adjust the function par
to adjust the parameter mar
, i.e. the number of lines in the margin, to remove the padding (This was suggested in the comment to my original question). The result when doing this is shown below:
The code stub is well marked at the end of ## ------ mar
in the example at the end.
However, I am still not pleased and want to move the labels, which are now on line 3 to line 2. This is done using the function mtext
in the script at the end. The result is shown below:
Finally, in order to make fine adjustments to the width of the margin I use the par
function with the variable "mai". The final result is:
The code stub contains the three changes producing the final plot, they are all marked using the pattern: ## ----------------------
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()
Upvotes: 1