Subversion Repositories bdplot

Rev

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

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