Subversion Repositories bdplot

Rev

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

Rev 21 Rev 22
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=NULL,
14
                   ylim=range(bdp()$ylim,y,na.rm=TRUE),
14
                   ylim=NULL,
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
  par(cex.main=prm$cex.main, cex.lab=prm$cex.lab, cex.axis=prm$cex.axis)
25
  par(cex.main=prm$cex.main, cex.lab=prm$cex.lab, cex.axis=prm$cex.axis)
26
  par(tcl=prm$tcl)
26
  par(tcl=prm$tcl)
27
  par(lwd=prm$lwd)
27
  par(lwd=prm$lwd)
28
  par(bty=prm$bty)
28
  par(bty=prm$bty)
29
 
29
 
30
  
30
  
31
  ##Set margins
31
  ##Set margins
32
  mar <- prm$mar.nolab
32
  mar <- prm$mar.nolab
33
  if( !is.na(prm$xlab) ){ mar[1] <- prm$mar.lab[1] }
33
  if( !is.na(prm$xlab) ){ mar[1] <- prm$mar.lab[1] }
34
  if( !is.na(prm$ylab) ){ mar[2] <- prm$mar.lab[2] }
34
  if( !is.na(prm$ylab) ){ mar[2] <- prm$mar.lab[2] }
35
  if( any(!is.na(prm$toplab),!is.null(main)) ) { mar[3] <- prm$mar.lab[3] }
35
  if( any(!is.na(prm$toplab),!is.null(main)) ) { mar[3] <- prm$mar.lab[3] }
36
  if( !is.na(prm$rightlab) ){ mar[4] <- prm$mar.lab[4] }
36
  if( !is.na(prm$rightlab) ){ mar[4] <- prm$mar.lab[4] }
37
  par(mar=mar)
37
  par(mar=mar)
38
 
38
 
39
  par(mgp=prm$mgp.global)
39
  par(mgp=prm$mgp.global)
40
  
40
  
41
  ## Do the plot
41
  ## Do the plot
42
  if(!is.null(y)){
42
  if(!is.null(y)){
43
    ##       cat("The Bacher/Delff Plotting System (R), (C), TM operating in xy-plotting mode.\n")
43
    ##       cat("The Bacher/Delff Plotting System (R), (C), TM operating in xy-plotting mode.\n")
-
 
44
 
-
 
45
    if(is.null(xlim))
-
 
46
      xlim <- range(x,bdp()$xlim,na.rm=TRUE)
-
 
47
    if(is.null(ylim))
-
 
48
      ylim <- range(y,bdp()$ylim,na.rm=TRUE)
44
    
49
    
45
    plot(x, y,
50
    plot(x, y,
46
         type=type,
51
         type=type,
47
         xaxt=xaxt, yaxt="n",
52
         xaxt=xaxt, yaxt="n",
48
         cex=cex.plot,
53
         cex=cex.plot,
49
         xlab=xlab,
54
         xlab=xlab,
50
         ylab=ylab,
55
         ylab=ylab,
51
         xlim=xlim,
56
         xlim=xlim,
52
         ylim=ylim,
57
         ylim=ylim,
53
         col=col,
58
         col=col,
54
         main=main,
59
         main=main,
55
         ...)
60
         ...)
56
  } else if (is.numeric(x)){
61
  } else if (is.numeric(x)){
57
    if(prm$method=="barplot"){
62
    if(prm$method=="barplot"){
58
      barplot(height=x,
63
      barplot(height=x,
59
              ## maybe these two are wrong/stupid?
64
              ## maybe these two are wrong/stupid?
60
              cex.axis=prm$cex.lab,
65
              cex.axis=prm$cex.lab,
61
              cex.names=prm$cex.lab,
66
              cex.names=prm$cex.lab,
62
              col=bdp()$hcol,
67
              col=bdp()$hcol,
63
              border=border,
68
              border=border,
64
              xlab=xlab,
69
              xlab=xlab,
65
              ylab=ylab,
70
              ylab=ylab,
66
              main=main,
71
              main=main,
67
              ...)
72
              ...)
68
      prm$draw.xaxis <- FALSE
73
      prm$draw.xaxis <- FALSE
69
      prm$draw.yaxis <- FALSE
74
      prm$draw.yaxis <- FALSE
70
      
75
      
71
    } else {
76
    } else {
-
 
77
 
-
 
78
      if(is.null(xlim))
-
 
79
        xlim <- range(c(1:length(x),bdp()$xlim),na.rm=TRUE)
-
 
80
 
-
 
81
      if(is.null(ylim))
-
 
82
        ylim <- range(x,bdp()$ylim,na.rm=TRUE)
-
 
83
      
72
      plot(x,
84
      plot(x,
73
           type=type,
85
           type=type,
74
           xaxt="n", yaxt="n",
86
           xaxt="n", yaxt="n",
75
           cex=cex.plot,
87
           cex=cex.plot,
76
           xlab=xlab,
88
           xlab=xlab,
77
           ylab=ylab,
89
           ylab=ylab,
-
 
90
           xlim=xlim,
78
           ylim=ylim,
91
           ylim=ylim,
79
           col=col,
92
           col=col,
80
           main=main,
93
           main=main,
81
           ...)
94
           ...)
82
    }
95
    }
83
  } else if( class(x) == "acf"){
96
  } else if( class(x) == "acf"){
84
    ## Not cleaned up/checked
97
    ## Not cleaned up/checked
85
    plot(x, xlab="", ylab="", xaxt="n", yaxt="n", cex=prm$cex.plot, xlim=prm$xlim, ylim=prm$ylim,col=col,...)
98
    plot(x, xlab="", ylab="", xaxt="n", yaxt="n", cex=prm$cex.plot, xlim=prm$xlim, ylim=prm$ylim,col=col,...)
86
  }
99
  }
87
  else if( class(x) == "histogram"){
100
  else if( class(x) == "histogram"){
88
    ## Notice: the color of the bars can ONLY be set width bdp(hcol="color").
101
    ## Notice: the color of the bars can ONLY be set width bdp(hcol="color").
89
    plot(x,
102
    plot(x,
90
         xlab=xlab, ylab=ylab,
103
         xlab=xlab, ylab=ylab,
91
         main=prm$main,
104
         main=prm$main,
92
         xaxt="n",yaxt="n",col=bdp()$hcol,
105
         xaxt="n",yaxt="n",col=bdp()$hcol,
93
         border=border,
106
         border=border,
94
         ...)
107
         ...)
95
  } else if( class(x) == "trellis"){
108
  } else if( class(x) == "trellis"){
96
    ## Not cleaned up/checked
109
    ## Not cleaned up/checked
97
    ## very experimental
110
    ## very experimental
98
    cat("The Bacher/Delff Plotting System (R), (C), TM operating in lattice mode.\n Remeber that labels must written in the lattice object.\n")
111
    cat("The Bacher/Delff Plotting System (R), (C), TM operating in lattice mode.\n Remeber that labels must written in the lattice object.\n")
99
    ##lattice.options(layout.widths = prm$lattice.width,
112
    ##lattice.options(layout.widths = prm$lattice.width,
100
    ##                layout.heights = prm$lattice.height)
113
    ##                layout.heights = prm$lattice.height)
101
    trellis.par.set(prm$myLatticeSettings()) 
114
    trellis.par.set(prm$myLatticeSettings()) 
102
    plot(x,col=col,...)
115
    plot(x,col=col,...)
103
    ## with lattice/trellis, the axis drawing doesn't work.
116
    ## with lattice/trellis, the axis drawing doesn't work.
104
    prm$draw.xaxis <- FALSE
117
    prm$draw.xaxis <- FALSE
105
    prm$draw.yaxis <- FALSE
118
    prm$draw.yaxis <- FALSE
106
  }
119
  }
107
  else if (prm$method=="image.plot"){
120
  else if (prm$method=="image.plot"){
108
    ### This could be checked in the beginning by is.null(z)
121
    ### This could be checked in the beginning by is.null(z)
109
    cat("The Bacher/Delff Plotting System (R), (C), TM operating in image plotting mode.\nx and y axis can not be configured.\n")
122
    cat("The Bacher/Delff Plotting System (R), (C), TM operating in image plotting mode.\nx and y axis can not be configured.\n")
110
    image.plot(x=x,y=y,z=z,
123
    image.plot(x=x,y=y,z=z,
111
               xlab=xlab,
124
               xlab=xlab,
112
               ylab=ylab,
125
               ylab=ylab,
113
               ...)
126
               ...)
114
  }
127
  }
115
  
128
  
116
  ## We could make default values for grid.v og grid.h. Man maa kunne lave noget kvalificeret ud fra range og noget heltalsdivision
129
  ## We could make default values for grid.v og grid.h. Man maa kunne lave noget kvalificeret ud fra range og noget heltalsdivision
117
  
130
  
118
  ##Grid stuff
131
  ##Grid stuff
119
  if( prm$grid ){
132
  if( prm$grid ){
120
    grid(col=prm$grid.col)
133
    grid(col=prm$grid.col)
121
  }
134
  }
122
  ## vertical lines
135
  ## vertical lines
123
  if( !is.na(prm$grid.v) ){
136
  if( !is.na(prm$grid.v) ){
124
    if(prm$grid.v=="Def"){
137
    if(prm$grid.v=="Def"){
125
      grid(nx=NULL,ny=NA,col=prm$grid.col)
138
      grid(nx=NULL,ny=NA,col=prm$grid.col)
126
    } else
139
    } else
127
    {
140
    {
128
      abline(v=prm$grid.v, lty="dotted", col=prm$grid.col)
141
      abline(v=prm$grid.v, lty="dotted", col=prm$grid.col)
129
    }
142
    }
130
  }
143
  }
131
 
144
 
132
  if( !is.na(prm$grid.h) ){
145
  if( !is.na(prm$grid.h) ){
133
    if(prm$grid.h=="Def"){
146
    if(prm$grid.h=="Def"){
134
      grid(nx=NA,ny=NULL,col=prm$grid.col)
147
      grid(nx=NA,ny=NULL,col=prm$grid.col)
135
    }else{
148
    }else{
136
      abline(h=prm$grid.h, lty="dotted", col=prm$grid.col)}
149
      abline(h=prm$grid.h, lty="dotted", col=prm$grid.col)}
137
    }
150
    }
138
 
151
 
139
  ## Axis stuff
152
  ## Axis stuff
140
  if(prm$draw.xaxis){
153
  if(prm$draw.xaxis){
141
    ##    mgp.old <- par()$mgp
154
    ##    mgp.old <- par()$mgp
142
    ##    par(mgp) <- mgp.xaxis
155
    ##    par(mgp) <- mgp.xaxis
143
    options(warn=-1)
156
    options(warn=-1)
144
    if( "POSIXt"%in%class(x[1])){
157
    if( "POSIXt"%in%class(x[1])){
145
      axis.POSIXct(1, x,mgp=prm$mgp.xaxis, lwd=prm$lwd)
158
      axis.POSIXct(1, x,mgp=prm$mgp.xaxis, lwd=prm$lwd)
146
    } else {
159
    } else {
147
      axis(1, mgp=prm$mgp.xaxis, lwd=prm$lwd)
160
      axis(1, mgp=prm$mgp.xaxis, lwd=prm$lwd)
148
    }
161
    }
149
    options(warn=0)
162
    options(warn=0)
150
    
163
    
151
  }
164
  }
152
  if(prm$draw.yaxis){ axis(2, mgp=prm$mgp.yaxis, lwd=prm$lwd) }
165
  if(prm$draw.yaxis){ axis(2, mgp=prm$mgp.yaxis, lwd=prm$lwd) }
153
  ## axis to the right
166
  ## axis to the right
154
  if(prm$draw.raxis){ axis(4, mgp=prm$mgp.raxis, lwd=prm$lwd) }
167
  if(prm$draw.raxis){ axis(4, mgp=prm$mgp.raxis, lwd=prm$lwd) }
155
  ##Title stuff
168
  ##Title stuff
156
  ## Hvorfor??
169
  ## Hvorfor??
157
  scale <- 1
170
  scale <- 1
158
  ##  if( prm$type=="hist" & !is.na(prm$xlab) )
171
  ##  if( prm$type=="hist" & !is.na(prm$xlab) )
159
#  if( !is.na(prm$xlab)  ){ mtext(prm$xlab, line=prm$xlabLine, side=1, cex=prm$cex.lab/scale) }
172
#  if( !is.na(prm$xlab)  ){ mtext(prm$xlab, line=prm$xlabLine, side=1, cex=prm$cex.lab/scale) }
160
  ##  if( prm$type=="hist" &!is.na(prm$ylab) )
173
  ##  if( prm$type=="hist" &!is.na(prm$ylab) )
161
#  if( !is.na(prm$ylab)  ){ mtext(prm$ylab, line=prm$ylabLine, side=2, cex=prm$cex.lab/scale) }
174
#  if( !is.na(prm$ylab)  ){ mtext(prm$ylab, line=prm$ylabLine, side=2, cex=prm$cex.lab/scale) }
162
  if( !is.na(prm$toplab) ){ mtext(prm$toplab, side=3, line=0.25, cex=prm$cex.lab/scale) }
175
  if( !is.na(prm$toplab) ){ mtext(prm$toplab, side=3, line=0.25, cex=prm$cex.lab/scale) }
163
  if( !is.na(prm$rightlab) ){
176
  if( !is.na(prm$rightlab) ){
164
    mtext(prm$rightlab, side=4, line=0.75, cex=prm$cex.lab/scale,mgp=prm$mgp.raxis)
177
    mtext(prm$rightlab, side=4, line=0.75, cex=prm$cex.lab/scale,mgp=prm$mgp.raxis)
165
##    mtext(rightlab, side=4, line=0.5, cex=bdp()$cex.lab,mgp=bdp()$mgp.raxis)
178
##    mtext(rightlab, side=4, line=0.5, cex=bdp()$cex.lab,mgp=bdp()$mgp.raxis)
166
  }
179
  }
167
}
180
}
168
 
181
 
169
 
182
 
170
 
183
 
171
 
184