wdVisXY.R 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. # * Author: Bangyou Zheng ([email protected])
  2. # * Created: 20/08/2010
  3. # *
  4. #' Visualize xml parameters
  5. #'
  6. #' @param doc doc
  7. #' @param xnode xnode
  8. #' @param ynode ynode
  9. #' @param width width
  10. #' @param height height
  11. #' @param keylab keylab
  12. #' @param keypos keypos
  13. #' @export
  14. wdVisXY <- function(doc, xnode, ynode,
  15. xlab = NULL,
  16. ylab = NULL,
  17. keylab = NULL,
  18. keypos = NULL,
  19. mtext = NULL)
  20. {
  21. # doc <- xmlInternalTreeParse( xml )
  22. library(lattice)
  23. library(grid)
  24. maxlen <- max( length( xnode ), length( ynode ) )
  25. xnode <- rep( xnode, len = maxlen )
  26. ynode <- rep( ynode, len = maxlen )
  27. res <- NULL
  28. dkeylab<- NULL
  29. for ( i in 1:maxlen )
  30. {
  31. temp <- xpathApply( doc, paste( "//", xnode[i], sep = "" ) )
  32. if ( length( temp ) == 0 )
  33. {
  34. stop( paste( "\"", xnode[i], "\" Can not be found \"", "\".", sep = "" ) )
  35. }
  36. xvalue <- xmlValue( temp[[1]] )
  37. xvalue <- as.numeric( strsplit( xvalue, " " )[[1]] )
  38. xvalue <- xvalue[ !is.na( xvalue ) ]
  39. temp <- xpathApply( doc, paste( "//", ynode[i], sep = "" ) )
  40. if ( length( temp ) == 0 )
  41. {
  42. stop( paste( "\"", ynode[i], "\" Can not be found \"","\".", sep = "" ) )
  43. }
  44. yvalue <- xmlValue( temp[[1]] )
  45. yvalue <- as.numeric( strsplit( yvalue, " " )[[1]] )
  46. yvalue <- yvalue[ !is.na( yvalue ) ]
  47. if ( length( xvalue ) != length( yvalue ) )
  48. {
  49. stop( paste( xnode[i], "(", toString( xvalue ),") and ", ynode[i], "(",
  50. toString( yvalue ), ") must be the same length.",
  51. sep = "" ) )
  52. }
  53. r <- NULL
  54. r <- cbind( x = xvalue, y = yvalue, index = i )
  55. dkeylab[i] <- paste( xnode[i], "vs", ynode[i] )
  56. r <- as.data.frame( r )
  57. res <- rbind( res, r )
  58. }
  59. res <- as.data.frame( res )
  60. if ( is.null( keylab ) )
  61. {
  62. keylab <- dkeylab
  63. }
  64. if ( is.null( keypos ) )
  65. {
  66. keypos <- c(1,1)
  67. }
  68. p <- NULL
  69. if ( maxlen == 1 )
  70. {
  71. p <- xyplot( y ~ x, data = res,
  72. xlab = xlab, ylab = ylab,
  73. page = function(x)
  74. {
  75. if (!is.null(mtext))
  76. {
  77. grid.text(mtext,
  78. x = 0.01, y = 0.99,
  79. default.units = "npc",
  80. just = c("left", "top"))
  81. }
  82. },
  83. panel = function(x, y, ...)
  84. {
  85. panel.xyplot(x, y, type = 'p', ...)
  86. x_r <- diff(range(x)) * 0.3
  87. x <- c(x[1] - x_r, x, x[length(x)] + x_r)
  88. y <- c(y[1], y, y[length(y)])
  89. panel.xyplot(x, y, type = 'l', ...)
  90. })
  91. }
  92. else
  93. {
  94. p <- xyplot( y ~ x, data = res, groups = index, type = "b",
  95. xlab = xlab, ylab = ylab,
  96. pch = 2:(maxlen+1),
  97. lty = 2:(maxlen+1),
  98. col = 1:maxlen,
  99. page = function(x)
  100. {
  101. if (!is.null(mtext))
  102. {
  103. grid.text(mtext,
  104. x = 0.01, y = 0.99,
  105. default.units = "npc",
  106. just = c("left", "top"))
  107. }
  108. },
  109. key = list(lines = list( col = 1:maxlen, lty = 2:(maxlen+1) ),
  110. points = list( col = 1:maxlen, pch = 2:(maxlen+1) ),
  111. text = list( lab = keylab ),
  112. corner = keypos ) )
  113. }
  114. p
  115. }