I want to calculate the area within a level of levelplot...
For example:
The area between the 130 and 140 mts in the volcano data:
contour(volcano)
win.graph()
dd<-levelplot(volcano, at=c(130,140))
I want compute only the pink area plotted by dd,
any suggestions? :-)
NOTE: the structure of dd is:
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"
Focusing on the output of levelplot
may be more complicated than what you actually need. The following code directly computes the total area of all cells between 130 and 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