R如何计算水平图内的面积

时间:2014-02-26 16:34:32

标签: r levelplot

我想计算一个等级图级别的区域......

例如:

火山数据中130和140 mts之间的区域:

contour(volcano)
win.graph()
dd<-levelplot(volcano, at=c(130,140))

我只想计算dd绘制的粉色区域,

有什么建议吗? : - )

注意:dd的结构是:

str(dd)

    List of 45
 $ formula          :Class 'formula' length 3 z ~ row * column
  .. ..- attr(*, ".Environment")=<environment: 0x031e53ec> 
 $ as.table         : logi FALSE
 $ aspect.fill      : logi FALSE
 $ legend           :List of 1
  ..$ right:List of 2
  .. ..$ fun : chr "draw.colorkey"
  .. ..$ args:List of 2
  .. .. ..$ key :List of 2
  .. .. .. ..$ at   : num [1:2] 130 140
  .. .. .. ..$ space: chr "right"
  .. .. ..$ draw: logi FALSE
 $ panel            : chr "panel.levelplot"
 $ page             : NULL
 $ layout           : NULL
 $ skip             : logi FALSE
 $ strip            : logi FALSE
 $ strip.left       : logi FALSE
 $ xscale.components:function (lim, packet.number = 0, packet.list = NULL, top = TRUE, ...)  
 $ yscale.components:function (lim, packet.number = 0, packet.list = NULL, right = TRUE, ...)  
 $ axis             :function (side = c("top", "bottom", "left", "right"), scales, components, as.table, labels = c("default", 
    "yes", "no"), ticks = c("default", "yes", "no"), ..., prefix = lattice.getStatus("current.prefix"))  
 $ xlab             : chr "row"
 $ ylab             : chr "column"
 $ xlab.default     : chr "row"
 $ ylab.default     : chr "column"
 $ xlab.top         : NULL
 $ ylab.right       : NULL
 $ main             : NULL
 $ sub              : NULL
 $ x.between        : num 0
 $ y.between        : num 0
 $ par.settings     : NULL
 $ plot.args        : NULL
 $ lattice.options  : NULL
 $ par.strip.text   : NULL
 $ index.cond       :List of 1
  ..$ : int 1
 $ perm.cond        : int 1
 $ condlevels       :List of 1
  ..$ : chr "1"
 $ call             : language levelplot(volcano, at = c(130, 140))
 $ x.scales         :List of 26
  ..$ draw          : logi TRUE
  ..$ axs           : chr "r"
  ..$ tck           : num [1:2] 1 1
  ..$ tick.number   : num 5
  ..$ at            : logi FALSE
  ..$ labels        : logi FALSE
  ..$ log           : logi FALSE
  ..$ alternating   : num [1:2] 1 2
  ..$ relation      : chr "same"
  ..$ abbreviate    : logi FALSE
  ..$ minlength     : num 4
  ..$ limits        : NULL
  ..$ format        : NULL
  ..$ equispaced.log: logi TRUE
  ..$ lty           : logi FALSE
  ..$ lwd           : logi FALSE
  ..$ cex           : logi [1:2] FALSE FALSE
  ..$ rot           : logi [1:2] FALSE FALSE
  ..$ col           : logi FALSE
  ..$ col.line      : logi FALSE
  ..$ alpha         : logi FALSE
  ..$ alpha.line    : logi FALSE
  ..$ font          : logi FALSE
  ..$ fontfamily    : logi FALSE
  ..$ fontface      : logi FALSE
  ..$ lineheight    : logi FALSE
 $ y.scales         :List of 26
  ..$ draw          : logi TRUE
  ..$ axs           : chr "r"
  ..$ tck           : num [1:2] 1 1
  ..$ tick.number   : num 5
  ..$ at            : logi FALSE
  ..$ labels        : logi FALSE
  ..$ log           : logi FALSE
  ..$ alternating   : num [1:2] 1 2
  ..$ relation      : chr "same"
  ..$ abbreviate    : logi FALSE
  ..$ minlength     : num 4
  ..$ limits        : NULL
  ..$ format        : NULL
  ..$ equispaced.log: logi TRUE
  ..$ lty           : logi FALSE
  ..$ lwd           : logi FALSE
  ..$ cex           : logi [1:2] FALSE FALSE
  ..$ rot           : logi [1:2] FALSE FALSE
  ..$ col           : logi FALSE
  ..$ col.line      : logi FALSE
  ..$ alpha         : logi FALSE
  ..$ alpha.line    : logi FALSE
  ..$ font          : logi FALSE
  ..$ fontfamily    : logi FALSE
  ..$ fontface      : logi FALSE
  ..$ lineheight    : logi FALSE
 $ panel.args.common:List of 5
  ..$ x     : int [1:5307] 1 2 3 4 5 6 7 8 9 10 ...
  ..$ y     : int [1:5307] 1 1 1 1 1 1 1 1 1 1 ...
  ..$ z     : num [1:5307] 100 101 102 103 104 105 105 106 107 108 ...
  ..$ at    : num [1:2] 130 140
  ..$ region: logi TRUE
 $ panel.args       :List of 1
  ..$ :List of 1
  .. ..$ subscripts: int [1:5307] 1 2 3 4 5 6 7 8 9 10 ...
 $ packet.sizes     : num 5307
 $ x.limits         : num [1:2] 0.5 87.5
 $ y.limits         : num [1:2] 0.5 61.5
 $ x.used.at        : NULL
 $ y.used.at        : NULL
 $ x.num.limit      : NULL
 $ y.num.limit      : NULL
 $ aspect.ratio     : num 0.701
 $ prepanel.default : chr "prepanel.default.levelplot"
 $ prepanel         : NULL
 - attr(*, "class")= chr "trellis"

1 个答案:

答案 0 :(得分:2)

关注levelplot的输出可能比实际需要的更复杂。以下代码直接计算130到140之间所有单元格的总面积:

# compute area represented by each cell in the matrix.
# `?volcano` says this is a regular 10m x 10m grid:
column_widths <- rep(10, ncol(volcano))
row_heights <- rep(10, nrow(volcano))
cell_areas <- row_heights %*% t(column_widths)

# which cells are >130, <140:
pink_cells <- (volcano > 130) & (volcano < 140)

# sum up area of pink_cells
(area <- sum(cell_areas[pink_cells]))

## [1] 43800