Subversion Repositories bdplot

Rev

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

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