The R Project SVN R

Rev

Rev 71676 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 71676 Rev 74928
Line 156... Line 156...
156
## axis.Date() and axis.POSIXct() with reversed 'xlim'
156
## axis.Date() and axis.POSIXct() with reversed 'xlim'
157
toD <- as.Date("2016-08-19"); dates <- c(toD - 10, toD)
157
toD <- as.Date("2016-08-19"); dates <- c(toD - 10, toD)
158
plot(dates, 1:2, xlim = rev(dates),
158
plot(dates, 1:2, xlim = rev(dates),
159
     ann=FALSE, yaxt="n", frame.plot=FALSE)
159
     ann=FALSE, yaxt="n", frame.plot=FALSE)
160
## failed to label the dates in R <= 3.3.1
160
## failed to label the dates in R <= 3.3.1
-
 
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
}
-
 
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