| 23048 |
ripley |
1 |
#### Regression tests for GRAPHICS & PLOTS
|
|
|
2 |
|
| 57488 |
ripley |
3 |
pdf("reg-plot.pdf", paper="a4r", encoding ="ISOLatin1.enc", compress = FALSE)
|
|
|
4 |
|
| 23048 |
ripley |
5 |
## since we supply the font metrics, the results depend only on
|
| 56953 |
ripley |
6 |
## the encoding used: Windows is different from Unix by default.
|
| 23048 |
ripley |
7 |
|
| 57488 |
ripley |
8 |
options(warn = 1) # print as they occur
|
| 23048 |
ripley |
9 |
|
|
|
10 |
plot(0) # this should remain constant
|
|
|
11 |
str(par(c("usr","xaxp","yaxp")))
|
| 71645 |
maechler |
12 |
stopifnot(all.equal(
|
|
|
13 |
par(c("usr","xaxp","yaxp"))
|
|
|
14 |
,
|
|
|
15 |
list(usr = c(0.568, 1.432, -1.08, 1.08),
|
|
|
16 |
xaxp = c(0.6, 1.4, 4), yaxp = c(-1, 1, 4))))
|
| 23048 |
ripley |
17 |
|
|
|
18 |
|
|
|
19 |
### Test for centring of chars. All the chars which are plotted should
|
|
|
20 |
### be centred, and there should be no warnings about
|
|
|
21 |
### font metrics unknown for character `?'
|
|
|
22 |
|
|
|
23 |
par(pty="s")
|
|
|
24 |
plot(c(-1,16), c(-1,16), type="n", xlab="", ylab="", xaxs="i", yaxs="i")
|
|
|
25 |
title("Centred chars in default char set (ISO Latin1)")
|
|
|
26 |
grid(17, 17, lty=1)
|
| 32436 |
ripley |
27 |
known <- c(32:126, 160:255)
|
| 23048 |
ripley |
28 |
|
|
|
29 |
for(i in known) {
|
|
|
30 |
x <- i %% 16
|
|
|
31 |
y <- i %/% 16
|
| 44151 |
ripley |
32 |
points(x, y, pch=-i)
|
| 23048 |
ripley |
33 |
}
|
|
|
34 |
|
|
|
35 |
par(pty="m")
|
|
|
36 |
|
|
|
37 |
## PR 816 (label sizes in dotchart)
|
|
|
38 |
|
|
|
39 |
### Prior to 1.2.2, the label sizes were unaffected by cex.
|
|
|
40 |
|
|
|
41 |
dotchart(VADeaths, main = "Death Rates in Virginia - 1940", cex = 0.5)
|
|
|
42 |
dotchart(VADeaths, main = "Death Rates in Virginia - 1940", cex = 1.5)
|
|
|
43 |
|
|
|
44 |
## killed by 0 prior to 1.4.0 and in 1.4.1:
|
|
|
45 |
t1 <- ts(0:100)
|
|
|
46 |
## only warnings about values <= 0
|
|
|
47 |
plot(t1, log = "y")
|
|
|
48 |
plot(cbind(t1, 10*t1, t1 - 4), log="y", plot.type = "single")
|
|
|
49 |
stopifnot(par("usr")[4] > 3) # log10: ylim[2] = 1000
|
|
|
50 |
|
|
|
51 |
|
|
|
52 |
## This one needs to be looked at.
|
|
|
53 |
## lty = "blank" killed the fill colour too.
|
|
|
54 |
plot(1:10, type="n")
|
|
|
55 |
polygon(c(1, 3, 3, 1), c(1, 1, 3, 3), col="yellow", border="red", lty="blank")
|
|
|
56 |
rect(6, 6, 10, 10, col="blue", border="red", lty="blank")
|
|
|
57 |
## in 1.5.0 all omit the fill colours.
|
|
|
58 |
with(trees, symbols(Height, Volume, circles=Girth/24, inches=FALSE,
|
|
|
59 |
lty="blank", bg="blue"))
|
|
|
60 |
## in 1.5.0 ignored the lty.
|
|
|
61 |
|
|
|
62 |
## axis() and par(mgp < 0) {keep this example S+ compatible!}:
|
|
|
63 |
lt <- if(is.R()) "31" else 2
|
|
|
64 |
x <- seq(-2,3, len=1001)
|
|
|
65 |
op <- par(tck= +0.02, mgp = -c(3,2,0))
|
|
|
66 |
plot(x, x^2 - 1.2, xaxt = "n", xlab="", type ='l', col = 2,
|
|
|
67 |
main = "mgp < 0: all ticks and labels inside `frame'")
|
|
|
68 |
x <- -2:3
|
|
|
69 |
lines(x, x^2 - 1.2, type ="h", col = 3, lwd=3)
|
|
|
70 |
axis(1, pos = 0, at=-1:1, lty = lt, col=4)## col & lty work only from R 1.6
|
|
|
71 |
par(op)
|
|
|
72 |
axis(1, pos = 0, at=c(-2,2,3), lty = lt, col=4)
|
|
|
73 |
mtext(side=1,"note the x-ticks on the other side of the bars")
|
|
|
74 |
|
| 23846 |
maechler |
75 |
## plot.table(): explicit xlab and ylab for non-1D
|
| 23048 |
ripley |
76 |
plot(UCBAdmissions)# default x- and y-lab
|
|
|
77 |
plot(UCBAdmissions, xlab = "x label", ylab = "YY")# wrong in 1.5.1
|
| 23846 |
maechler |
78 |
## axis suppression
|
|
|
79 |
plot(tt <- table(c(rep(0,7), rep(1,4), rep(5, 3))), axes = FALSE)
|
|
|
80 |
plot(tt, xaxt = "n")
|
|
|
81 |
## wrong till (incl.) 1.6.x
|
| 25869 |
maechler |
82 |
|
|
|
83 |
## legend with call
|
|
|
84 |
lo <- legend(2,2, substitute(hat(theta) == that, list(that= pi)))
|
|
|
85 |
stopifnot(length(lo$text$x) == 1)
|
|
|
86 |
## length() was 3 till 1.7.x
|
| 26717 |
maechler |
87 |
|
|
|
88 |
plot(ecdf(c(1:4,8,12)), ylab = "ECDF", main=NULL)
|
|
|
89 |
## ylab didn't work till 1.8.0
|
| 29381 |
maechler |
90 |
|
|
|
91 |
plot(1:10, pch = NA) # gave error till 1.9.0
|
|
|
92 |
points(1:3, pch=c("o",NA,"x"))# used "N"
|
|
|
93 |
try(points(4, pch=c(NA,FALSE)))# still give an error
|
| 30819 |
maechler |
94 |
|
|
|
95 |
## 'lwd' should transfer to plot symbols
|
|
|
96 |
legend(1,10, c("A","bcd"), lwd = 2:3, pch= 21:22, pt.bg="skyblue",
|
|
|
97 |
col = 2:3, bg = "thistle")
|
|
|
98 |
## (gave an error for 2 days in "2.0.0 unstable")
|
| 33335 |
maechler |
99 |
|
|
|
100 |
x <- 2^seq(1,1001, length=20)
|
|
|
101 |
plot(x, x^0.9, type="l", log="xy")
|
|
|
102 |
## gave error 'Infinite axis extents [GEPretty(1.87013e-12,inf,5)]' for R 2.0.1
|
| 35127 |
maechler |
103 |
|
|
|
104 |
plot(as.Date("2001/1/1") + 12*(1:9), 1:9)
|
|
|
105 |
## used bad 'xlab/ylab' in some versions of R 2.2.0(unstable)
|
| 39725 |
maechler |
106 |
|
| 40032 |
maechler |
107 |
## dotchart() restoring par()
|
| 39725 |
maechler |
108 |
Opar <- par(no.readonly=TRUE) ; dotchart(1:4, cex= 0.7)
|
|
|
109 |
Npar <- par(no.readonly=TRUE)
|
| 40032 |
maechler |
110 |
ii <- c(37, 50:51, 58:59, 63)
|
|
|
111 |
stopifnot(identical(names(Opar)[ii],
|
|
|
112 |
c("mai","pin","plt","usr","xaxp","yaxp")),
|
|
|
113 |
identical(Opar[-ii], Npar[-ii]))
|
|
|
114 |
## did not correctly restore par("mar") up to (incl) R 2.4.0
|
|
|
115 |
|
| 43202 |
maechler |
116 |
## plot.function() [n=11, ... : since we store and diff PS file !]
|
|
|
117 |
plot(cos, xlim=c(-5,5), n=11, axes=FALSE); abline(v=0)
|
| 43181 |
maechler |
118 |
## did *not* plot for negative x up to R 2.5.1
|
| 43202 |
maechler |
119 |
plot(sin, -2,3, xlim=c(-5,5), n=11, axes=FALSE, xlab="")# plot from -2
|
|
|
120 |
axis(1, at=c(-2,3), tcl=-1); axis(1, at=c(-5,5))
|
|
|
121 |
## (from,to) & xlim should work simultaneously
|
|
|
122 |
|
| 43181 |
maechler |
123 |
plot(cos, -7,7, n=11, axes=FALSE)
|
|
|
124 |
## gave wrong ylab in R 2.6.0
|
|
|
125 |
plot(cos, -7,7, ylab = "Cosine cos(x)", n=11, axes=FALSE)
|
|
|
126 |
## partial matching of 'ylab'; mapping [0,1] (not [-7.7]):
|
| 43286 |
ripley |
127 |
## margins chosen to avoid rouding error showing to 2dp.
|
|
|
128 |
op <- par(mar=c(5,4.123,4,2)+0.1)
|
| 43181 |
maechler |
129 |
plot(gamma, yla = expression(Gamma(x)), n=11, yaxt="n")
|
| 43286 |
ripley |
130 |
par(op)
|
| 43202 |
maechler |
131 |
|
| 43526 |
maechler |
132 |
## plot.ts(x, y) could get the labels wrong in R <= 2.6.0:
|
|
|
133 |
x <- ts(1:5);x1 <- lag(x, 2); plot(x1, x, axes=FALSE)
|
| 43636 |
maechler |
134 |
|
|
|
135 |
# adding a curve in log scale :
|
|
|
136 |
curve(5*exp(-x), 0.1, 100, n = 3, log="x", ylab="", axes=FALSE)
|
|
|
137 |
curve(5*exp(-x), add=TRUE, n = 3, col=2,lwd=3)
|
|
|
138 |
## should fully overplot; wrong default xlim in 2.6.1
|
|
|
139 |
## (and *slightly* wrong up to 2.6.0)
|
| 45449 |
maechler |
140 |
|
|
|
141 |
## Axis() calls via plot() {[xy]axt to keep *.ps small}
|
|
|
142 |
x <- as.Date("2008-04-22 09:45") + (i <- c(0,4))
|
|
|
143 |
plot(x, xaxt="n")# not ok in 2.6.2, nor 2.7.0
|
|
|
144 |
plot(x, i, yaxt="n")# ok in 2.6.2 and 2.7.0
|
|
|
145 |
plot(i, x, xaxt="n")# ok in 2.6.2 and not in 2.7.0
|
| 54481 |
ripley |
146 |
|
|
|
147 |
## table methods should be bypassed:
|
|
|
148 |
dotchart(table(infert$education))
|
|
|
149 |
## failed in 2.12.[12]
|
| 62846 |
maechler |
150 |
|
|
|
151 |
## cex as "..." in "high level" function
|
|
|
152 |
hc <- hclust(dst <- dist(c(1:2, 5)), method="ave")
|
|
|
153 |
plot(hc, cex = 2, axes=FALSE, ann=FALSE)
|
|
|
154 |
## cex was not used in 3.0.[01]
|
|
|
155 |
|
| 71118 |
maechler |
156 |
## axis.Date() and axis.POSIXct() with reversed 'xlim'
|
| 71128 |
maechler |
157 |
toD <- as.Date("2016-08-19"); dates <- c(toD - 10, toD)
|
| 71118 |
maechler |
158 |
plot(dates, 1:2, xlim = rev(dates),
|
|
|
159 |
ann=FALSE, yaxt="n", frame.plot=FALSE)
|
|
|
160 |
## failed to label the dates in R <= 3.3.1
|
| 74863 |
maechler |
161 |
|
|
|
162 |
|
|
|
163 |
## axis() -- labels only written when there's room
|
|
|
164 |
plot2 <- function(at, wait=FALSE) {
|
|
|
165 |
plot1 <- function(x,y) plot(x,y, type="n", xlab="", ylab="", tck=0, frame.plot=FALSE)
|
|
|
166 |
plot1( at, at); axis(3, at= at, tck=0)
|
|
|
167 |
plot1(-at,-at); axis(3, at=-at, tck=0)
|
|
|
168 |
if(wait) { mtext("Click here to advance!", line=-2, cex=1.5, col=2); locator(1) }
|
|
|
169 |
}
|
|
|
170 |
at <- c(7:15, 2*(8:15), 5*(7:15), 10*(8:15))
|
|
|
171 |
op <- par(mfrow=2:1, mgp = c(1.5, 0.6, 0), mar = .1+c(2,2,2,1),
|
|
|
172 |
lab = c(20,20,7), las = 1) # las=1: all horizontal => y-axis perpendicular
|
|
|
173 |
interAct <- dev.interactive()
|
|
|
174 |
|
|
|
175 |
if(interAct) { xMar <- c(1,3)/2 ; nP <- 10
|
|
|
176 |
} else { xMar <- c(1,3)*3/2; nP <- 3 }
|
|
|
177 |
## Now increasing margins ==> decreasing plot area ==> shrinking plot
|
|
|
178 |
## (*is* device dependent [here have "a4r"-pdf]) :
|
|
|
179 |
for(n in 1:nP) {
|
|
|
180 |
par(mar = par("mar") + xMar)
|
|
|
181 |
plot2(at, n < nP && interAct)
|
|
|
182 |
}
|
| 74871 |
maechler |
183 |
par(op)
|
|
|
184 |
|
|
|
185 |
|
|
|
186 |
##----- pairs() using verInd & horInd ----------
|
|
|
187 |
mpairs <- function(..., p=3, npp=2, main) { # p=4, npp=7 was Chris Andrews' example
|
|
|
188 |
x <- matrix((1:(p*npp))/npp, ncol=p, dimnames=list(NULL, paste0("x", 1:p)))
|
|
|
189 |
## -> x \in [1/npp,.., p] <==> ceiling(x) \in {1,2,...,p}
|
|
|
190 |
## the panel function
|
|
|
191 |
mP <- function(x,y, ...) { ## really made for the 'x = ...' above
|
|
|
192 |
j <- ceiling(mean(y)) # in 1:p
|
|
|
193 |
points(x,y, col=ceiling(mean(x)), pch= 3*j, cex=((p-1.5)*j+1)/p, ...)
|
|
|
194 |
g <- 1:(p-1); abline(h=g, v=g, lty=3, col="gray33")
|
|
|
195 |
}
|
|
|
196 |
if(missing(main))
|
|
|
197 |
main <- if(...length()) sub("mpairs", '', deparse(sys.call()))
|
|
|
198 |
lim <- range(x)
|
|
|
199 |
k <- max(2, npp-2) ## bug in R(?): par(lab = c(1,1,7)) giving nonsense!
|
|
|
200 |
op <- par(lab = c(k,k,7), oma=c(1,0,0,0)); on.exit(par(op))
|
|
|
201 |
pairs(x, panel=mP, xlim=lim, ylim=lim, main=main, cex.main=1, ...)
|
|
|
202 |
}
|
|
|
203 |
|
|
|
204 |
mpairs() # 4x4 matrix of scatterplots
|
|
|
205 |
if(interAct) dev.set(if(length(dev.list()) < 2) dev.new() else dev.prev())
|
|
|
206 |
mpairs(horInd=1:2,verInd= 1:3 ) # 3x2 matrix: upper left [WRONG in R <= 3.5.0]
|
|
|
207 |
mpairs(horInd=3:2) # 2x4: middle rows swapped
|
|
|
208 |
mpairs( verInd=c(3,1)) # 4x2: cols 3,1
|
|
|
209 |
##
|
|
|
210 |
## now all with 'row1attop=FALSE'=======
|
|
|
211 |
if(interAct) dev.set(dev.prev())
|
|
|
212 |
mpairs(row1attop=FALSE) # 4x4 matrix of scatterplots -- perfect in R <= 3.5.0
|
|
|
213 |
if(interAct) dev.set(dev.next())
|
|
|
214 |
## a version of those above, with 'row1attop = FALSE'
|
|
|
215 |
mpairs(horInd=2:1,verInd=3:1 , row1attop=FALSE) # 3x2: *swapped* upper left
|
|
|
216 |
mpairs(horInd=c(3,1) , row1attop=FALSE) # 2x3: swapped outer rows
|
|
|
217 |
mpairs( verInd=c(3,2), row1attop=FALSE) # 3x2: cols 3,2
|
| 75182 |
maechler |
218 |
|
|
|
219 |
|
|
|
220 |
## axis() when in subnormal range (x is subnormal if 0 < x < .Machine$double.xmin):
|
|
|
221 |
(.min.exp.subnormal <- with(.Machine, double.min.exp + double.ulp.digits)) # -1074
|
|
|
222 |
(ry <- range(y74 <- 2^(.min.exp.subnormal + 0:50))) # all subnormal
|
|
|
223 |
plotNchk <- function(y) {
|
| 75196 |
maechler |
224 |
plot(y, log = "y", xlab="", ylab="", xaxt="n") # (as small pdf as possible)
|
| 75182 |
maechler |
225 |
ry <- range(y)
|
|
|
226 |
xp <- par("yaxp")
|
|
|
227 |
## and indeed yaxp do cover the range(y) :
|
|
|
228 |
stopifnot(xp[1] <= ry[1], ry[2] <= xp[2], xp[3] == 1)
|
|
|
229 |
invisible(xp)
|
|
|
230 |
}
|
| 75196 |
maechler |
231 |
if(interactive()) # not regularly, where pdf is stored
|
| 75182 |
maechler |
232 |
plotNchk(y74) # gives 3 warnings; 1. from pretty(): "very small range"
|
|
|
233 |
plotNchk(y74[1:8]) # 3 warnings *and* no error anymore
|
|
|
234 |
plotNchk(y74[1:2]) # (ditto)
|
| 77311 |
maechler |
235 |
|
|
|
236 |
|
|
|
237 |
## dotchart(*, pch=., groups=*) -- PR#16953
|
|
|
238 |
x <- setNames(0:5, c('a','aa', 'aaa', 'b', 'bb', 'bbb'))
|
|
|
239 |
y <- nchar(names(x)) # (minimal plot:)
|
|
|
240 |
dotchart(x, color=y, pch=y, groups=y, xaxt="n", frame.plot=FALSE)
|
|
|
241 |
## now pch and colors match groups
|