/packages/archive/2010/04.2010/04.30.2010/spacodi/R/nodelabels.sp.R

http://github.com/eastman/spacodiR · R · 87 lines · 85 code · 2 blank · 0 comment · 34 complexity · c5da25246b6598268e6af16a0721ed97 MD5 · raw file

  1. nodelabels.sp <-
  2. function (text, node, adj = c(0.5, 0.5), frame = "rect", pch = NULL, thermo = NULL, pie = NULL, piecol = NULL, col = "black", bg = "lightblue", border = "black", ...) {
  3. lastPP <- get("last_plot.phylo", envir = .PlotPhyloEnv)
  4. if (missing(node))
  5. node <- (lastPP$Ntip + 1):length(lastPP$xx)
  6. XX <- lastPP$xx[node]
  7. YY <- lastPP$yy[node]
  8. BOTHlabels.sp(text, node, XX, YY, adj, frame, pch, thermo, pie, piecol, col, bg, border, ...)
  9. }
  10. BOTHlabels.sp <-
  11. function (text, sel, XX, YY, adj, frame, pch, thermo, pie, piecol, col, bg, border, ...) {
  12. if (missing(text))
  13. text <- NULL
  14. if (length(adj) == 1)
  15. adj <- c(adj, 0.5)
  16. if (is.null(text) && is.null(pch) && is.null(thermo) && is.null(pie))
  17. text <- as.character(sel)
  18. frame <- match.arg(frame, c("rect", "circle", "none"))
  19. args <- list(...)
  20. CEX <- if ("cex" %in% names(args))
  21. args$cex
  22. else par("cex")
  23. if (frame != "none" && !is.null(text)) {
  24. if (frame == "rect") {
  25. width <- strwidth(text, units = "inches", cex = CEX)
  26. height <- strheight(text, units = "inches", cex = CEX)
  27. if ("srt" %in% names(args)) {
  28. args$srt <- args$srt%%360
  29. if (args$srt == 90 || args$srt == 270) {
  30. tmp <- width
  31. width <- height
  32. height <- tmp
  33. }
  34. else if (args$srt != 0)
  35. warning("only right angle rotation of frame is supported;\n try `frame = \"n\"' instead.\n")
  36. }
  37. width <- xinch(width)
  38. height <- yinch(height)
  39. xl <- XX - xinch(0.10)
  40. xr <- XX + xinch(-0.01)
  41. yb <- YY - yinch(0.02)
  42. yt <- YY + yinch(0.02)
  43. rect(xl, yb, xr, yt, col = bg, border = border)
  44. }
  45. if (frame == "circle") {
  46. radii <- 0.8 * apply(cbind(strheight(text, units = "inches",
  47. cex = CEX), strwidth(text, units = "inches",
  48. cex = CEX)), 1, max)
  49. symbols(XX, YY, circles = radii, inches = max(radii), add = TRUE, bg = bg)
  50. }
  51. }
  52. if (!is.null(thermo)) {
  53. parusr <- par("usr")
  54. width <- CEX * (parusr[2] - parusr[1])/40
  55. height <- CEX * (parusr[4] - parusr[3])/15
  56. if (is.vector(thermo))
  57. thermo <- cbind(thermo, 1 - thermo)
  58. thermo <- height * thermo
  59. xl <- XX - width/2
  60. xr <- xl + width
  61. yb <- YY - height/2
  62. if (is.null(piecol))
  63. piecol <- rainbow(ncol(thermo))
  64. rect(xl, yb, xr, yb + thermo[, 1], border = NA, col = piecol[1])
  65. for (i in 2:ncol(thermo)) rect(xl, yb + rowSums(thermo[,
  66. 1:(i - 1), drop = FALSE]), xr, yb + rowSums(thermo[,
  67. 1:i]), border = NA, col = piecol[i])
  68. rect(xl, yb, xr, yb + height, border = "black")
  69. segments(xl, YY, xl - width/5, YY)
  70. segments(xr, YY, xr + width/5, YY)
  71. }
  72. if (!is.null(pie)) {
  73. if (is.vector(pie))
  74. pie <- cbind(pie, 1 - pie)
  75. xrad <- CEX * diff(par("usr")[1:2])/50
  76. xrad <- rep(xrad, length(sel))
  77. for (i in 1:length(sel)) floating.pie.asp(XX[i], YY[i],
  78. pie[i, ], radius = xrad[i], col = piecol)
  79. }
  80. if (!is.null(text))
  81. text(XX, YY, text, adj = adj, col = col, ...)
  82. if (!is.null(pch))
  83. points(XX + adj[1] - 0.5, YY + adj[2] - 0.5, pch = pch,
  84. col = col, bg = bg, ...)
  85. }