Debian Codegraph @

R/bubble.R

raw
1 # Colorbrewer.org, 5-class PiYG
2 #d01c8b
3 #f1b6da
4 #f7f7f7
5 #b8e186
6 #4dac26
7
8 "bubble" <- function (obj, zcol = 1, ..., fill = TRUE, maxsize = 3, do.sqrt = TRUE,
9 pch, col = c("#d01c8b", "#4dac26"), key.entries = quantile(data[,zcol]),
10 main = ifelse(is.numeric(zcol), names(data)[zcol], zcol),
11 identify = FALSE, labels = row.names(data.frame(obj)), key.space = "right",
12 scales = list(draw = FALSE), xlab = NULL, ylab = NULL, panel = panel.bubble,
13 sp.layout = NULL,
14 xlim = bbexpand(bbox(obj)[1,], 0.04),
15 ylim = bbexpand(bbox(obj)[2,], 0.04))
16 {
17 obj = as(obj, "SpatialPointsDataFrame")
18 data = as.data.frame(obj)
19 cc = coordinates(obj)
20 x = cc[, 1]
21 y = cc[, 2]
22 if (NCOL(data) == 1)
23 z = data
24 else if (NCOL(data) == 0)
25 z = rep(1, NROW(cc))
26 else
27 z = data[, zcol]
28 if (missing(pch))
29 pch = ifelse(fill, 16, 1)
30 if (length(col) == 1)
31 col = rep(col, 2)
32 z.col = as.vector(ifelse(z < 0, col[1], col[2]))
33 q = key.entries
34 q.pch = rep(pch, length(q))
35 q.text = as.character(round(q, 3))
36 q.col = as.vector(ifelse(q < 0, col[1], col[2]))
37 az = abs(z)
38 q = abs(q)
39 if (do.sqrt) {
40 az = sqrt(az)
41 q = sqrt(q)
42 }
43 cex = as.vector(maxsize * az/max(az,q))
44 q.cex = as.vector(maxsize * q/max(az,q))
45
46 scales = longlat.scales(obj, scales, xlim, ylim)
47 if (identify) {
48 plot(x, y, asp = 1, cex = cex, main = main, ...)
49 return(identify(x, y, labels))
50 } else {
51 key = list(space = key.space, points = list(pch = q.pch, col = q.col,
52 cex = q.cex), text = list(q.text))
53 xyplot(y ~ x, col = z.col, cex = cex, pch = pch, asp = mapasp(obj),
54 key = key, main = main, scales = scales, xlab = xlab, ylab = ylab,
55 panel = panel, sp.layout = sp.layout, ...)
56 }
57 }
58
59 panel.bubble = function(x, y, subscripts, sp.layout, ...) {
60 # sp.panel.layout(sp.layout, panel.number())
61 sppanel(sp.layout, panel.number())
62 panel.xyplot(x, y, ...)
63 }