Advertisement
Guest User

myplot.kohcodes.r

a guest
Oct 1st, 2017
766
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.96 KB | None | 0 0
  1. myplot.kohcodes <- function (x, whatmap, main, palette.name, bgcol, codeRendering,
  2. keepMargins, shape = c("round", "straight"), border = "black", ncolsleg=3,
  3. ...)
  4. {
  5. if (!keepMargins) {
  6. opar <- par(c("mar"))
  7. on.exit(par(opar))
  8. }
  9. if (is.null(palette.name))
  10. palette.name <- terrain.colors
  11. whatmap <- check.whatmap(x, whatmap)
  12. nmaps <- length(whatmap)
  13. if (is.list(x$codes)) {
  14. for (i in 1:nmaps) {
  15. huhn <- list(whatmap = 1, grid = x$grid)
  16. huhn$codes <- getCodes(x, whatmap[i])
  17. if (length(main) == length(x$codes)) {
  18. main.title <- main[whatmap[i]]
  19. }
  20. else {
  21. if (length(main) == nmaps) {
  22. main.title <- main[i]
  23. }
  24. else {
  25. if (length(main) == 1) {
  26. main.title <- main
  27. }
  28. else {
  29. if (is.null(main)) {
  30. if (!is.null(names(x$codes))) {
  31. main.title <- names(x$codes)[whatmap[i]]
  32. }
  33. else {
  34. main.title <- "Codes plot"
  35. }
  36. }
  37. }
  38. }
  39. }
  40. if (length(codeRendering) == length(x$codes)) {
  41. cR <- codeRendering[whatmap[i]]
  42. }
  43. else {
  44. if (length(codeRendering) == nmaps) {
  45. cR <- codeRendering[i]
  46. }
  47. else {
  48. cR <- codeRendering
  49. }
  50. }
  51. myplot.kohcodes(huhn, main = main.title, palette.name = palette.name,
  52. bgcol = bgcol, whatmap = NULL, codeRendering = cR,
  53. keepMargins = TRUE, shape = shape, border = border, ncolsleg,
  54. ...)
  55. }
  56. }
  57. else {
  58. codes <- x$codes
  59. nvars <- ncol(codes)
  60. maxlegendcols <- 3
  61. ncols <- ncolsleg
  62. print(ncolsleg)
  63. if (is.null(codeRendering))
  64. codeRendering <- ifelse(nvars < 15, "segments", "lines")
  65. margins <- rep(0.6, 4)
  66. if (!is.null(main))
  67. margins[3] <- margins[3] + 2
  68. par(mar = margins)
  69. if (codeRendering == "segments" & !is.null(colnames(codes))) {
  70. kohonen:::plot.somgrid(x$grid, ylim = c(max(x$grid$pts[, 2]) + min(x$grid$pts[,
  71. 2]), -2))
  72. current.plot <- par("mfg")
  73. plot.width <- diff(par("usr")[1:2])
  74. cex <- 1
  75.  
  76. leg.result <- legend(x = mean(x$grid$pts[, 1]), xjust = 0.5,
  77. y = 0, yjust = 1, legend = colnames(codes), cex = cex,
  78. plot = FALSE, ncol = ncols, fill = palette.name(nvars))
  79. while (leg.result$rect$w > plot.width) {
  80. cex <- cex * 0.9
  81. leg.result <- legend(x = mean(x$grid$pts[, 1]),
  82. xjust = 0.5, y = 0, yjust = 1, legend = colnames(codes),
  83. cex = cex, plot = FALSE, ncol = ncols, fill = palette.name(nvars))
  84. }
  85. leg.result <- legend(x = mean(x$grid$pts[, 1]), xjust = 0.5,
  86. y = 0, yjust = 1, cex = cex, legend = colnames(codes),
  87. plot = FALSE, ncol = ncols, fill = palette.name(nvars),
  88. ...)
  89. par(mfg = current.plot)
  90. kohonen:::plot.somgrid(x$grid, ylim = c(max(x$grid$pts[, 2]) + min(x$grid$pts[,
  91. 2]), -leg.result$rect$h))
  92. legend(x = mean(x$grid$pts[, 1]), xjust = 0.5, y = 0,
  93. yjust = 1, cex = cex, plot = TRUE, legend = colnames(codes),
  94. ncol = ncols, fill = palette.name(nvars), ...)
  95. }
  96. else {
  97. plot(x$grid, ...)
  98. }
  99. title.y <- max(x$grid$pts[, 2]) + 1.2
  100. if (title.y > par("usr")[4] - 0.2) {
  101. title(main)
  102. }
  103. else {
  104. text(mean(range(x$grid$pts[, 1])), title.y, main,
  105. adj = 0.5, cex = par("cex.main"), font = par("font.main"))
  106. }
  107. if (is.null(bgcol))
  108. bgcol <- "transparent"
  109. shape <- match.arg(shape)
  110. sym <- ifelse(shape == "round", "circle", ifelse(x$grid$topo ==
  111. "rectangular", "square", "hexagon"))
  112. switch(sym, circle = symbols(x$grid$pts[, 1], x$grid$pts[,
  113. 2], circles = rep(0.5, nrow(x$grid$pts)), inches = FALSE,
  114. add = TRUE, fg = border, bg = bgcol), hexagon = hexagons(x$grid$pts[,
  115. 1], x$grid$pts[, 2], unitcell = 1, col = bgcol, border = border),
  116. square = symbols(x$grid$pts[, 1], x$grid$pts[, 2],
  117. squares = rep(1, nrow(x$grid$pts)), inches = FALSE,
  118. add = TRUE, fg = border, bg = bgcol))
  119. if (codeRendering == "lines") {
  120. yrange <- range(codes)
  121. codes <- codes - mean(yrange)
  122. }
  123. else {
  124. codemins <- apply(codes, 2, min)
  125. codes <- sweep(codes, 2, codemins)
  126. }
  127. switch(codeRendering, segments = {
  128. stars(codes, locations = x$grid$pts, labels = NULL,
  129. len = 0.4, add = TRUE, col.segments = palette.name(nvars),
  130. draw.segments = TRUE)
  131. }, lines = {
  132. for (i in 1:nrow(x$grid$pts)) {
  133. if (yrange[1] < 0 & yrange[2] > 0) {
  134. lines(seq(x$grid$pts[i, 1] - 0.4, x$grid$pts[i,
  135. 1] + 0.4, length = 2), rep(x$grid$pts[i,
  136. 2], 2), col = "gray")
  137. }
  138. lines(seq(x$grid$pts[i, 1] - 0.4, x$grid$pts[i,
  139. 1] + 0.4, length = ncol(codes)), x$grid$pts[i,
  140. 2] + codes[i, ] * 0.8/diff(yrange), col = "red")
  141. }
  142. }, stars = stars(codes, locations = x$grid$pts, labels = NULL,
  143. len = 0.4, add = TRUE))
  144. }
  145. invisible()
  146. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement