Subversion Repositories bdplot

Rev

Rev 43 | Rev 47 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

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