Subversion Repositories bdplot

Rev

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