Debian Codegraph @

R/p.goodies.R

raw
1 #### Original is /u/sfs/S/p.goodies.S [v 1.12 1999/05/06 10:17:00 sfs Exp ]
2 ####
3 ### p.goodies.S ---- SfS- S(plus) - Funktionen, welche
4 ### ---------------- mit 'p.' (für "Plot") beginnen [alte SfS-Tradition ..]
5 ### == =
6 ###
7 ### see also "/u/sfs/S/u.goodies.S"
8 ### "/u/sfs/S/f.goodies.S"
9 ### "/u/sfs/S/misc-goodies.S"
10 ###
11
12 ### **********************
13 ### INHALT von p.goodies.S (bitte jeweils ergaenzen):
14 ### **********************
15
16 ### p.clear Bildschirm "putzen"
17 ### p.datum Deutsches Datum "unten rechts"
18 ### p.dchisq \
19 ### p.dgamma > Dichten plotten
20 ### p.dnorm /
21 ### p.pairs 'pairs' mit mehr Moeglichkeiten
22 ### p.pllines
23 ### p.lm.hyperb --> ./linesHyberb.lm.R
24 ### p.scales
25 ### p.two.forget
26 ### p.two.res
27
28 ### p.profileTraces Profil-Spuren fuer Nichtlineare Regression
29 ### p.hboxp Horizontale Boxplots
30 ### p.arrows Nicer arrows(): FILLED arrow heads
31 ###
32 ### ==========================================================================
33
34 p.datum <- function(outer = FALSE, cex = 0.75, ...)
35 mtext(u.Datumvonheute(...), 4, cex = cex, adj = 0, outer = outer, las = 0)
36
37
38 ## ===========================================================================
39
40 ## curve(.. xlim..) only satisfactory from R version 1.2 on ..
41 p.dchisq <- function(nu, h0.col = "light gray", ...) {
42 x <- NULL # against codetools' FP warning
43 curve(dchisq(x, nu), xlim= qchisq(c(1e-5,.999), nu),
44 ylab = paste("dchisq(x, nu=",format(nu),")"), ...)
45 abline(h=0, col = h0.col)
46 }
47
48 p.dgamma <- function(shape, h0.col = "light gray", ...) {
49 x <- NULL # against codetools' FP warning
50 curve(dgamma(x, shape), xlim= qgamma(c(1e-5,.999), shape),
51 ylab = paste("dgamma(x, shape=",format(shape),")"), ...)
52 abline(h=0, col = h0.col)
53 }
54
55 p.dnorm <- function(mu = 0, s = 1, h0.col = "light gray",
56 ms.lines = TRUE, ms.col = "gray", ...)
57 {
58 f <- function(x) dnorm(x, mu, s)
59 curve(f, xlim = qnorm(c(1e-5, 0.999), mu, s),
60 ylab = substitute(phi(x, mu == m, sigma == ss),
61 list(m=format(mu), ss=format(s))), ...)
62 abline(h=0, col = h0.col)
63 if(ms.lines) {
64 segments(mu,0, mu, f(mu), col=ms.col)
65 f.ms <- f(mu-s)
66 arrows(mu-s, f.ms, mu+s, f.ms, length= 1/8, code= 3, col=ms.col)
67 text(mu+c(-s/2,s/2), f.ms, expression(-sigma, +sigma), adj=c(.5,0))
68 }
69 }
70
71 p.m <- function(mat, ...)
72 matplot(mat[, 1], mat[, -1, drop = FALSE], ...)
73
74 ## ===========================================================================
75
76 p.scales <- function(unit = relsysize * 2.54 * min(pin), relsysize = 0.05)
77 {
78 ## Fn.name: p.scales
79 ## Purpose: Conversion between plot scales: usr, cm, symbol
80 ## Author: W. Stahel , Date: May/90; updated: M.Mae. 9/93
81 ## ----------------------------------------------------------------
82 ## Arguments:
83 ## unit: length of unit (or x and y units) of symbol coordinates in cm
84 ## relsysize: same, as a proportion of the plotting area
85 ## ----------------------------------------------------------------
86 usr <- par("usr")
87 pin <- par("pin")
88 usr2cm <- (2.54 * pin)/(usr[c(2, 4)] - usr[c(1, 3)])
89 names(usr2cm) <- c("x", "y")
90 cbind(sy2usr = unit/usr2cm,
91 usr2cm = usr2cm)
92 }
93
94
95
96 p.profileTraces <-
97 function(x, cex=1, subtitle=paste("t-Profiles and traces of ",
98 deparse(attr(x,"summary")$formula)))
99 {
100 nx <- names(x)
101 np <- length(x)
102 opar <- par(oma = c(2, 2, 1.5, 0), mfrow = c(np, np),
103 mar = c(2,4, 0, 0) + 0.2)
104 on.exit(par(opar))
105 for (i in 1:np) {
106 for (j in 1:i) {
107 if (i == j) { ## Diagonale : Profil t-Funktionen
108 if (!is.null(this.comp <- x[[i]])) {
109 xx <- this.comp$par[, nx[i]]
110 tau <- this.comp[[1]]
111 plot(spline(xx, tau), xlab = "", ylab = "",
112 type = "l", las = 1, mgp = c(3, 0.8, 0),
113 cex = 0.5 * cex)
114 points(xx[tau == 0], 0, pch = 3)
115 mtext(side = 1, line = 0.8, at = -1/(2*np)+i/np,
116 text = nx[j] , outer = TRUE, cex = cex)
117 mtext(side = 2, line = 0.8, at = 1+1/(2*np)-i/np,
118 text = nx[i], outer = TRUE, cex = cex)
119 }
120 }
121 else { ## j < i : Likelihood Profilspuren
122 if ((!is.null(x.comp <- x[[j]])) & (!is.null(y.comp <- x[[i]]))) {
123 xx <- x.comp$par[, nx[j]]
124 xy <- x.comp$par[, nx[i]]
125 yx <- y.comp$par[, nx[j]]
126 yy <- y.comp$par[, nx[i]]
127 plot(xx, xy, xlab = "", ylab = "", las = 1,
128 mgp = c(3, 0.8, 0), type = "n",
129 xlim = range(c(xx, yx)),
130 ylim = range(c(xy, yy)), cex = 0.5 * cex)
131 lines(xx, xy, col = 2)
132 lines(yx, yy, col = 3)
133 }
134 }
135 }
136 if (i < np)
137 for (k in 1:(np - i)) frame()
138 }
139 mtext(side = 3, line = 0.2, text = subtitle,
140 outer = TRUE, cex = 1.2 * cex)
141 }
142
143 ## Test Beispiel :
144
145 ## --> /u/sfs/ueb/fortgeschrittene/loesungen/loes-rg.truthennen.R
146
147 ## mainly auxiliary of histBxp() [in ./misc-goodies.R ] :
148 p.hboxp <- function(x, y.lo, y.hi, boxcol = 3, medcol = 2,
149 medlwd = 5, whisklty = 2, staplelty = 1)
150 {
151 if(missing(y.hi) && length(y.lo) == 2) { y.hi <- y.lo[2]; y.lo <- y.lo[1] }
152 ## should test y.lo < y.hi, both to be numbers...
153
154 ##--- 2nd set of Defaults (by setting the args to NA) :
155 if(is.na(medcol)) medcol <- par("col")
156 if(is.na(medlwd)) medlwd <- par("lwd")
157 if(is.na(whisklty)) whisklty <- par("lty")
158 if(is.na(staplelty)) staplelty <- par("lty") #
159
160 b <- boxplot(x, plot = FALSE)
161 st <- c(b$stats)## names(st) <- c("max","Q3","med","Q1","min")
162
163 ##-------- drawing the boxplot --------------
164 ## coordinates :
165 m <- (y.hi + y.lo)/2
166 llhh <- c(y.lo, y.lo, y.hi, y.hi)
167 ## drawing the box
168 polygon(c(st[4], st[2], st[2], st[4]), llhh,
169 col = ifelse(boxcol == 0, par("col"), boxcol), lty = 1,
170 density = ifelse(boxcol == 0, 0, -1)) #
171 ## Median
172 lines(rep.int(st[3], 2), c(y.lo, y.hi),
173 col = ifelse(boxcol == 0 && missing(medcol), par("col"), medcol),
174 lwd = medlwd, lty = 1) #
175 ## Border of the box
176 lines(c(st[4], st[2], st[2], st[4]), llhh,
177 col = ifelse(boxcol == 0, par("col"), boxcol), lty = 1) #
178 ## Whiskers
179 lines(c(st[1:2], NA, st[4:5]), rep.int(m, 5), lty = whisklty) #
180 ## Staples
181 k <- .01 * diff(range(x))
182 lines(st[1]+ c(-k, 0, 0, -k), llhh, lty = staplelty)
183 lines(st[5]+ c( k, 0, 0, k), llhh, lty = staplelty)#
184 ## Outliers
185 for(out in b$out)
186 lines(rep.int(out, 2), c(y.lo, y.hi), lty = staplelty)
187 }
188
189
190
191 p.arrows <- function(x1, y1, x2, y2,
192 size=1, width = (sqrt(5)-1)/4/cin, fill = 2, ...)
193 {
194 ## Purpose: Nicer arrows(): FILLED arrow heads
195 ## -------------------------------------------------------------------------
196 ## Arguments: size: symbol size as a fraction of a character height
197 ## width: width of the Arrow Head
198 ## ...: further arguments for the segment routine
199 ## Author: Andreas Ruckstuhl, Date: 19 May 94; Cosmetic by MM: June'98
200 ## -------------------------------------------------------------------------
201 cin <- size*par("cin")[2] ## vertical symbol size in inches
202 uin <- 1/xyinch() # inches per usr unit
203
204 segments(x1, y1, x2, y2, ...)
205
206 ## Create coordinate of a polygon for a ``unit arrow head'':
207 x <- sqrt(seq(0, cin^2, length=floor(35*cin)+2))
208 delta <- 0.005/2.54 # ? 2.54cm = 1 in
209 x.arr <- c(-x, -rev(x))
210 wx2 <- width* x^2
211 y.arr <- c(- wx2 - delta, rev(wx2) + delta)
212 ## Polar(x.., y..):
213 deg.arr <- c(atan2(y.arr, x.arr), NA)# - NA to 'break' long polygon
214 r.arr <- c(sqrt(x.arr^2 + y.arr^2), NA)
215
216 ## Draw Arrow Head at (x2,y2)
217 theta <- atan2((y2-y1)*uin[2], (x2-x1)*uin[1])
218 lx <- length(x1)
219 Rep <- rep.int(length(deg.arr), lx)
220 x2 <- rep.int(x2, Rep)
221 y2 <- rep.int(y2, Rep)
222 theta <- rep.int(theta, Rep) + rep.int(deg.arr, lx)
223 r.arr <- rep.int(r.arr, lx)
224 polygon(x2+ r.arr*cos(theta)/uin[1],
225 y2+ r.arr*sin(theta)/uin[2], col= fill)
226 }