Subversion Repositories bdplot

Rev

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