Subversion Repositories bdplot

Rev

Rev 69 | Details | Compare with Previous | Last modification | View Log | RSS feed

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