Advertisement
Guest User

myballoonplot.r

a guest
Aug 23rd, 2017
672
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.99 KB | None | 0 0
  1. myballoonplot.default <- function (x, y, z, xlab, ylab, zlab = deparse(substitute(z)),
  2. dotsize = 2/max(strwidth(19), strheight(19)), dotchar = 19,
  3. col.bar.x = "lightgray", col.bar.y = "lightgray",
  4. dotcolor = "skyblue", text.size = 1, text.color = par("fg"),
  5. main, label = TRUE, label.digits = 2, label.size = 1, label.color = par("fg"),
  6. scale.method = c("volume", "diameter"), scale.range = c("absolute",
  7. "relative"), colsrt = par("srt"), rowsrt = par("srt"),
  8. colmar = 1, rowmar = 2, show.zeros = FALSE, show.margins = TRUE,
  9. cum.margins = TRUE, sorted = TRUE, label.lines = TRUE, fun = function(x) sum(x,
  10. na.rm = T), hide.duplicates = TRUE, ...)
  11. {
  12. if (is.null(names(x))) {
  13. xnames <- as.character(substitute(x))
  14. if (length(xnames) > 1)
  15. xnames <- xnames[-1]
  16. }
  17. else xnames <- names(x)
  18. if (is.null(names(y))) {
  19. ynames <- as.character(substitute(y))
  20. if (length(ynames) > 1)
  21. ynames <- ynames[-1]
  22. }
  23. else ynames <- names(y)
  24. scale.method <- match.arg(scale.method)
  25. scale.range <- match.arg(scale.range)
  26. if (scale.method == "absolute" && any(z < 0, na.rm = TRUE))
  27. warning("z value(s) below zero detected.", " No balloons will be displayed for these cells.")
  28. if (missing(main)) {
  29. if (scale.method == "volume")
  30. main <- paste("Balloon Plot for ", paste(xnames,
  31. collapse = ", "), " by ", paste(ynames, collapse = ", "),
  32. ".\nArea is proportional to ", zlab, ".", sep = "")
  33. else main <- paste("Balloon Plot for ", paste(ynames,
  34. collapse = ", "), " by ", paste(ynames, collapse = ", "),
  35. ".\nDiameter is proportional to ", zlab, ".", sep = "")
  36. }
  37. if (length(dotcolor) < length(z))
  38. dotcolor <- rep(dotcolor, length = length(z))
  39. if (is.list(x)) {
  40. xlabs <- x
  41. x$sep = ":"
  42. x <- do.call(paste, x)
  43. }
  44. else xlabs <- list(x)
  45. if (is.list(y)) {
  46. ylabs <- y
  47. y$sep = ":"
  48. y <- do.call(paste, y)
  49. ylab <- paste(names(y))
  50. }
  51. else ylabs <- list(y)
  52. if (sorted) {
  53. ord.x <- do.call(order, xlabs)
  54. ord.y <- do.call(order, ylabs)
  55. }
  56. else ord.x <- ord.y <- 1:length(x)
  57. forceOrder <- function(X, sord, lord) factor(X[sord], levels = unique(X[lord]))
  58. x <- forceOrder(x, ord.y, ord.y)
  59. y <- forceOrder(y, ord.y, ord.y)
  60. z <- as.numeric(z[ord.y])
  61. dotcolor <- dotcolor[ord.y]
  62. xlabs <- unique(data.frame(lapply(xlabs, forceOrder, sord = ord.y,
  63. lord = ord.y)))
  64. ylabs <- unique(data.frame(lapply(ylabs, forceOrder, sord = ord.y,
  65. lord = ord.y)))
  66. myscale <- function(X, min = 0, max = 16, scale.method, scale.range) {
  67. if (scale.method == "volume") {
  68. X[X < 0] <- 0
  69. X <- sqrt(X)
  70. }
  71. if (scale.range == "relative")
  72. X <- (X - min(X, na.rm = TRUE))
  73. X <- X/max(X, na.rm = TRUE)
  74. X <- min + X * (max - min)
  75. cin.x <- par("cin")[1]
  76. cin.y <- par("cin")[2]
  77. if (cin.x < cin.y)
  78. X <- X * cin.x/cin.y
  79. X
  80. }
  81. nlabels.y <- length(ylabs)
  82. nlabels.x <- length(xlabs)
  83. tab1 <- split(data.frame(z, dotcolor, x, y), f = list(x,
  84. y))
  85. ztab <- do.call(rbind, lapply(tab1, FUN = function(X) cbind(z = fun(X[,
  86. 1]), X[1, -1])))
  87. oldpar <- par("xpd", "mar")
  88. on.exit(par(oldpar))
  89. if (!show.margins) {
  90. xlim = c(-0.5, nlevels(x) + nlabels.y * rowmar - 0.25)
  91. ylim = c(0.5, nlevels(y) + nlabels.x * colmar + 1)
  92. }
  93. else {
  94. xlim = c(-0.5, nlevels(x) + nlabels.y * rowmar + 1)
  95. ylim = c(0, nlevels(y) + nlabels.x * colmar + 1)
  96. }
  97. plot(x = nlabels.y * rowmar + 0.25 + as.numeric(ztab$x) -
  98. 1, y = nlevels(y) - as.numeric(ztab$y) + 1, cex = myscale(ztab$z,
  99. max = dotsize, scale.method = scale.method, scale.range = scale.range),
  100. pch = dotchar, col = as.character(ztab$dotcolor), xlab = "",
  101. ylab = "", xaxt = "n", yaxt = "n", bty = "n", xaxs = "i",
  102. yaxs = "i", xlim = xlim, ylim = ylim, ...)
  103. ny <- nlevels(ztab$y)
  104. nx <- nlevels(ztab$x)
  105. sumz <- sum(ztab$z, na.rm = TRUE)
  106. colsumz <- sapply(split(ztab$z, ztab$y), sum, na.rm = TRUE)
  107. rowsumz <- sapply(split(ztab$z, ztab$x), sum, na.rm = TRUE)
  108. if (show.margins) {
  109. text(x = (1:nx) + nlabels.y * rowmar + 0.25 - 1, y = 0.25,
  110. labels = format(c(sumz, rowsumz), digits = label.digits)[-1],
  111. font = 1, adj = c(0.5, 0), col = text.color, cex = text.size)
  112. rowlabs <- format(c(sumz, colsumz), digits = label.digits)[-1]
  113. width <- max(strwidth(rowlabs), na.rm = TRUE)
  114. text(x = nx + nlabels.y * rowmar - 0.25 + width, y = (ny:1),
  115. labels = rowlabs, font = 1, adj = c(1, 0.5), col = text.color,
  116. cex = text.size)
  117. text(x = nx + nlabels.y * rowmar - 0.25 + width, y = 0.25,
  118. labels = sumz, font = 1, adj = c(1, 0), col = text.color,
  119. cex = text.size)
  120. }
  121. if (cum.margins) {
  122. cx <- c(0, cumsum(rowsumz)/sumz)
  123. rect(xleft = nlabels.y * rowmar - 1 - 0.25 + 1:nx, xright = nlabels.y *
  124. rowmar - 1 + 0.75 + 1:nx, ybottom = ny + 0.75 + cx[1:nx] *
  125. colmar * nlabels.x, ytop = ny + 0.75 + cx[2:(nx +
  126. 1)] * colmar * nlabels.x, col = col.bar.y, border = NA)
  127. cy <- c(0, cumsum(colsumz)/sumz)
  128. rect(xleft = -0.5 + rowmar * cy[ny:1] * nlabels.y, xright = -0.5 +
  129. rowmar * cy[(ny + 1):2] * nlabels.y, ybottom = 1:ny -
  130. 0.5, ytop = 1:ny + 0.5, col = rev(col.bar.x), border = NA)
  131. tx <- paste(levels(x), "\n[", rowsumz, "]")
  132. ty <- paste(levels(y), "\n[", colsumz, "]")
  133. }
  134. segments(x0 = nlabels.y * rowmar - 0.25, x1 = nx + nlabels.y *
  135. rowmar - 0.25, y0 = (0:ny) + 0.5, y1 = (0:ny) + 0.5)
  136. segments(x0 = (0:nx) + nlabels.y * rowmar - 0.25, x1 = (0:nx) +
  137. nlabels.y * rowmar - 0.25, y0 = 0.5, y1 = ny + 0.5, )
  138. if (hide.duplicates)
  139. undupe <- function(X) {
  140. X <- as.character(X)
  141. c(X[1], ifelse(X[-1] == X[-length(X)], "", X[-1]))
  142. }
  143. else undupe <- function(X) X
  144. for (i in 1:nlabels.x) {
  145. y <- ny + 0.75 + (nlabels.x - i + 0.5) * colmar
  146. text(x = (1:nx) + nlabels.y * rowmar + 0.25 - 1, y = y,
  147. labels = undupe(xlabs[, i]), srt = colsrt, font = 1,
  148. col = text.color, cex = text.size)
  149. }
  150. for (i in 1:length(ylabs)) {
  151. text(y = ny:1, x = (i - 0.5) * rowmar - 0.5, labels = undupe(ylabs[,
  152. i]), srt = rowsrt, font = 1, col = text.color, cex = text.size)
  153. }
  154. if (missing(ylab) || length(ylab) == 0)
  155. text(x = ((1:length(ylabs)) - 0.5) * rowmar - 0.5, y = ny +
  156. 0.5, labels = ynames, srt = colsrt, font = 2, adj = c(0.5,
  157. 0), col = text.color, cex = text.size)
  158. else text(x = ((1:length(ylab)) - 0.5) * rowmar - 0.5, y = ny +
  159. 0.5, labels = ylab, srt = colsrt, font = 2, adj = c(0.5,
  160. 0), col = text.color, cex = text.size)
  161. if (missing(xlab) || length(xlab) == 0)
  162. text(x = nlabels.y * rowmar - 0.25 - strwidth(","), y = ny +
  163. 0.75 + ((nlabels.x:1) - 1 + 0.5) * colmar, labels = xnames,
  164. srt = colsrt, font = 2, adj = c(1, 0.5), col = text.color,
  165. cex = text.size)
  166. else text(x = nlabels.y * rowmar - 0.25 - strwidth(","),
  167. y = ny + 0.75 + ((length(xlab):1) - 1 + 0.5) * colmar,
  168. labels = xlab, srt = colsrt, font = 2, adj = c(1, 0.5),
  169. col = text.color, cex = text.size)
  170. if (label.lines) {
  171. segments(x0 = (0:nlabels.y) * rowmar - 0.5, x1 = (0:nlabels.y) *
  172. rowmar - 0.5, y0 = 0.5, y1 = ny + 0.5)
  173. segments(x0 = nlabels.y * rowmar - 0.25, x1 = nlabels.y *
  174. rowmar + nx - 0.25, y0 = (0:nlabels.x) * colmar +
  175. ny + 0.75, y1 = (0:nlabels.x) * colmar + ny + 0.75)
  176. }
  177. if (label) {
  178. if (show.zeros)
  179. indiv <- 1:length(ztab$y)
  180. else indiv <- which(ztab$z != 0)
  181. text(x = as.numeric(ztab$x[indiv]) + nlabels.y * rowmar -
  182. 0.75, y = ny - as.numeric(ztab$y[indiv]) + 1, labels = format(ztab$z[indiv],
  183. digits = label.digits), font = 2, adj = c(0.5, 0.5),
  184. col = label.color, cex = label.size)
  185. }
  186. title(main = main)
  187. }
  188.  
  189.  
  190. myballoonplot.table <- function (x, xlab, ylab, zlab, show.zeros = FALSE, show.margins = TRUE,
  191. ...)
  192. {
  193. obj <- x
  194. tmp <- as.data.frame(x)
  195. x <- tmp[, 1]
  196. y <- tmp[, 2]
  197. z <- tmp[, 3]
  198. tableflag <- TRUE
  199. if (missing(xlab))
  200. xlab <- names(dimnames(obj))[1]
  201. if (missing(ylab))
  202. ylab <- names(dimnames(obj))[2]
  203. if (missing(zlab))
  204. zlab <- "Freq"
  205. myballoonplot.default(x, y, z, xlab = xlab, ylab = ylab, zlab = zlab,
  206. show.zeros = show.zeros, show.margins = show.margins,
  207. ...)
  208. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement