Subversion Repositories bdplot

Rev

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

Rev 5 Rev 7
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
                   xlab=bdp()$xlab, ylab=bdp()$ylab,
12
                   xlab=bdp()$xlab, ylab=bdp()$ylab,
13
                   xlim=range(x,bdp()$xlim,na.rm=TRUE),
13
                   xlim=range(x,bdp()$xlim,na.rm=TRUE),
14
                   ylim=range(bdp()$ylim,y,na.rm=TRUE),
14
                   ylim=range(bdp()$ylim,y,na.rm=TRUE),
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,
19
                   border=bdp()$border,
20
                   ...)
20
                   ...)
21
{
21
{
22
  prm <- bdp()
22
  prm <- bdp()
23
 
23
 
24
 
24
 
25
  ##Set margins
25
  ##Set margins
26
  mar <- prm$mar.nolab
26
  mar <- prm$mar.nolab
27
  if( !is.na(prm$xlab) ){ mar[1] <- prm$mar.lab[1] }
27
  if( !is.na(prm$xlab) ){ mar[1] <- prm$mar.lab[1] }
28
  if( !is.na(prm$ylab) ){ mar[2] <- prm$mar.lab[2] }
28
  if( !is.na(prm$ylab) ){ mar[2] <- prm$mar.lab[2] }
29
  if( any(!is.na(prm$toplab),!is.null(main)) ) { mar[3] <- prm$mar.lab[3] }
29
  if( any(!is.na(prm$toplab),!is.null(main)) ) { mar[3] <- prm$mar.lab[3] }
30
  if( !is.na(prm$rightlab) ){ mar[4] <- prm$mar.lab[4] }
30
  if( !is.na(prm$rightlab) ){ mar[4] <- prm$mar.lab[4] }
31
  par(mar=mar)
31
  par(mar=mar)
32
 
32
 
33
  par(mgp=prm$mgp.global)
33
  par(mgp=prm$mgp.global)
34
  
34
  
35
  ## Do the plot
35
  ## Do the plot
36
  ## the method variable could be used more intelligently. If x is numeric, only three methods are possible: plot, barplot, image.plot.
36
  ## the method variable could be used more intelligently. If x is numeric, only three methods are possible: plot, barplot, image.plot.
37
    ## maybe this check can be skipped. I guess x and y can just be deleted from these commands  
37
    ## maybe this check can be skipped. I guess x and y can just be deleted from these commands  
38
 ## if( prm$method=="xy"){
38
 ## if( prm$method=="xy"){
39
  
39
  
40
  if(!is.null(y)){
40
  if(!is.null(y)){
41
    ##       cat("The Bacher/Delff Plotting System (R), (C), TM operating in xy-plotting mode.\n")
41
    ##       cat("The Bacher/Delff Plotting System (R), (C), TM operating in xy-plotting mode.\n")
42
    
42
    
43
    plot(x, y,
43
    plot(x, y,
44
         type=type,
44
         type=type,
45
         xaxt=xaxt, yaxt="n",
45
         xaxt=xaxt, yaxt="n",
46
         cex=cex.plot,
46
         cex=cex.plot,
47
         xlab=xlab,
47
         xlab=xlab,
48
         ylab=ylab,
48
         ylab=ylab,
49
         xlim=xlim,
49
         xlim=xlim,
50
         ylim=ylim,
50
         ylim=ylim,
51
         col=col,
51
         col=col,
52
         main=main,
52
         main=main,
53
         ...)
53
         ...)
54
  } else if (is.numeric(x)){
54
  } else if (is.numeric(x)){
55
    plot(x,
55
    if(prm$method=="barplot"){
56
         type=type,
56
      barplot(height=x,
-
 
57
              ## maybe these two are wrong/stupid?
57
         xaxt="n", yaxt="n",
58
              cex.axis=prm$cex.lab,
58
         cex=cex.plot,
59
              cex.names=prm$cex.lab,
59
         xlab=xlab,
60
              col=bdp()$hcol,
60
         ylab=ylab,
61
              border=border,
61
         ylim=xlim,
62
              xlab=xlab,
62
         col=col,
63
              ylab=ylab,
63
         main=main,
64
              main=main,
64
         ...)
65
              ...)
-
 
66
      prm$draw.xaxis <- FALSE
65
  }  else if(prm$method=="barplot"){
67
      prm$draw.yaxis <- FALSE
-
 
68
      
-
 
69
    } else {
66
    barplot(height=x,
70
      plot(x,
67
            ## maybe these two are wrong/stupid?
71
           type=type,
68
            cex.axis=prm$cex.lab,
72
           xaxt="n", yaxt="n",
69
            cex.names=prm$cex.lab,
73
           cex=cex.plot,
70
            
74
           xlab=xlab,
71
            xlab=xlab,
75
           ylab=ylab,
72
            ylab=ylab,
76
           ylim=xlim,
73
            col=col,
77
           col=col,
74
            main=main,
78
           main=main,
75
            ...)
79
           ...)
76
    prm$draw.xaxis <- FALSE
-
 
77
    prm$draw.yaxis <- FALSE
-
 
78
    
80
    }
79
  } else if( class(x) == "acf"){
81
  } else if( class(x) == "acf"){
80
    ## Not cleaned up/checked
82
    ## Not cleaned up/checked
81
    plot(x, xlab="", ylab="", xaxt="n", yaxt="n", cex=prm$cex.plot, xlim=prm$xlim, ylim=prm$ylim,col=col,...)
83
    plot(x, xlab="", ylab="", xaxt="n", yaxt="n", cex=prm$cex.plot, xlim=prm$xlim, ylim=prm$ylim,col=col,...)
82
  }
84
  }
83
  else if( class(x) == "histogram"){
85
  else if( class(x) == "histogram"){
84
    ## Notice: the color of the bars can ONLY be set with bdp(hcol="color").
86
    ## Notice: the color of the bars can ONLY be set with bdp(hcol="color").
85
    plot(x,
87
    plot(x,
86
         xlab=xlab, ylab=ylab,
88
         xlab=xlab, ylab=ylab,
87
         main=prm$main,
89
         main=prm$main,
88
         xaxt="n",yaxt="n",col=bdp()$hcol,
90
         xaxt="n",yaxt="n",col=bdp()$hcol,
89
         border=border,
91
         border=border,
90
         ...)
92
         ...)
91
  } else if( class(x) == "trellis"){
93
  } else if( class(x) == "trellis"){
92
    ## Not cleaned up/checked
94
    ## Not cleaned up/checked
93
    ## very experimental
95
    ## very experimental
94
    cat("The Bacher/Delff Plotting System (R), (C), TM operating in lattice mode.\n Remeber that labels must written in the lattice object.\n")
96
    cat("The Bacher/Delff Plotting System (R), (C), TM operating in lattice mode.\n Remeber that labels must written in the lattice object.\n")
95
    ##lattice.options(layout.widths = prm$lattice.width,
97
    ##lattice.options(layout.widths = prm$lattice.width,
96
    ##                layout.heights = prm$lattice.height)
98
    ##                layout.heights = prm$lattice.height)
97
    trellis.par.set(prm$myLatticeSettings()) 
99
    trellis.par.set(prm$myLatticeSettings()) 
98
    plot(x,col=col,...)
100
    plot(x,col=col,...)
99
    ## with lattice/trellis, the axis drawing doesn't work.
101
    ## with lattice/trellis, the axis drawing doesn't work.
100
    prm$draw.xaxis <- FALSE
102
    prm$draw.xaxis <- FALSE
101
    prm$draw.yaxis <- FALSE
103
    prm$draw.yaxis <- FALSE
102
  }
104
  }
103
  else if (prm$method=="image.plot"){
105
  else if (prm$method=="image.plot"){
104
    cat("The Bacher/Delff Plotting System (R), (C), TM operating in image plotting mode.\nx and y axis can not be configured.\n")
106
    cat("The Bacher/Delff Plotting System (R), (C), TM operating in image plotting mode.\nx and y axis can not be configured.\n")
105
    image.plot(x=x,y=y,z=z,
107
    image.plot(x=x,y=y,z=z,
106
               xlab=xlab,
108
               xlab=xlab,
107
               ylab=ylab,
109
               ylab=ylab,
108
               ...)
110
               ...)
109
  }
111
  }
110
 
112
  
111
  ## We could make default values for grid.v og grid.h. Man maa kunne lave noget kvalificeret ud fra range og noget heltalsdivision
113
  ## We could make default values for grid.v og grid.h. Man maa kunne lave noget kvalificeret ud fra range og noget heltalsdivision
112
  
114
  
113
  ##Grid stuff
115
  ##Grid stuff
114
  if( prm$grid ){
116
  if( prm$grid ){
115
    grid(col=prm$grid.col)
117
    grid(col=prm$grid.col)
116
  }
118
  }
117
  ## vertical lines
119
  ## vertical lines
118
  if( !is.na(prm$grid.v) ){
120
  if( !is.na(prm$grid.v) ){
119
    if(prm$grid.v=="Def"){
121
    if(prm$grid.v=="Def"){
120
      grid(nx=NULL,ny=NA,col=prm$grid.col)
122
      grid(nx=NULL,ny=NA,col=prm$grid.col)
121
    } else
123
    } else
122
    {
124
    {
123
      abline(v=prm$grid.v, lty="dotted", col=prm$grid.col)
125
      abline(v=prm$grid.v, lty="dotted", col=prm$grid.col)
124
    }
126
    }
125
  }
127
  }
126
 
128
 
127
  if( !is.na(prm$grid.h) ){
129
  if( !is.na(prm$grid.h) ){
128
    if(prm$grid.h=="Def"){
130
    if(prm$grid.h=="Def"){
129
      grid(nx=NA,ny=NULL,col=prm$grid.col)
131
      grid(nx=NA,ny=NULL,col=prm$grid.col)
130
    }else{
132
    }else{
131
      abline(h=prm$grid.h, lty="dotted", col=prm$grid.col)}
133
      abline(h=prm$grid.h, lty="dotted", col=prm$grid.col)}
132
    }
134
    }
133
 
135
 
134
  ## Axis stuff
136
  ## Axis stuff
135
  if(prm$draw.xaxis){
137
  if(prm$draw.xaxis){
136
    ##    mgp.old <- par()$mgp
138
    ##    mgp.old <- par()$mgp
137
    ##    par(mgp) <- mgp.xaxis
139
    ##    par(mgp) <- mgp.xaxis
138
    options(warn=-1)
140
    options(warn=-1)
139
    if( "POSIXt"%in%class(x[1])){
141
    if( "POSIXt"%in%class(x[1])){
140
      axis.POSIXct(1, x,mgp=prm$mgp.xaxis, lwd=prm$lwd)
142
      axis.POSIXct(1, x,mgp=prm$mgp.xaxis, lwd=prm$lwd)
141
    } else {
143
    } else {
142
      axis(1, mgp=prm$mgp.xaxis, lwd=prm$lwd)
144
      axis(1, mgp=prm$mgp.xaxis, lwd=prm$lwd)
143
    }
145
    }
144
    options(warn=0)
146
    options(warn=0)
145
    
147
    
146
  }
148
  }
147
  if(prm$draw.yaxis){ axis(2, mgp=prm$mgp.yaxis, lwd=prm$lwd) }
149
  if(prm$draw.yaxis){ axis(2, mgp=prm$mgp.yaxis, lwd=prm$lwd) }
148
  
150
  
149
  ##Title stuff
151
  ##Title stuff
150
  ## Hvorfor??
152
  ## Hvorfor??
151
  scale <- 1
153
  scale <- 1
152
  ##  if( prm$type=="hist" & !is.na(prm$xlab) )
154
  ##  if( prm$type=="hist" & !is.na(prm$xlab) )
153
#  if( !is.na(prm$xlab)  ){ mtext(prm$xlab, line=prm$xlabLine, side=1, cex=prm$cex.lab/scale) }
155
#  if( !is.na(prm$xlab)  ){ mtext(prm$xlab, line=prm$xlabLine, side=1, cex=prm$cex.lab/scale) }
154
  ##  if( prm$type=="hist" &!is.na(prm$ylab) )
156
  ##  if( prm$type=="hist" &!is.na(prm$ylab) )
155
#  if( !is.na(prm$ylab)  ){ mtext(prm$ylab, line=prm$ylabLine, side=2, cex=prm$cex.lab/scale) }
157
#  if( !is.na(prm$ylab)  ){ mtext(prm$ylab, line=prm$ylabLine, side=2, cex=prm$cex.lab/scale) }
156
  if( !is.na(prm$toplab) ){ mtext(prm$xxlab, side=3, line=0.25, cex=prm$cex.lab/scale) }
158
  if( !is.na(prm$toplab) ){ mtext(prm$xxlab, side=3, line=0.25, cex=prm$cex.lab/scale) }
157
  if( !is.na(prm$rightlab) ){ mtext(prm$yylab, side=4, line=0.25, cex=prm$cex.lab/scale) }
159
  if( !is.na(prm$rightlab) ){ mtext(prm$yylab, side=4, line=0.25, cex=prm$cex.lab/scale) }
158
}
160
}
159
 
161
 
160
 
162
 
161
## a function to add points or lines to a plot. det kan skrives meget
163
## a function to add points or lines to a plot. det kan skrives meget
162
## paenere, om en streng indeholder en karakter.
164
## paenere, om en streng indeholder en karakter.
163
 
165
 
164
## grep("l",type)
166
## grep("l",type)
165
 
167
 
166
bdxyadd <- function(x,y=NULL,type=bdp()$type,lwd=bdp()$lwd,pch=bdp()$pch,col=bdp()$col,...){
168
bdxyadd <- function(x,y=NULL,type=bdp()$type,lwd=bdp()$lwd,pch=bdp()$pch,col=bdp()$col,...){
167
 
169
 
168
  prm <- .bdgetpars()
170
  prm <- .bdgetpars()
169
  
171
  
170
  if(any(type=="p",type=="l",type=="s")){
172
  if(any(type=="p",type=="l",type=="s")){
171
    if(!is.null(y)){
173
    if(!is.null(y)){
172
      points(x,y,cex=prm$cex.plot,pch=prm$pch,type=type,col=col,...)
174
      points(x,y,cex=prm$cex.plot,pch=prm$pch,type=type,col=col,...)
173
    } else {
175
    } else {
174
      points(x,cex=prm$cex.plot,pch=prm$pch,col=col,type=type,...)
176
      points(x,cex=prm$cex.plot,pch=prm$pch,col=col,type=type,...)
175
    }
177
    }
176
  } else if (type=="lp"|type=="pl"){
178
  } else if (type=="lp"|type=="pl"){
177
    if(!is.null(y)){
179
    if(!is.null(y)){
178
      points(x,y,cex=prm$cex.plot,pch=prm$pch,col=col,...)
180
      points(x,y,cex=prm$cex.plot,pch=prm$pch,col=col,...)
179
      lines(x,y,lwd=lwd,col=col,...)
181
      lines(x,y,lwd=lwd,col=col,...)
180
    } else {
182
    } else {
181
      points(x,cex=prm$cex.plot,pch=prm$pch,col=col,...)
183
      points(x,cex=prm$cex.plot,pch=prm$pch,col=col,...)
182
      lines(x,lwd=lwd,col=col,...)
184
      lines(x,lwd=lwd,col=col,...)
183
    }
185
    }
184
  }
186
  }
185
}
187
}
186
 
188
 
187
 
189
 
188
bdlegend <- function(legend,cex=bdp()$cex.legend,x=bdp()$pos.legend,bg=bdp()$bg.legend,col=bdp()$col,...){
190
bdlegend <- function(legend,cex=bdp()$cex.legend,x=bdp()$pos.legend,bg=bdp()$bg.legend,col=bdp()$col,...){
189
  prm <- .bdgetpars()
191
  prm <- .bdgetpars()
190
  ## dette er noget rod med baade x og pos, hvis man giver begge dele fucker det op.
192
  ## dette er noget rod med baade x og pos, hvis man giver begge dele fucker det op.
191
  legend(x=x,
193
  legend(x=x,
192
         cex=cex,
194
         cex=cex,
193
         legend=legend,
195
         legend=legend,
194
         bg=bg,
196
         bg=bg,
195
         col=col,
197
         col=col,
196
         ...)
198
         ...)
197
  
199
  
198
}
200
}
199
 
201
 
200
 
202