Subversion Repositories bdplot

Rev

Rev 60 | Rev 62 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
5 pdel 1
### bdplot.R
2
#### This is a collection of functions that must bpre reachable by the user This is called 
3
 
4
 
5
## strategy missing for xytype
6
## Could we test if there is an active graphics device? And if not, run bdopen()?
7
bdplot <- function(x, y=NULL, z=NULL,
8
                   ## The rest is irrelevant for the user
9
                   cex.plot=bdp()$cex.plot,
51 pdel 10
                   cex.main=bdp()$cex.main,
11
                   cex.sub=bdp()$cex.sub,
34 pdel 12
                   xaxt=bdp()$xaxt,yaxt=bdp()$yaxt,
36 pdel 13
                   xaxt.in=bdp()$xaxt.in,yaxt.in=bdp()$xaxt.in,
5 pdel 14
                   xlab=bdp()$xlab, ylab=bdp()$ylab,
42 pdel 15
                   tlab=bdp()$tlab, rlab=bdp()$rlab,
36 pdel 16
                   xlim=bdp()$xlim,ylim=bdp()$ylim,
5 pdel 17
                   col=bdp()$col,main=NULL,
18
                   ## only used in case of plot mode
19
                   type=bdp()$type,
20
                   ## Only used in case of histogram plotting
43 pdel 21
                   border=bdp()$border,freq=NULL,
42 pdel 22
                   ...){
36 pdel 23
 
24
 
25
  ## experimental. Is this the right consequence of not having a
26
  ## device opened?
27
 
28
  if(length(dev.list())==0){
29
    cat("No graphics device is initialised. Running bdopen() without arguments.\n")
30
    bdopen()
31
  }
32
 
55 pdel 33
## we'd like to be able to setup the plotting device from different functions. It's better to use bdp() to handle values then.
34
  bdp(xlab=xlab,ylab=ylab,rlab=rlab,tlab=tlab,
35
      cex.plot=cex.plot)
36 pdel 36
 
5 pdel 37
  prm <- bdp()
38
 
55 pdel 39
############# init starts - to be put in seperate function?
40
 
47 pdel 41
## It's a mess to set cex.lab here. Or it should not be set in
42
##  functions calls. If set both ways, it's scaled twice, meaning that
43
##  we don't know how to make it fit with mtext for eg rlab.
44
  par(cex.main=prm$cex.main, cex.lab=prm$cex.lab,
45
  cex.axis=prm$cex.axis)
55 pdel 46
 
9 pdel 47
  par(tcl=prm$tcl)
48
  par(lwd=prm$lwd)
49
  par(bty=prm$bty)
50
 
34 pdel 51
 
55 pdel 52
 ##Set margins. If mar is set, then that should be used.
53
  if (!is.null(prm$mar)){
54
    print(prm$mar)
55
    mar <- prm$mar
56
  } else {
37 pdel 57
 
55 pdel 58
    detmar <- function(idx,lab){
59
      if (is.null(lab)) {
60
        mar <- prm$mar.lab[idx]
61
      } else if(all(is.na(lab))) {
62
        mar <- prm$mar.nolab[idx]
63
      } else {
64
        mar <- prm$mar.lab[idx]
65
      }
66
      return(mar)
37 pdel 67
    }
58 pdel 68
##    browser()
55 pdel 69
    mar <- c(detmar(1,xlab),
70
             detmar(2,ylab),
58 pdel 71
             detmar(3,tlab)+ifelse(is.null(main),0,prm$mar.main),
55 pdel 72
             detmar(4,rlab))
58 pdel 73
##    print(length(mar))
74
##    print(mar)    
37 pdel 75
  }
5 pdel 76
  par(mar=mar)
77
 
78
  par(mgp=prm$mgp.global)
55 pdel 79
 
80
########### init done
81
 
34 pdel 82
 
47 pdel 83
### xaxt and yaxt can be set to "axis" which is the special case where the axis is drawn with the axis
35 pdel 84
  if(class(xaxt)=="function") {
85
 
36 pdel 86
    if(is.null(xaxt.in))
87
      xaxt.in <- x
88
 
35 pdel 89
    draw.xaxis <- "fun"
90
    xaxt.fun <- xaxt
34 pdel 91
    xaxt <- "n"
35 pdel 92
  } else if(xaxt=="axis"){
93
    xaxt <- "n"
34 pdel 94
    draw.xaxis <- TRUE
95
  } else {
96
    draw.xaxis <- FALSE
97
  }
35 pdel 98
  if(class(yaxt)=="function") {
99
    yaxt(ifelse(is.null(yaxt.in),range(y),yaxt.in))
100
    draw.yaxis <- FALSE
101
  } else if(yaxt=="axis"){
34 pdel 102
    yaxt <- "n"
103
    draw.yaxis <- TRUE
104
  } else {
105
    draw.yaxis <- FALSE
106
  }
107
 
108
 
32 pdel 109
 
42 pdel 110
### this variable needs to be initialized. Why?
36 pdel 111
  addcase <- NULL
5 pdel 112
 
113
  ## Do the plot
114
  if(!is.null(y)){
22 pdel 115
 
34 pdel 116
    if(!is.null(z)){
117
      if(prm$debug)
118
        cat("bdgraphics operating in image plotting mode.\nx and y axis can not be configured.\n")
119
      image(x=x,y=y,z=z,
120
            xlab=xlab,
121
            ylab=ylab,
122
            ...)
123
    } else {
124
      if(prm$debug)
125
        cat("The Bacher/Delff Plotting System (R), (C), TM operating in xy-plotting mode.\n")
126
 
127
      ## this is ugly, non standard.
128
      if(is.null(xlim))
36 pdel 129
        xlim <- range(x,na.rm=TRUE) #,bdp()$xlim,na.rm=TRUE)
34 pdel 130
      if(is.null(ylim))
36 pdel 131
        ylim <- range(y,na.rm=TRUE) #,bdp()$ylim,na.rm=TRUE)
54 pdel 132
 
133
      xlabel <- if (!missing(x)) 
134
        deparse(substitute(x))
135
      ylabel <- if (!missing(y)) 
136
        deparse(substitute(y))
137
      ##       xy <- xy.coords(x, y, xlabel, ylabel, log)
55 pdel 138
      xlab <- ifelse(is.null(xlab), xlabel, xlab)
139
      ylab <- ifelse(is.null(ylab), ylabel, ylab)
54 pdel 140
 
141
      ## here we make the empty plot
34 pdel 142
      plot(x, y,
36 pdel 143
           type="n",
34 pdel 144
           xaxt=xaxt, yaxt=yaxt,
145
           cex=cex.plot,
146
           xlab=xlab,
147
           ylab=ylab,
148
           xlim=xlim,
149
           ylim=ylim,
150
           main=main,
151
           ...)
36 pdel 152
 
153
      addcase <- "plotxy"
154
 
34 pdel 155
    }
5 pdel 156
  } else if (is.numeric(x)){
7 pdel 157
    if(prm$method=="barplot"){
34 pdel 158
      if(prm$debug)
159
        cat("The Bacher/Delff Plotting System (R), (C), TM operating in barplot mode.\n")
29 pdel 160
      ## this does not provide the full x,y functionality of barplot
7 pdel 161
      barplot(height=x,
162
              ## maybe these two are wrong/stupid?
163
              cex.axis=prm$cex.lab,
164
              cex.names=prm$cex.lab,
165
              col=bdp()$hcol,
166
              border=border,
167
              xlab=xlab,
168
              ylab=ylab,
59 pdel 169
              ylim=ylim,
7 pdel 170
              main=main,
171
              ...)
34 pdel 172
      draw.xaxis <- FALSE
173
      draw.yaxis <- FALSE
7 pdel 174
 
175
    } else {
34 pdel 176
      if(prm$debug)
177
        cat("The Bacher/Delff Plotting System operating in xy-plotting mode, only plotting x.\n")
36 pdel 178
 
22 pdel 179
      if(is.null(xlim))
36 pdel 180
        xlim <- c(1,length(x))#,bdp()$xlim),na.rm=TRUE)
34 pdel 181
 
22 pdel 182
      if(is.null(ylim))
36 pdel 183
        ylim <- range(x,na.rm=TRUE)#,bdp()$ylim,na.rm=TRUE)
22 pdel 184
 
7 pdel 185
      plot(x,
36 pdel 186
           type="n",
32 pdel 187
           xaxt=xaxt, yaxt=yaxt,
7 pdel 188
           cex=cex.plot,
189
           xlab=xlab,
190
           ylab=ylab,
22 pdel 191
           xlim=xlim,
21 pdel 192
           ylim=ylim,
7 pdel 193
           main=main,
194
           ...)
36 pdel 195
      addcase <- "plotx"
7 pdel 196
    }
5 pdel 197
  } else if( class(x) == "acf"){
198
    ## Not cleaned up/checked
32 pdel 199
    plot(x, xlab="", ylab="", xaxt=xaxt, yaxt=yaxt, cex=prm$cex.plot, xlim=prm$xlim, ylim=prm$ylim,col=col,...)
28 pdel 200
  }  else if( class(x) == "histogram"){
15 pdel 201
    ## Notice: the color of the bars can ONLY be set width bdp(hcol="color").
5 pdel 202
    plot(x,
56 pdel 203
         xlab=xlab,ylab=ylab,
58 pdel 204
         main=main,freq=freq,
61 pdel 205
         ylim=ylim,
32 pdel 206
         xaxt=xaxt,yaxt=yaxt,col=bdp()$hcol,
5 pdel 207
         border=border,
208
         ...)
209
  } else if( class(x) == "trellis"){
210
    ## Not cleaned up/checked
211
    ## very experimental
212
    cat("The Bacher/Delff Plotting System (R), (C), TM operating in lattice mode.\n Remeber that labels must written in the lattice object.\n")
213
    ##lattice.options(layout.widths = prm$lattice.width,
214
    ##                layout.heights = prm$lattice.height)
215
    trellis.par.set(prm$myLatticeSettings()) 
216
    plot(x,col=col,...)
217
    ## with lattice/trellis, the axis drawing doesn't work.
34 pdel 218
    draw.xaxis <- FALSE
219
    draw.yaxis <- FALSE
28 pdel 220
  } else if (class(x)=="princomp") {
34 pdel 221
    if(prm$debug)
222
      cat("The Bacher/Delff Plotting System operating in princomp plotting mode.\n")
28 pdel 223
    plot(x,
58 pdel 224
         main=main,
28 pdel 225
         ...)
51 pdel 226
  } else if (class(x)=="lm") {
227
    print("lm mode. This is experimental, mar and oma are set to fixed values.")
228
    par(mar=c(2,2,2,.3))
229
    par(oma = c(0, 0, 0, 0))
230
    par(cex=0.4)
231
    ### cex.caption is the title over each plot, cex.id magnification of point labels. What is eg "Cook's distance" written inside the plots?
232
    plot.lm(x,cex.caption=cex.sub,cex.id=.5)##,cex=cex.plot)
55 pdel 233
  }  else if (class(x)=="data.frame") {
234
    print("data.frame mode")
58 pdel 235
    plot(x,main=main,
55 pdel 236
         xaxt=xaxt,yaxt=yaxt)
237
  }else if (prm$method=="image.plot"){
15 pdel 238
    ### This could be checked in the beginning by is.null(z)
34 pdel 239
  }
7 pdel 240
 
31 pdel 241
###Grid stuff
242
  ## This really hould be done before adding the rest of the plot contents.
5 pdel 243
  ## We could make default values for grid.v og grid.h. Man maa kunne lave noget kvalificeret ud fra range og noget heltalsdivision
244
 
245
  if( prm$grid ){
246
    grid(col=prm$grid.col)
247
  }
248
  ## vertical lines
249
  if( !is.na(prm$grid.v) ){
250
    if(prm$grid.v=="Def"){
251
      grid(nx=NULL,ny=NA,col=prm$grid.col)
252
    } else
253
    {
254
      abline(v=prm$grid.v, lty="dotted", col=prm$grid.col)
255
    }
256
  }
257
 
258
  if( !is.na(prm$grid.h) ){
259
    if(prm$grid.h=="Def"){
260
      grid(nx=NA,ny=NULL,col=prm$grid.col)
261
    }else{
262
      abline(h=prm$grid.h, lty="dotted", col=prm$grid.col)}
263
    }
264
 
36 pdel 265
### Now, grid has bee drawn. Then contents can be added.
266
 ## add support for more cases! 
267
  if(!is.null(addcase)){
268
    if(addcase=="plotxy") {
269
      bdpoints(x, y,
270
               type=type,
271
               cex=cex.plot,
272
               xlim=xlim,
273
               ylim=ylim,
274
               col=col,
275
               ...)
276
    } else if (addcase=="plotx"){
277
      bdpoints(x, y,
278
               type=type,
279
               cex=cex.plot,
280
               xlim=xlim,
281
               ylim=ylim,
282
               col=col,
283
               ...)
284
    }
285
  }
286
 
31 pdel 287
### Axis stuff This part should be improved. A function that draws
288
### axis should be run for all four axis. And it should be possible to
289
### supply whatever vector to base it on (especially relevant for
44 pdel 290
### taxis and raxis). What about colors?
31 pdel 291
 
292
  ## this is because the use of mgp gives a ridiculous warning when too small
293
  options(warn=-1)
35 pdel 294
  if(draw.xaxis=="fun"){
36 pdel 295
    xaxt.fun(xaxt.in)
35 pdel 296
  } else if(draw.xaxis){
31 pdel 297
    ## is the abscissa a time object?
5 pdel 298
    if( "POSIXt"%in%class(x[1])){
299
      axis.POSIXct(1, x,mgp=prm$mgp.xaxis, lwd=prm$lwd)
300
    } else {
301
      axis(1, mgp=prm$mgp.xaxis, lwd=prm$lwd)
302
    }
303
 
304
  }
31 pdel 305
 
34 pdel 306
  if(draw.yaxis){ axis(2, y,mgp=prm$mgp.yaxis, lwd=prm$lwd) }
31 pdel 307
  ## top axis
308
  if(prm$draw.taxis){ axis(3, mgp=prm$mgp.taxis, lwd=prm$lwd) }
12 pdel 309
  ## axis to the right
310
  if(prm$draw.raxis){ axis(4, mgp=prm$mgp.raxis, lwd=prm$lwd) }
31 pdel 311
 
312
  ## switch back on warnings
313
  options(warn=0)
314
 
315
 
5 pdel 316
  ##Title stuff
317
  ## Hvorfor??
318
  scale <- 1
42 pdel 319
 
56 pdel 320
  if( !is.na(prm$tlab) ){
321
    mtext(prm$tlab, side=3, line=0.25, cex=prm$cex.lab/scale)
322
  }
323
  if( !is.na(prm$rlab) ){
42 pdel 324
    mtext(prm$rlab, side=4, line=0.75, cex=prm$cex.lab/scale,mgp=prm$mgp.raxis)
12 pdel 325
  }
5 pdel 326
}
327
 
328