Subversion Repositories bdplot

Rev

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

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