Subversion Repositories bdplot

Rev

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

Rev 37 Rev 42
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
                   xlim=bdp()$xlim,ylim=bdp()$ylim,
14
                   xlim=bdp()$xlim,ylim=bdp()$ylim,
14
                   col=bdp()$col,main=NULL,
15
                   col=bdp()$col,main=NULL,
15
                   ## only used in case of plot mode
16
                   ## only used in case of plot mode
16
                   type=bdp()$type,
17
                   type=bdp()$type,
17
                   ## Only used in case of histogram plotting
18
                   ## Only used in case of histogram plotting
18
                   border=bdp()$border,
19
                   border=bdp()$border,
19
                   ...)
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
    ##  if( !is.na(prm$xlab) ){ mar[1] <- prm$mar.lab[1] }
57
  mar[3] <- detmar(3,tlab)
58
  #if( !is.na(prm$ylab) ){ mar[2] <- prm$mar.lab[2] }
58
  mar[4] <- detmar(4,rlab)
-
 
59
    
59
  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] }
60
  if( !is.na(prm$rightlab) ){ mar[4] <- prm$mar.lab[4] }
61
  ## if( !is.na(prm$rightlab) ){ mar[4] <- prm$mar.lab[4] }
61
  par(mar=mar)
62
  par(mar=mar)
62
 
63
 
63
  par(mgp=prm$mgp.global)
64
  par(mgp=prm$mgp.global)
64
  
65
  
65
### 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
66
  if(class(xaxt)=="function") {
67
  if(class(xaxt)=="function") {
67
 
68
 
68
    if(prm$debug)
69
    if(prm$debug)
69
      print(class(x))
70
      print(class(x))
70
 
71
 
71
    if(is.null(xaxt.in))
72
    if(is.null(xaxt.in))
72
      xaxt.in <- x
73
      xaxt.in <- x
73
 
74
 
74
    
75
    
75
    draw.xaxis <- "fun"
76
    draw.xaxis <- "fun"
76
    xaxt.fun <- xaxt
77
    xaxt.fun <- xaxt
77
    xaxt <- "n"
78
    xaxt <- "n"
78
  } else if(xaxt=="axis"){
79
  } else if(xaxt=="axis"){
79
    xaxt <- "n"
80
    xaxt <- "n"
80
    draw.xaxis <- TRUE
81
    draw.xaxis <- TRUE
81
  } else {
82
  } else {
82
    draw.xaxis <- FALSE
83
    draw.xaxis <- FALSE
83
  }
84
  }
84
  if(class(yaxt)=="function") {
85
  if(class(yaxt)=="function") {
85
    yaxt(ifelse(is.null(yaxt.in),range(y),yaxt.in))
86
    yaxt(ifelse(is.null(yaxt.in),range(y),yaxt.in))
86
    draw.yaxis <- FALSE
87
    draw.yaxis <- FALSE
87
  } else if(yaxt=="axis"){
88
  } else if(yaxt=="axis"){
88
    yaxt <- "n"
89
    yaxt <- "n"
89
    draw.yaxis <- TRUE
90
    draw.yaxis <- TRUE
90
  } else {
91
  } else {
91
    draw.yaxis <- FALSE
92
    draw.yaxis <- FALSE
92
  }
93
  }
93
  
94
  
94
 
95
 
95
 
96
 
96
### this variable needs to be initialized
97
### this variable needs to be initialized. Why?
97
  addcase <- NULL
98
  addcase <- NULL
98
  
99
  
99
  ## Do the plot
100
  ## Do the plot
100
  if(!is.null(y)){
101
  if(!is.null(y)){
101
 
102
 
102
    if(!is.null(z)){
103
    if(!is.null(z)){
103
      if(prm$debug)
104
      if(prm$debug)
104
        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")
105
      image(x=x,y=y,z=z,
106
      image(x=x,y=y,z=z,
106
            xlab=xlab,
107
            xlab=xlab,
107
            ylab=ylab,
108
            ylab=ylab,
108
            ...)
109
            ...)
109
    } else {
110
    } else {
110
      if(prm$debug)
111
      if(prm$debug)
111
        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")
112
      
113
      
113
      ## this is ugly, non standard.
114
      ## this is ugly, non standard.
114
      if(is.null(xlim))
115
      if(is.null(xlim))
115
        xlim <- range(x,na.rm=TRUE) #,bdp()$xlim,na.rm=TRUE)
116
        xlim <- range(x,na.rm=TRUE) #,bdp()$xlim,na.rm=TRUE)
116
      if(is.null(ylim))
117
      if(is.null(ylim))
117
        ylim <- range(y,na.rm=TRUE) #,bdp()$ylim,na.rm=TRUE)
118
        ylim <- range(y,na.rm=TRUE) #,bdp()$ylim,na.rm=TRUE)
118
      
119
      
119
      plot(x, y,
120
      plot(x, y,
120
           type="n",
121
           type="n",
121
           xaxt=xaxt, yaxt=yaxt,
122
           xaxt=xaxt, yaxt=yaxt,
122
           cex=cex.plot,
123
           cex=cex.plot,
123
           xlab=xlab,
124
           xlab=xlab,
124
           ylab=ylab,
125
           ylab=ylab,
125
           xlim=xlim,
126
           xlim=xlim,
126
           ylim=ylim,
127
           ylim=ylim,
127
           main=main,
128
           main=main,
128
           ...)
129
           ...)
129
 
130
 
130
      addcase <- "plotxy"
131
      addcase <- "plotxy"
131
      
132
      
132
    }
133
    }
133
  } else if (is.numeric(x)){
134
  } else if (is.numeric(x)){
134
    if(prm$method=="barplot"){
135
    if(prm$method=="barplot"){
135
      if(prm$debug)
136
      if(prm$debug)
136
        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")
137
      ## this does not provide the full x,y functionality of barplot
138
      ## this does not provide the full x,y functionality of barplot
138
      barplot(height=x,
139
      barplot(height=x,
139
              ## maybe these two are wrong/stupid?
140
              ## maybe these two are wrong/stupid?
140
              cex.axis=prm$cex.lab,
141
              cex.axis=prm$cex.lab,
141
              cex.names=prm$cex.lab,
142
              cex.names=prm$cex.lab,
142
              col=bdp()$hcol,
143
              col=bdp()$hcol,
143
              border=border,
144
              border=border,
144
              xlab=xlab,
145
              xlab=xlab,
145
              ylab=ylab,
146
              ylab=ylab,
146
              main=main,
147
              main=main,
147
              ...)
148
              ...)
148
      draw.xaxis <- FALSE
149
      draw.xaxis <- FALSE
149
      draw.yaxis <- FALSE
150
      draw.yaxis <- FALSE
150
      
151
      
151
    } else {
152
    } else {
152
      if(prm$debug)
153
      if(prm$debug)
153
        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")
154
 
155
 
155
      if(is.null(xlim))
156
      if(is.null(xlim))
156
        xlim <- c(1,length(x))#,bdp()$xlim),na.rm=TRUE)
157
        xlim <- c(1,length(x))#,bdp()$xlim),na.rm=TRUE)
157
      
158
      
158
      if(is.null(ylim))
159
      if(is.null(ylim))
159
        ylim <- range(x,na.rm=TRUE)#,bdp()$ylim,na.rm=TRUE)
160
        ylim <- range(x,na.rm=TRUE)#,bdp()$ylim,na.rm=TRUE)
160
      
161
      
161
      plot(x,
162
      plot(x,
162
           type="n",
163
           type="n",
163
           xaxt=xaxt, yaxt=yaxt,
164
           xaxt=xaxt, yaxt=yaxt,
164
           cex=cex.plot,
165
           cex=cex.plot,
165
           xlab=xlab,
166
           xlab=xlab,
166
           ylab=ylab,
167
           ylab=ylab,
167
           xlim=xlim,
168
           xlim=xlim,
168
           ylim=ylim,
169
           ylim=ylim,
169
           main=main,
170
           main=main,
170
           ...)
171
           ...)
171
      addcase <- "plotx"
172
      addcase <- "plotx"
172
    }
173
    }
173
  } else if( class(x) == "acf"){
174
  } else if( class(x) == "acf"){
174
    ## Not cleaned up/checked
175
    ## Not cleaned up/checked
175
    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,...)
176
  }  else if( class(x) == "histogram"){
177
  }  else if( class(x) == "histogram"){
177
    ## 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").
178
    plot(x,
179
    plot(x,
179
         xlab=xlab, ylab=ylab,
180
         xlab=xlab, ylab=ylab,
180
         main=prm$main,
181
         main=prm$main,
181
         xaxt=xaxt,yaxt=yaxt,col=bdp()$hcol,
182
         xaxt=xaxt,yaxt=yaxt,col=bdp()$hcol,
182
         border=border,
183
         border=border,
183
         ...)
184
         ...)
184
  } else if( class(x) == "trellis"){
185
  } else if( class(x) == "trellis"){
185
    ## Not cleaned up/checked
186
    ## Not cleaned up/checked
186
    ## very experimental
187
    ## very experimental
187
    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")
188
    ##lattice.options(layout.widths = prm$lattice.width,
189
    ##lattice.options(layout.widths = prm$lattice.width,
189
    ##                layout.heights = prm$lattice.height)
190
    ##                layout.heights = prm$lattice.height)
190
    trellis.par.set(prm$myLatticeSettings()) 
191
    trellis.par.set(prm$myLatticeSettings()) 
191
    plot(x,col=col,...)
192
    plot(x,col=col,...)
192
    ## with lattice/trellis, the axis drawing doesn't work.
193
    ## with lattice/trellis, the axis drawing doesn't work.
193
    draw.xaxis <- FALSE
194
    draw.xaxis <- FALSE
194
    draw.yaxis <- FALSE
195
    draw.yaxis <- FALSE
195
  } else if (class(x)=="princomp") {
196
  } else if (class(x)=="princomp") {
196
    if(prm$debug)
197
    if(prm$debug)
197
      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")
198
    plot(x,
199
    plot(x,
199
         main=prm$main,
200
         main=prm$main,
200
         ...)
201
         ...)
201
  } else if (prm$method=="image.plot"){
202
  } else if (prm$method=="image.plot"){
202
    ### This could be checked in the beginning by is.null(z)
203
    ### This could be checked in the beginning by is.null(z)
203
  }
204
  }
204
  
205
  
205
###Grid stuff
206
###Grid stuff
206
  ## 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.
207
  ## 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
208
  
209
  
209
  if( prm$grid ){
210
  if( prm$grid ){
210
    grid(col=prm$grid.col)
211
    grid(col=prm$grid.col)
211
  }
212
  }
212
  ## vertical lines
213
  ## vertical lines
213
  if( !is.na(prm$grid.v) ){
214
  if( !is.na(prm$grid.v) ){
214
    if(prm$grid.v=="Def"){
215
    if(prm$grid.v=="Def"){
215
      grid(nx=NULL,ny=NA,col=prm$grid.col)
216
      grid(nx=NULL,ny=NA,col=prm$grid.col)
216
    } else
217
    } else
217
    {
218
    {
218
      abline(v=prm$grid.v, lty="dotted", col=prm$grid.col)
219
      abline(v=prm$grid.v, lty="dotted", col=prm$grid.col)
219
    }
220
    }
220
  }
221
  }
221
 
222
 
222
  if( !is.na(prm$grid.h) ){
223
  if( !is.na(prm$grid.h) ){
223
    if(prm$grid.h=="Def"){
224
    if(prm$grid.h=="Def"){
224
      grid(nx=NA,ny=NULL,col=prm$grid.col)
225
      grid(nx=NA,ny=NULL,col=prm$grid.col)
225
    }else{
226
    }else{
226
      abline(h=prm$grid.h, lty="dotted", col=prm$grid.col)}
227
      abline(h=prm$grid.h, lty="dotted", col=prm$grid.col)}
227
    }
228
    }
228
 
229
 
229
### Now, grid has bee drawn. Then contents can be added.
230
### Now, grid has bee drawn. Then contents can be added.
230
 ## add support for more cases! 
231
 ## add support for more cases! 
231
  if(!is.null(addcase)){
232
  if(!is.null(addcase)){
232
    if(addcase=="plotxy") {
233
    if(addcase=="plotxy") {
233
      bdpoints(x, y,
234
      bdpoints(x, y,
234
               type=type,
235
               type=type,
235
               cex=cex.plot,
236
               cex=cex.plot,
236
               xlim=xlim,
237
               xlim=xlim,
237
               ylim=ylim,
238
               ylim=ylim,
238
               col=col,
239
               col=col,
239
               ...)
240
               ...)
240
    } else if (addcase=="plotx"){
241
    } else if (addcase=="plotx"){
241
      bdpoints(x, y,
242
      bdpoints(x, y,
242
               type=type,
243
               type=type,
243
               cex=cex.plot,
244
               cex=cex.plot,
244
               xlim=xlim,
245
               xlim=xlim,
245
               ylim=ylim,
246
               ylim=ylim,
246
               col=col,
247
               col=col,
247
               ...)
248
               ...)
248
    }
249
    }
249
  }
250
  }
250
 
251
 
251
### Axis stuff This part should be improved. A function that draws
252
### Axis stuff This part should be improved. A function that draws
252
### 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
253
### supply whatever vector to base it on (especially relevant for
254
### supply whatever vector to base it on (especially relevant for
254
### taxis and raxis)
255
### taxis and raxis)
255
  
256
  
256
  ## 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
257
  options(warn=-1)
258
  options(warn=-1)
258
  if(draw.xaxis=="fun"){
259
  if(draw.xaxis=="fun"){
259
    xaxt.fun(xaxt.in)
260
    xaxt.fun(xaxt.in)
260
  } else if(draw.xaxis){
261
  } else if(draw.xaxis){
261
    ## is the abscissa a time object?
262
    ## is the abscissa a time object?
262
    if( "POSIXt"%in%class(x[1])){
263
    if( "POSIXt"%in%class(x[1])){
263
      axis.POSIXct(1, x,mgp=prm$mgp.xaxis, lwd=prm$lwd)
264
      axis.POSIXct(1, x,mgp=prm$mgp.xaxis, lwd=prm$lwd)
264
    } else {
265
    } else {
265
      axis(1, mgp=prm$mgp.xaxis, lwd=prm$lwd)
266
      axis(1, mgp=prm$mgp.xaxis, lwd=prm$lwd)
266
    }
267
    }
267
    
268
    
268
  }
269
  }
269
 
270
 
270
  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) }
271
  ## top axis
272
  ## top axis
272
  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) }
273
  ## axis to the right
274
  ## axis to the right
274
  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) }
275
 
276
 
276
  ## switch back on warnings
277
  ## switch back on warnings
277
  options(warn=0)
278
  options(warn=0)
278
 
279
 
279
 
280
 
280
  ##Title stuff
281
  ##Title stuff
281
  ## Hvorfor??
282
  ## Hvorfor??
282
  scale <- 1
283
  scale <- 1
-
 
284
 
283
  ##  if( prm$type=="hist" & !is.na(prm$xlab) )
285
  ##  if( prm$type=="hist" & !is.na(prm$xlab) )
284
#  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) }
285
  ##  if( prm$type=="hist" &!is.na(prm$ylab) )
287
  ##  if( prm$type=="hist" &!is.na(prm$ylab) )
286
#  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) }
287
  if( !is.na(prm$toplab) ){ mtext(prm$toplab, 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) }
288
  if( !is.na(prm$rightlab) ){
290
  if( !is.na(prm$rlab) ){
289
    mtext(prm$rightlab, 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)
290
##    mtext(rightlab, side=4, line=0.5, cex=bdp()$cex.lab,mgp=bdp()$mgp.raxis)
-
 
291
  }
292
  }
292
}
293
}
293
 
294
 
294
 
295
 
295
 
296
 
296
 
297