Subversion Repositories bdplot

Rev

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