Subversion Repositories bdplot

Rev

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