Subversion Repositories bdplot

Rev

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