1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777
|
## lattice version of gplot.hexbin
## There are two major problems. (1) For comparability across panels,
## we want the same mincnt and maxcnt in all panels. However, a
## suitable default can really only be determined at printing time,
## since it would depend on the physical dimensions of the panel. (2)
## there is no proper way to communicate the mincnt and maxcnt to the
## legend.
## Tentative solution: the counts can be calculated once enough things
## are known, namely the aspect ratio, xbins and [xy]bnds. An
## important question then is whether [xy]bnds should be [xy]lim or
## range([xy]). Both should be allowed, since [xy]lim makes them
## comparable, range([xy]) potentially shows more detail. For
## relation != "same", both are more or less similar. An important
## observation is that with range([xy]), 'shape = aspect ratio of
## panel' does not guarantee symmetric hexagons, so shape has to be
## different for each panel.
## Only feasible approach I can think of is to produce the trellis
## object first (with known aspect, so aspect="fill" is absolutely
## no-no), then analyze the limits and relevant panel arguments to get
## 'maxcnt' (essentially doing a dry run of the panel calculations).
## This needs undocumented knowledge of the trellis object, which is
## kinda not good, but at least it gets the job done. Once we know
## maxcnt, we can also set up a suitable legend function.
## Unfortunately, this has the potential to screw up update calls that
## modify certain things. Is there any way to capture those? Maybe
## make a new class that inherits from "trellis". For now, we'll
## pretend that the problem doesn't exist.
## tool borrowed from lattice
updateList <- function (x, val)
{
if (is.null(x)) x <- list()
modifyList(x, val)
}
prepanel.hexbinplot <-
function(x, y, type = character(0),...)
{
if('tmd'%in%type){
tmp <- x
x <- (y + x)/sqrt(2)
y <- (y - tmp)/sqrt(2)
}
ans <-
list(xlim = range(x, finite = TRUE),
ylim = range(y, finite = TRUE),
dx = IQR(x,na.rm=TRUE),
dy = IQR(y,na.rm=TRUE))
}
panel.hexbinplot <-
function(x, y, ..., groups = NULL)
{
if (is.null(groups)) panel.hexbin(x, y, ...)
else panel.hexpose(x, y, ..., groups = groups)
}
panel.hexbin <-
function(x, y,
xbins = 30,
xbnds = c("data", "panel"), # was: xbnds = c("panel", "data"),
ybnds = c("data", "panel"), # was: ybnds = c("panel", "data"),
## special args
.prelim = FALSE,
.cpl = current.panel.limits(),
.xlim = .cpl$xlim,
.ylim = .cpl$ylim,
.aspect.ratio = 1, # default useful with splom(, panel = panel.hexbin)
type = character(0),
...,
check.erosion = FALSE)
{
if ("tmd" %in% type) {
tmp <- x
x <- (y + x)/sqrt(2)
y <- (y - tmp)/sqrt(2)
}
if (is.character(xbnds))
xbnds <-
switch(match.arg(xbnds),
panel = .xlim,
data = range(x, finite = TRUE))
if (is.character(ybnds))
ybnds <-
switch(match.arg(ybnds),
panel = .ylim,
data = range(y, finite = TRUE))
shape <-
.aspect.ratio * (diff(ybnds) / diff(.ylim)) /
(diff(xbnds) / diff(.xlim))
if (!missing(check.erosion))
warning("explicit 'check.erosion' specification ignored")
h <- hexbin(x = x, y = y,
xbins = xbins, shape = shape,
xbnds = xbnds, ybnds = ybnds)
if (.prelim)
return(max(h@count))
## have to do this because grid.hexagons croaks with unrecognized
## arguments:
args <- list(dat = h, check.erosion = FALSE, ...)
keep <- names(args) %in% names(formals(grid.hexagons))
if ('g' %in% type) panel.grid(h = -1, v = -1)
if ('hg' %in% type) panel.hexgrid(h)
do.call("grid.hexagons", args[keep])
if ("r" %in% type) panel.lmline(x, y, ...)
if ("smooth" %in% type) panel.hexloess(h,...)
invisible()
}
panel.hexboxplot <-
function(x, y,
xbins = 30,
xbnds = c("data", "panel"), # was: xbnds = c("panel", "data"),
ybnds = c("data", "panel"), # was: ybnds = c("panel", "data"),
## special args
.prelim = FALSE,
.cpl = current.panel.limits(),
.xlim = .cpl$xlim,
.ylim = .cpl$ylim,
.aspect.ratio = 1,
type = character(0),
cdfcut=.25,
shadow=.05,
...,
check.erosion = TRUE)
{
if (is.character(xbnds))
xbnds <-
switch(match.arg(xbnds),
panel = .xlim,
data = range(x, finite = TRUE))
if (is.character(ybnds))
ybnds <-
switch(match.arg(ybnds),
panel = .ylim,
data = range(y, finite = TRUE))
shape <-
.aspect.ratio * (diff(ybnds) / diff(.ylim)) /
(diff(xbnds) / diff(.xlim))
if (!missing(check.erosion))
warning("explicit 'check.erosion' specification ignored")
h <-hexbin(x = x, y = y,
xbins = xbins, shape = shape,
xbnds = xbnds, ybnds = ybnds,IDs=TRUE)
if (.prelim)
return(max(h@count))
## have to do this because grid.hexagons croaks with unrecognized
## arguments:
args <- list(dat = h, check.erosion = FALSE, ...)
keep <- names(args) %in% names(formals(grid.hexagons))
if ('hg' %in% type) panel.hexgrid(h)
if ('g' %in% type) panel.grid(h = -1, v = -1)
if(shadow) {
eh <- erode(h,cdfcut=shadow)
h.xy <- hcell2xy(eh,check.erosion=TRUE)
dx <- (0.5 * diff(eh@xbnds))/eh@xbins
dy <- (0.5 * diff(eh@ybnds))/(eh@xbins * h@shape * sqrt(3))
hexC <- hexcoords(dx, dy, sep = NULL)
hexpolygon(h.xy$x,h.xy$y, hexC, density = density,
fill = NA, border = gray(.75))
}
eh <- erode(h,cdfcut=cdfcut)
h.xy <- hcell2xy(eh,check.erosion=TRUE)
dx <- (0.5 * diff(eh@xbnds))/eh@xbins
dy <- (0.5 * diff(eh@ybnds))/(eh@xbins * h@shape * sqrt(3))
hexC <- hexcoords(dx, dy, sep = NULL)
hexpolygon(h.xy$x,h.xy$y, hexC, density = density,
fill = "green", border = gray(.75))
med <- which.max(eh@erode)
xnew <- h.xy$x[med]
ynew <- h.xy$y[med]
hexpolygon(xnew, ynew, hexC, density = density,
fill = "red", border =gray(.25))
invisible()
}
panel.hexpose <-
function(x, y, groups, subscripts,
xbins = 30,
xbnds = c("data", "panel"), # was: xbnds = c("panel", "data"),
ybnds = c("data", "panel"), # was: ybnds = c("panel", "data"),
## special args
.prelim = FALSE,
.cpl = current.panel.limits(),
.xlim = .cpl$xlim,
.ylim = .cpl$ylim,
.aspect.ratio = 1,
#erode Args
cdfcut=.05,
#hdiff Args
hexpose.focus=c(1,2),
hexpose.focus.colors=c("yellow","blue"),
hexpose.focus.border=c("cyan","orange"),
hexpose.median.color="red",
hexpose.median.border="black",
arrows = TRUE,
size = unit(0.1, "inches"),
arrow.lwd = 2,
eps = 1e-6,
type = character(0),
...,
check.erosion = TRUE)
{
if (is.character(xbnds))
xbnds <-
switch(match.arg(xbnds),
panel = .xlim,
data = range(x, finite = TRUE))
if (is.character(ybnds))
ybnds <-
switch(match.arg(ybnds),
panel = .ylim,
data = range(y, finite = TRUE))
shape <-
.aspect.ratio * (diff(ybnds) / diff(.ylim)) /
(diff(xbnds) / diff(.xlim))
if (is.numeric(groups)) groups <- as.character(groups[subscripts])
else groups <- groups[subscripts]
binL <- hexList(x, y, given=groups, xbins=xbins, shape=shape,
xbnds=xbnds, ybnds=ybnds)
if ('hs' %in% type) lapply(binL@hbins,smooth.hexbin)
binL@hbins <- lapply(binL@hbins,erode,cdfcut=cdfcut)
if ('hg' %in% type) panel.hexgrid(binL@hbins[[1]]) ## ???
if ('g' %in% type) panel.grid(h = -1, v = -1)
eroded <- unlist(lapply(binL@hbins, is, "erodebin"))
tmph.xy <- lapply(binL@hbins, hcell2xy, check.erosion = TRUE)
##__________________ Construct hexagon___________________
dx <- (0.5 * diff(binL@Xbnds))/xbins
dy <- (0.5 * diff(binL@Ybnds))/(xbins * binL@Shape * sqrt(3))
hexC <- hexcoords(dx = dx, dy = dy)
##__________________ Set up intersections and colors___________________
## Reorder so that the focus bin objects are at the top of the list
if(length(hexpose.focus) < binL@n) {
binL@hbins <- c(binL@hbins[hexpose.focus], binL@hbins[-hexpose.focus])
binL@Bnames <- c(binL@Bnames[hexpose.focus], binL@Bnames[-hexpose.focus])
}
cell.stat <- .all.intersect(binL@hbins)
cell.stat.n <- apply(cell.stat, 1, sum)
i.depth <- max(cell.stat.n)
diff.cols <- vector(mode = "list", length = i.depth)
levcells <- which(cell.stat.n == 1)
whichbin <- apply(cell.stat[levcells, ], 1, which)
## Set all the focal colors for the unique bin cells
## if not specified make them equally spaced on the color wheel
## with high saturation and set the background bins to gray
nfcol <- length(hexpose.focus)
nhb <- binL@n
nbcol <- nhb-nfcol
fills <-
if(is.null(hexpose.focus.colors)) {
if(nbcol > 0)
hsv(h = c(seq(0, 1, length = nfcol+1)[1:nfcol],rep(0, nbcol)),
s = c(rep(1, nfcol), rep(0, nbcol)),
## V = c(rep(1, nfcol), seq(.9, .1, length=nbcol))
v = c(rep(1, nfcol), rep(.9, nbcol)))
else hsv(h=seq(0, 1, length = nhb+1))[1:nfcol]
}
else {
foc.col <- t(col2rgb(hexpose.focus.colors))/255
if(nbcol > 0) {
bcol <- t(col2rgb(rep(grey(.6),nbcol)))/255
rbind(foc.col, bcol)
}
else foc.col
}
diff.cols[[1]] <- list(fill = fills, border = gray(.8))
##_______________ Full Cell Plotting for Unique BinL Cells_________________
if(length(levcells) > 0) {
for(i in unique(whichbin)) {
pcells <-
if(eroded[i])
binL@hbins[[i]]@cell[binL@hbins[[i]]@eroded]
else binL@hbins[[i]]@cell
pcells <- which(pcells %in% levcells[whichbin == i])
hexpolygon(x = tmph.xy[[i]]$x[pcells],
y = tmph.xy[[i]]$y[pcells], hexC,
border = hexpose.focus.border[i] ,
fill = hexpose.focus.colors[i] )
}
}
## Now do the intersections. All intersections are convex
## combinations of the colors of the overlapping unique bins in
## the CIEluv colorspace. so if the binlist is of length 2 and
## the focal hbins are "blue" and "yellow" respectively the
## intersection would be green. First I need to get this to work
## and then I can think about how to override this with an option
## in color.control. -NL
if(i.depth > 1) {
for(dl in 2:(i.depth)) {
levcells <- which(cell.stat.n == dl)
if(length(levcells) == 0) next
whichbin <- apply(cell.stat[levcells, ], 1,
function(x)paste(which(x), sep = "", collapse = ":"))
inter.nm <- unique(whichbin)
fills <- matrix(0, length(inter.nm), 3)
i <- 1
for(bn in inter.nm) {
who <- as.integer(unlist(strsplit(bn, ":")))
## FIXME (DS): this doesn't work
fills[i, ] <- mixcolors2(1/length(who),
diff.cols[[1]]$fill[who,])
i <- i+1
}
fills <- rgb(fills[,1],fills[,2],fills[,3])
diff.cols[[dl]] <- list(fill = fills,
border = gray((i.depth-dl)/i.depth))
##____Full Cell Plotting for Intersecting Cells at Intersection Depth i____
i <- 1
for(ints in inter.nm) {
bin.i <- as.integer(unlist(strsplit(ints, ":"))[1])
pcells <-
if(eroded[bin.i])
binL@hbins[[bin.i]]@cell[binL@hbins[[bin.i]]@eroded]
else binL@hbins[[bin.i]]@cell
pcells <- which(pcells %in% levcells[whichbin == ints])
hexpolygon(x = tmph.xy[[bin.i]]$x[pcells],
y = tmph.xy[[bin.i]]$y[pcells], hexC,
border = diff.cols[[dl]]$border ,
fill = diff.cols[[dl]]$fill[i] )
i <- i+1
}
}
}
if(any(eroded)) {
hmeds <- matrix(unlist(lapply(binL@hbins[eroded],
function(x)unlist(getHMedian(x)))),
ncol = 2, byrow = TRUE)
hexpolygon(x = hmeds[, 1], y = hmeds[, 2], hexC,
border = hexpose.median.border,
fill = hexpose.median.color)
if(arrows) {
for(i in hexpose.focus) {
for(j in hexpose.focus[hexpose.focus < i]) {
if(abs(hmeds[i, 1] - hmeds[j, 1]) +
abs(hmeds[i, 2] - hmeds[j, 2]) > eps)
grid.arrows(c(hmeds[i, 1], hmeds[j, 1]),
c(hmeds[i, 2], hmeds[j, 2]),
default.units = "native",
length = size, gp = gpar(lwd = arrow.lwd))
}
}
}
}
invisible()
}
hexbinplot <- function(x, data, ...) UseMethod("hexbinplot")
hexbinplot.formula <-
function(x, data = NULL,
prepanel = prepanel.hexbinplot,
panel = panel.hexbinplot,
groups = NULL,
aspect = "xy",
trans = NULL,
inv = NULL,
colorkey = TRUE,
...,
maxcnt,
legend = NULL,
legend.width = TRUE,
subset = TRUE)
{
ocall <- sys.call(sys.parent())
ocall[[1]] <- quote(hexbinplot)
ccall <- match.call()
if (is.logical(legend.width)) legend.width <- 1.2 * as.numeric(legend.width)
if (is.character(aspect) && aspect == "fill")
stop("aspect = 'fill' not permitted")
if (!is.null(trans) && is.null(inv))
stop("Must supply the inverse transformation 'inv'")
ccall$data <- data
ccall$prepanel <- prepanel
ccall$panel <- panel
ccall$aspect <- aspect
ccall$trans <- trans
ccall$inv <- inv
ccall$legend <- legend
ccall[[1]] <- quote(lattice::xyplot)
ans <- eval(ccall, parent.frame())
## panel needs to know aspect ratio to calculate shape
ans <- update(ans, .aspect.ratio = ans$aspect.ratio)
## also need maxcnt, o.w. can't draw legend, panels not comparable
## either
if (missing(maxcnt))
maxcnt <-
max(mapply(panel.hexbinplot, ## note: not 'panel'
x = lapply(ans$panel.args, "[[", "x"),
y = lapply(ans$panel.args, "[[", "y"),
.xlim =
if (is.list(ans$x.limits)) ans$x.limits
else rep(list(ans$x.limits), length(ans$panel.args)),
.ylim =
if (is.list(ans$y.limits)) ans$y.limits
else rep(list(ans$y.limits), length(ans$panel.args)),
MoreArgs =
c(ans$panel.args.common,
list(.prelim = TRUE, .cpl = NA))))
ans <- update(ans, maxcnt = maxcnt)
if (colorkey)
ans <-
update(ans,
legend = updateList(ans$legend,
list(right =
list(fun = hexlegendGrob,
args =
list(maxcnt = maxcnt,
trans = trans,
inv = inv,
legend = legend.width,
...)))))
ans$call <- ocall
ans
}
old.hexbinplot.formula <-
function(x, data = parent.frame(),
prepanel = prepanel.hexbinplot,
panel = if (is.null(groups)) panel.hexbinplot
else panel.hexpose,
groups=NULL,
aspect = "xy",
trans = NULL,
inv = NULL,
colorkey = TRUE,
...,
maxcnt,
legend = NULL,
legend.width = TRUE)
{
if (is.logical(legend.width))
legend.width <- 1.2 * as.numeric(legend.width)
if (is.character(aspect) && aspect == "fill")
stop("aspect = 'fill' not permitted")
if (!is.null(trans) && is.null(inv))
stop("Must supply the inverse transformation 'inv'")
groups <- eval(substitute(groups), data, parent.frame())
## There must be a better way to handle this, ugh.
ans <-
if(is.null(groups))
{
xyplot(x, data = data,
prepanel = prepanel,
panel = panel,
aspect = aspect,
trans = trans,
inv = inv,
legend = legend,
...)
}
else
{
xyplot(x, data = data,
prepanel = prepanel,
panel = panel,
groups=groups,
aspect = aspect,
trans = trans,
inv = inv,
legend = legend,
...)
}
## panel needs to know aspect ratio to calculate shape
ans <- update(ans, .aspect.ratio = ans$aspect.ratio)
## also need maxcnt, o.w. can't draw legend, panels not comparable
## either
if (missing(maxcnt))
maxcnt <-
max(mapply(panel.hexbinplot, ## note: not 'panel'
x = lapply(ans$panel.args, "[[", "x"),
y = lapply(ans$panel.args, "[[", "y"),
.xlim =
if (is.list(ans$x.limits)) ans$x.limits
else rep(list(ans$x.limits), length(ans$panel.args)),
.ylim =
if (is.list(ans$y.limits)) ans$y.limits
else rep(list(ans$y.limits), length(ans$panel.args)),
MoreArgs =
c(ans$panel.args.common,
list(.prelim = TRUE, .cpl = NA))))
ans <- update(ans, maxcnt = maxcnt)
if (colorkey)
ans <-
update(ans,
legend = updateList(ans$legend,
list(right =
list(fun = hexlegendGrob,
args =
list(maxcnt = maxcnt,
trans = trans,
inv = inv,
legend = legend.width,
...)))))
ans
}
## want a grob instead of actual plotting
hexlegendGrob <-
function(legend = 1.2,
inner = legend / 5,
cex.labels = 1,
cex.title = 1.2,
style = "colorscale",
minarea = 0.05, maxarea = 0.8,
mincnt = 1, maxcnt,
trans = NULL, inv = NULL,
colorcut = seq(0, 1, length = 17),
density = NULL, border = NULL, pen = NULL,
colramp = function(n) { LinGray(n,beg = 90,end = 15) },
...,
vp = NULL,
draw = FALSE)
{
## the formal arg matching should happen
style <- match.arg(style, eval(formals(grid.hexagons)[["style"]]))
if (style %in% c("centroids", "lattice", "colorscale")) {
## _______________tranformations_______________________
if(is.null(trans))
{
sc <- maxcnt - mincnt
bnds <- round(mincnt + sc * colorcut)
}
else
{
if(!is.function(trans) && !is.function(inv))
stop("'trans' and 'inv' must both be functions if 'trans' is not NULL")
con <- trans(mincnt)
sc <- trans(maxcnt) - con
bnds <- round(inv(con + sc * colorcut))
}
}
## grob
ans <-
switch(style,
"colorscale" = {
n <- length(bnds)
pen <- colramp(n-1)
## rectangles instead of polygons
## pol <-
## rectGrob(x = 0.5, y = 1:(n-1)/n,
## height = 1/n,
## default.units = "npc",
## gp = gpar(fill = pen, col = border))
hexxy <- hexcoords(dx = 1, n = 1)[c("x", "y")]
maxxy <- max(abs(unlist(hexxy)))
hexxy <- lapply(hexxy, function(x) 0.5 * x/ maxxy)
pol <-
polygonGrob(x = 0.5 + rep(hexxy$x, n-1),
y = (rep(1:(n-1), each = 6) + hexxy$y) / n,
id.lengths = rep(6, n-1),
gp = gpar(fill = pen, col = border),
default.units = "npc")
txt <-
textGrob(as.character(bnds),
x = 0.5,
y = (0:(n-1) + 0.5) / n,
gp = gpar(cex = cex.labels),
default.units = "npc")
ttl <- textGrob("Counts", gp = gpar(cex = cex.title))
key.layout <-
grid.layout(nrow = 2, ncol = 2,
heights =
unit(c(1.5, 1),
c("grobheight", "grobheight"),
data = list(ttl, txt)),
widths =
unit(c(1/n, 1),
c("grobheight", "grobwidth"),
data = list(pol, txt)),
respect = TRUE)
key.gf <- frameGrob(layout = key.layout, vp = vp)
key.gf <- placeGrob(key.gf, ttl, row = 1, col = 1:2)
key.gf <- placeGrob(key.gf, pol, row = 2, col = 1)
key.gf <- placeGrob(key.gf, txt, row = 2, col = 2)
key.gf
},
"centroids" = ,
"lattice" = {
warning("legend shows relative sizes")
## Note: it may not be impossible to get absolute
## sizes. The bigger problem is that when
## [xy]bnds="data", the sizes (for the same count) may
## not be the same across panels. IMO, that's a more
## useful feature than getting the absolute sizes
## right.
radius <- sqrt(minarea + (maxarea - minarea) * colorcut)
n <- length(radius)
if(is.null(pen)) pen <- 1
if(is.null(border)) border <- pen
hexxy <- hexcoords(dx = 1, n = 1)[c("x", "y")]
maxxy <- max(abs(unlist(hexxy)))
hexxy <- lapply(hexxy, function(x) 0.5 * x/ maxxy)
pol <-
polygonGrob(x = 0.5 + rep(radius, each = 6) * rep(hexxy$x, n),
y = (rep(0.5 + 1:n, each = 6) +
rep(radius, each = 6) * hexxy$y - 1) / n,
id.lengths = rep(6, n),
gp = gpar(fill = pen, col = border),
default.units = "npc")
txt <-
textGrob(as.character(bnds),
x = 0.5,
y = (1:n - 0.5) / n,
gp = gpar(cex = cex.labels),
default.units = "npc")
ttl <- textGrob("Counts", gp = gpar(cex = cex.title))
key.layout <-
grid.layout(nrow = 2, ncol = 2,
heights =
unit(c(1.5, 1),
c("grobheight", "grobheight"),
data = list(ttl, txt)),
widths =
unit(c(1/n, 1),
c("grobheight", "grobwidth"),
data = list(pol, txt)),
respect = TRUE)
key.gf <- frameGrob(layout = key.layout, vp = vp)
key.gf <- placeGrob(key.gf, ttl, row = 1, col = 1:2)
key.gf <- placeGrob(key.gf, pol, row = 2, col = 1)
key.gf <- placeGrob(key.gf, txt, row = 2, col = 2)
key.gf
},
"nested.lattice" = ,
"nested.centroids" = {
dx <- inner/2
dy <- dx/sqrt(3)
hexC <- hexcoords(dx, dy, n = 1, sep = NULL)
## _____________x scaling_____________________________
numb <- cut(floor(legend/inner), breaks = c(-1, 0, 2,4))
## Note: In old code
## top breaks=c(-1,0,2,4,8), numb<- 5 and size=1:9
if (is.na(numb)) numb <- 4
switch(numb,
{
warning("not enough space for legend")
return(textGrob(""))
},
size <- 5,
size <- c(1, 5, 9),
size <- c(1, 3, 5, 7, 9))
xmax <- length(size)
radius <- sqrt(minarea + (maxarea - minarea) * (size - 1)/9)
txt <- as.character(size)
##___________________y scaling_____________________
lab <- c("Ones", "Tens", "Hundreds",
"Thousands", "10 Thousands", "100 Thousands",
"Millions", "10 Millions",
"100 Millions", "Billions")
power <- floor(log10(maxcnt)) + 1
yinc <- 16 * dy
ysize <- yinc * power
xmid <- 0
x <- inner * (1:xmax - (1 + xmax)/2) + xmid
n <- length(x)
tx <- rep.int(hexC$x, n)
ty <- rep.int(hexC$y, n)
six <- rep.int(6:6, n)
## y <- rep.int(3 * dy - yinc, xmax)
y <- rep.int(3 * dy - 0.75 * yinc, xmax)
if (is.null(pen)) {
pen <- 1:power +1
pen <- cbind(pen, pen +10)
}
if (is.null(border)) border <- TRUE
key.layout <-
grid.layout(nrow = 1, ncol = 1,
heights = unit(ysize, "inches"),
widths = unit(legend, "inches"),
respect = TRUE)
key.gf <- frameGrob(layout = key.layout, vp = vp)
## for debugging
## key.gf <-
## placeGrob(key.gf, rectGrob(gp = gpar(fill = "transparent")))
n6 <- rep.int(6, n)
for(i in 1:power) {
y <- y + yinc
key.gf <-
placeGrob(key.gf,
polygonGrob(x = unit(legend / 2 + rep.int(hexC$x, n) + rep.int(x, n6), "inches"),
y = unit(rep.int(hexC$y, n) + rep.int(y, n6), "inches"),
id.lengths = n6,
gp =
gpar(col = pen[i, 1],
fill = if (border) 1 else pen[i, 1])),
row = 1, col = 1)
key.gf <-
placeGrob(key.gf,
polygonGrob(x = legend / 2 + tx * rep.int(radius, six) + rep.int(x, six),
y = ty * rep.int(radius, six) + rep.int(y, six),
default.units = "inches", id=NULL,
id.lengths=rep(6,n),
gp = gpar(fill = pen[i,2], col = border)),
row = 1, col = 1)
key.gf <-
placeGrob(key.gf,
textGrob(txt,
x = legend / 2 + x,
y = y - 4.5 * dy,
default.units = "inches",
gp = gpar(cex = cex.labels)),
row = 1, col = 1)
key.gf <-
placeGrob(key.gf,
textGrob(lab[i],
x = legend / 2 + xmid,
y = y[1] + 4.5 * dy,
default.units = "inches",
gp = gpar(cex = 1.3 * cex.title)),
row = 1, col = 1)
}
key.gf
})
if (draw)
{
grid.draw(ans)
invisible(ans)
}
else ans
}
|