Subversion Repositories bdplot

Rev

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