Subversion Repositories bdplot

Rev

Rev 7 | Go to most recent revision | Details | Last modification | View Log | RSS feed

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