{"id":536,"date":"2014-02-21T15:28:26","date_gmt":"2014-02-21T20:28:26","guid":{"rendered":"http:\/\/homepages.uc.edu\/~yaozo\/wordpress\/?p=536"},"modified":"2014-02-21T15:28:26","modified_gmt":"2014-02-21T20:28:26","slug":"level-and-contour-plots-of-raster-objects","status":"publish","type":"post","link":"https:\/\/zhuoyao.net\/index.php\/2014\/02\/21\/level-and-contour-plots-of-raster-objects\/","title":{"rendered":"Level and contour plots of Raster objects."},"content":{"rendered":"<h3>Description<\/h3>\n<p>Level and contour plots of Raster objects with\u00a0<code>lattice<\/code>\u00a0methods and marginal plots with\u00a0<code>grid<\/code>\u00a0objects.<\/p>\n<h3>Usage<\/h3>\n<pre>## S4 method for signature 'Raster,missing'\nlevelplot(x, data=NULL, layers,\n            margin=!(any(is.factor(x))), FUN.margin=mean, \n            maxpixels=1e5,\n            par.settings=rasterTheme(),\n            between=list(x=0.5, y=0.2),\n            as.table=TRUE,\n            xlab='', ylab='', main='',\n            names.attr,\n            scales=list(), scales.margin=NULL,\n            xscale.components=xscale.raster,\n            yscale.components=yscale.raster,\n            zscaleLog=NULL,\n            colorkey=list(space='right'),\n            panel=panel.levelplot,\n            contour=FALSE, region=TRUE, labels=FALSE,\n            ...)\n\n## S4 method for signature 'Raster,missing'\ncontourplot(x, data=NULL, layers, ...)<\/pre>\n<h3>Arguments<\/h3>\n<table summary=\"R argblock\">\n<tbody>\n<tr valign=\"top\">\n<td><code>x<\/code><\/td>\n<td>A Raster object.<\/td>\n<\/tr>\n<tr valign=\"top\">\n<td><code>data<\/code><\/td>\n<td>Not used.<\/td>\n<\/tr>\n<tr valign=\"top\">\n<td><code>layers<\/code><\/td>\n<td>A numeric or character which should indicate the layers to be displayed.<\/td>\n<\/tr>\n<tr valign=\"top\">\n<td><code>margin<\/code><\/td>\n<td>logical, if TRUE a marginal plot of the\u00a0<code>FUN.margin<\/code>\u00a0function to each coordinate will be displayed.<\/td>\n<\/tr>\n<tr valign=\"top\">\n<td><code>FUN.margin<\/code><\/td>\n<td>A function to be applied to each coordinate.<\/td>\n<\/tr>\n<tr valign=\"top\">\n<td><code>scales.margin<\/code><\/td>\n<td>A list with components\u00a0<code>x<\/code>\u00a0and\u00a0<code>y<\/code>. Each of these components is a numeric vector of length 2 defining the range for each marginal plot. If\u00a0<code>scales.margin=NULL<\/code>\u00a0the range is internally computed.<\/td>\n<\/tr>\n<tr valign=\"top\">\n<td><code>maxpixels<\/code><\/td>\n<td>A numeric, for\u00a0<code>sampleRegular<\/code>.<\/td>\n<\/tr>\n<tr valign=\"top\">\n<td><code>xlab, ylab, main<\/code><\/td>\n<td>Arguments for\u00a0<code>levelplot<\/code>.<\/td>\n<\/tr>\n<tr valign=\"top\">\n<td><code>names.attr<\/code><\/td>\n<td>Character, names to use in each panel. If missing its default value is the result of\u00a0<code>names(x)<\/code>\u00a0(after subsetting the layers to be displayed).<\/td>\n<\/tr>\n<tr valign=\"top\">\n<td><code>xscale.components, yscale.components<\/code><\/td>\n<td>See\u00a0<code>xscale.raster<\/code>\u00a0and\u00a0<code>yscale.raster<\/code>.<\/td>\n<\/tr>\n<tr valign=\"top\">\n<td><code>between, as.table, par.settings, scales, colorkey, panel, contour, region, labels<\/code><\/td>\n<td>Graphical parameters of\u00a0<code>lattice<\/code>. See\u00a0<code>xyplot<\/code>\u00a0for details.<\/td>\n<\/tr>\n<tr valign=\"top\">\n<td><code>zscaleLog<\/code><\/td>\n<td>Controls whether the Raster* will be log transformed before being passed to the panel function. Defaults to ???NULL???, in which case the Raster* is not transformed. Other possible values are any number that works as a base for taking logarithm, ???TRUE??? (which is equivalent to 10), and ???&#8221;e&#8221;??? (for the natural logarithm). As a side effect, the colorkey is labeled differently.<\/td>\n<\/tr>\n<tr valign=\"top\">\n<td><code>...<\/code><\/td>\n<td>Additional arguments for\u00a0<code>levelplot<\/code><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h3>Details<\/h3>\n<p>The result of the\u00a0<code>levelplot<\/code>\u00a0method is similar to the\u00a0<code>spplot<\/code>\u00a0method for\u00a0<code>Raster<\/code>\u00a0objects defined in the\u00a0<code>raster<\/code>\u00a0package. However, this method does not use the\u00a0<code>spplot<\/code>\u00a0of the\u00a0<code>sp<\/code>\u00a0package and, therefore, no conversion between classes is needed.<\/p>\n<p>The\u00a0<code>contourplot<\/code>\u00a0method is a wrapper for\u00a0<code>levelplot<\/code>\u00a0with the next additional default settings:\u00a0<code>cuts=7<\/code>,\u00a0<code>labels=TRUE<\/code>,\u00a0<code>contour<\/code>=TRUE,<code>pretty<\/code>=TRUE,\u00a0<code>region=TRUE<\/code>\u00a0and\u00a0<code>colorkey=TRUE<\/code>\u00a0(see the help of\u00a0<code>contourplot<\/code>\u00a0for details.)<\/p>\n<p><code>levelplot<\/code>\u00a0displays categorical data with a convenient legend. You should use\u00a0<code>ratify<\/code>\u00a0to define a layer as a categorical variable. It is able to display multilayer categorical rasters\u00a0<b>only if<\/b>\u00a0they share the same RAT (Raster Attribute Table).\u00a0<code>levelplot<\/code>\u00a0is not able to display multilayer rasters with factor\u00a0<b>and<\/b>\u00a0numeric layers. See\u00a0<code>ratify<\/code>\u00a0and the examples below for details.<\/p>\n<h3>Author(s)<\/h3>\n<p>Oscar Perpi????n Lamigueiro<\/p>\n<h3>See Also<\/h3>\n<p><code>spplot<\/code>,\u00a0<code>levelplot<\/code><\/p>\n<h3>Examples<\/h3>\n<pre>f &lt;- system.file(\"external\/test.grd\", package=\"raster\")\nr &lt;- raster(f)\nlevelplot(r)\n\n## With subticks\nlevelplot(r, xscale.components=xscale.raster.subticks,\n             yscale.components=yscale.raster.subticks)\n\nlevelplot(r, xscale.components=xscale.raster.subticks,\n             yscale.components=yscale.raster.subticks,\n             scales=list(x=list(rot=30, cex=0.8)))\n\n## defining the scales for the marginal plot\nlevelplot(r, scales.margin=list(x=c(100, 600), y=c(100, 1000)))\n## if a component of the list is null, it is internally calculated\nlevelplot(r, scales.margin=list(x=c(100, 1000)))\n\n## log-scale\nlevelplot(r^2, zscaleLog=TRUE, contour=TRUE)\n\ns &lt;- stack(r, r+500, r-500)\nlevelplot(s, contour=TRUE)\ncontourplot(s, labels=list(cex=0.4), cuts=12)\n\nlevelplot(s, names.attr=c('R', 'R + 500', 'R - 500'))\n\n## Add a layer of sampling points\n## and change the theme\npts &lt;- sampleRandom(r, size=20, sp=TRUE)\n\n## Using +.trellis and layer from latticeExtra\nlevelplot(r, par.settings = BTCTheme) + layer(sp.points(pts, col = 'red'))\ncontourplot(r, labels = FALSE) + layer(sp.points(pts, col = 'red'))\n\n## or with a custom panel function\nlevelplot(r, par.settings=BTCTheme,\n          panel=function(...){\n            panel.levelplot(...)\n            sp.points(pts, col='red')\n            })\n\n## Categorical data\nr &lt;- raster(nrow=10, ncol=10)\nr[] = 1\nr[51:100] = 3\nr[3:6, 1:5] = 5\nr &lt;- ratify(r)\n\nrat &lt;- levels(r)[[1]]\nrat$landcover &lt;- c('Pine', 'Oak', 'Meadow')\nlevels(r) &lt;- rat\nr\n\nlevelplot(r, col.regions=c('palegreen', 'midnightblue', 'indianred1'))\n\nr2 &lt;- raster(r)\nr2[] = 3\nr2[51:100] = 1\nr2[3:6, 1:5] = 5\n\nr3 &lt;- init(r, function(n)sample(c(1, 3, 5), n, replace=TRUE))\n\n## Multilayer categorical Raster* are displayed only if their RATs are the same\nlevels(r2) &lt;- levels(r3) &lt;- levels(r)\n\ns &lt;- stack(r, r2, r3)\nnames(s) &lt;- c('A', 'B', 'C')\n\nlevelplot(s)\n\n## Not run: \n##Solar irradiation data from CMSAF\n##Data available from http:\/\/www.box.net\/shared\/rl51y1t9sldxk54ogd44\n\nold &lt;- getwd()\n##change to your folder...\nsetwd('CMSAF')\nlistFich &lt;- dir(pattern='2008')\nstackSIS &lt;- stack(listFich)\nstackSIS &lt;- stackSIS*24 ##from irradiance (W\/m2) to irradiation Wh\/m2\nsetwd(old)\n\nidx &lt;- seq(as.Date('2008-01-15'), as.Date('2008-12-15'), 'month')\n\nSISmm &lt;- setZ(stackSIS, idx)\nnames(SISmm) &lt;- month.abb\n\nlevelplot(SISmm)\n\nlevelplot(SISmm, layers=1, FUN.margin=median, contour=TRUE)\n\n## End(Not run)<\/pre>\n<h3>Results<\/h3>\n<pre>R version 2.15.3 (2013-03-01) -- \"Security Blanket\"\nCopyright (C) 2013 The R Foundation for Statistical Computing\nISBN 3-900051-07-0\nPlatform: x86_64-unknown-linux-gnu (64-bit)\n\nR is free software and comes with ABSOLUTELY NO WARRANTY.\nYou are welcome to redistribute it under certain conditions.\nType 'license()' or 'licence()' for distribution details.\n\nR is a collaborative project with many contributors.\nType 'contributors()' for more information and\n'citation()' on how to cite R or R packages in publications.\n\nType 'demo()' for some demos, 'help()' for on-line help, or\n'help.start()' for an HTML browser interface to help.\nType 'q()' to quit R.\n\n&gt; library(rasterVis)\nLoading required package: raster\nLoading required package: sp\nLoading required package: lattice\nLoading required package: latticeExtra\nLoading required package: RColorBrewer\nLoading required package: hexbin\nLoading required package: grid\n&gt; png(filename=\"\/home\/oogasawa\/snapshot\/RGM3\/R_CC\/result\/rasterVis\/levelplot-methods.Rd_%03d_medium.png\", width=480, height=480)\n&gt; ### Name: levelplot-methods\n&gt; ### Title: Level and contour plots of Raster objects.\n&gt; ### Aliases: levelplot contourplot levelplot,Raster,missing-method\n&gt; ###   contourplot,Raster,missing-method\n&gt; ### Keywords: methods spatial\n&gt; \n&gt; ### ** Examples\n&gt; \n&gt; f &lt;- system.file(\"external\/test.grd\", package=\"raster\")\n&gt; r &lt;- raster(f)\n&gt; levelplot(r)\n&gt; \n&gt; ## With subticks\n&gt; levelplot(r, xscale.components=xscale.raster.subticks,\n+              yscale.components=yscale.raster.subticks)\n&gt; \n&gt; levelplot(r, xscale.components=xscale.raster.subticks,\n+              yscale.components=yscale.raster.subticks,\n+              scales=list(x=list(rot=30, cex=0.8)))\n&gt; \n&gt; ## defining the scales for the marginal plot\n&gt; levelplot(r, scales.margin=list(x=c(100, 600), y=c(100, 1000)))\n&gt; ## if a component of the list is null, it is internally calculated\n&gt; levelplot(r, scales.margin=list(x=c(100, 1000)))\n&gt; \n&gt; ## log-scale\n&gt; levelplot(r^2, zscaleLog=TRUE, contour=TRUE)\n&gt; \n&gt; s &lt;- stack(r, r+500, r-500)\n&gt; levelplot(s, contour=TRUE)\n&gt; contourplot(s, labels=list(cex=0.4), cuts=12)\n&gt; \n&gt; levelplot(s, names.attr=c('R', 'R + 500', 'R - 500'))\n&gt; \n&gt; ## Add a layer of sampling points\n&gt; ## and change the theme\n&gt; pts &lt;- sampleRandom(r, size=20, sp=TRUE)\n&gt; \n&gt; ## Using +.trellis and layer from latticeExtra\n&gt; levelplot(r, par.settings = BTCTheme) + layer(sp.points(pts, col = 'red'))\n&gt; contourplot(r, labels = FALSE) + layer(sp.points(pts, col = 'red'))\n&gt; \n&gt; ## or with a custom panel function\n&gt; levelplot(r, par.settings=BTCTheme,\n+           panel=function(...){\n+             panel.levelplot(...)\n+             sp.points(pts, col='red')\n+             })\n&gt; \n&gt; \n&gt; ## Categorical data\n&gt; r &lt;- raster(nrow=10, ncol=10)\n&gt; r[] = 1\n&gt; r[51:100] = 3\n&gt; r[3:6, 1:5] = 5\n&gt; r &lt;- ratify(r)\n&gt;      \n&gt; rat &lt;- levels(r)[[1]]\n&gt; rat$landcover &lt;- c('Pine', 'Oak', 'Meadow')\n&gt; levels(r) &lt;- rat\n&gt; r\nclass       : RasterLayer \ndimensions  : 10, 10, 100  (nrow, ncol, ncell)\nresolution  : 36, 18  (x, y)\nextent      : -180, 180, -90, 90  (xmin, xmax, ymin, ymax)\ncoord. ref. : +proj=longlat +datum=WGS84 \ndata source : in memory\nnames       : layer \nvalues      : 1, 5  (min, max)\nattributes  :\n ID landcover\n  1      Pine\n  3       Oak\n  5    Meadow\n\n&gt; \n&gt; levelplot(r, col.regions=c('palegreen', 'midnightblue', 'indianred1'))\n&gt; \n&gt; r2 &lt;- raster(r)\n&gt; r2[] = 3\n&gt; r2[51:100] = 1\n&gt; r2[3:6, 1:5] = 5\n&gt; \n&gt; r3 &lt;- init(r, function(n)sample(c(1, 3, 5), n, replace=TRUE))\n&gt; \n&gt; ## Multilayer categorical Raster* are displayed only if their RATs are the same\n&gt; levels(r2) &lt;- levels(r3) &lt;- levels(r)\n&gt; \n&gt; s &lt;- stack(r, r2, r3)\n&gt; names(s) &lt;- c('A', 'B', 'C')\n&gt; \n&gt; levelplot(s)\n&gt; \n&gt; \n&gt; ## Not run: \n&gt; ##D ##Solar irradiation data from CMSAF\n&gt; ##D ##Data available from http:\/\/www.box.net\/shared\/rl51y1t9sldxk54ogd44\n&gt; ##D \n&gt; ##D old &lt;- getwd()\n&gt; ##D ##change to your folder...\n&gt; ##D setwd('CMSAF')\n&gt; ##D listFich &lt;- dir(pattern='2008')\n&gt; ##D stackSIS &lt;- stack(listFich)\n&gt; ##D stackSIS &lt;- stackSIS*24 ##from irradiance (W\/m2) to irradiation Wh\/m2\n&gt; ##D setwd(old)\n&gt; ##D \n&gt; ##D idx &lt;- seq(as.Date('2008-01-15'), as.Date('2008-12-15'), 'month')\n&gt; ##D \n&gt; ##D SISmm &lt;- setZ(stackSIS, idx)\n&gt; ##D names(SISmm) &lt;- month.abb\n&gt; ##D \n&gt; ##D levelplot(SISmm)\n&gt; ##D \n&gt; ##D levelplot(SISmm, layers=1, FUN.margin=median, contour=TRUE)\n&gt; ## End(Not run)\n&gt; \n&gt; \n&gt; \n&gt; \n&gt; dev.off()\nnull device \n          1 \n&gt;<\/pre>\n<p>&nbsp;<\/p>\n<table>\n<tbody>\n<tr>\n<td><a href=\"http:\/\/rgm3.lab.nig.ac.jp\/RGM-files\/R_CC\/result\/rasterVis\/levelplot-methods.Rd_001_large.png\"><img decoding=\"async\" alt=\"\" src=\"http:\/\/rgm3.lab.nig.ac.jp\/RGM-files\/R_CC\/result\/rasterVis\/levelplot-methods.Rd_001_medium.png\" \/><\/a><a href=\"http:\/\/rgm3.lab.nig.ac.jp\/RGM-files\/R_CC\/result\/rasterVis\/levelplot-methods.Rd_002_large.png\"><img decoding=\"async\" alt=\"\" src=\"http:\/\/rgm3.lab.nig.ac.jp\/RGM-files\/R_CC\/result\/rasterVis\/levelplot-methods.Rd_002_medium.png\" \/><\/a><a href=\"http:\/\/rgm3.lab.nig.ac.jp\/RGM-files\/R_CC\/result\/rasterVis\/levelplot-methods.Rd_003_large.png\"><img decoding=\"async\" alt=\"\" src=\"http:\/\/rgm3.lab.nig.ac.jp\/RGM-files\/R_CC\/result\/rasterVis\/levelplot-methods.Rd_003_medium.png\" \/><\/a><a href=\"http:\/\/rgm3.lab.nig.ac.jp\/RGM-files\/R_CC\/result\/rasterVis\/levelplot-methods.Rd_004_large.png\"><img decoding=\"async\" alt=\"\" src=\"http:\/\/rgm3.lab.nig.ac.jp\/RGM-files\/R_CC\/result\/rasterVis\/levelplot-methods.Rd_004_medium.png\" \/><\/a><a href=\"http:\/\/rgm3.lab.nig.ac.jp\/RGM-files\/R_CC\/result\/rasterVis\/levelplot-methods.Rd_005_large.png\"><img decoding=\"async\" alt=\"\" src=\"http:\/\/rgm3.lab.nig.ac.jp\/RGM-files\/R_CC\/result\/rasterVis\/levelplot-methods.Rd_005_medium.png\" \/><\/a><a href=\"http:\/\/rgm3.lab.nig.ac.jp\/RGM-files\/R_CC\/result\/rasterVis\/levelplot-methods.Rd_006_large.png\"><img decoding=\"async\" alt=\"\" src=\"http:\/\/rgm3.lab.nig.ac.jp\/RGM-files\/R_CC\/result\/rasterVis\/levelplot-methods.Rd_006_medium.png\" \/><\/a><a href=\"http:\/\/rgm3.lab.nig.ac.jp\/RGM-files\/R_CC\/result\/rasterVis\/levelplot-methods.Rd_007_large.png\"><img decoding=\"async\" alt=\"\" src=\"http:\/\/rgm3.lab.nig.ac.jp\/RGM-files\/R_CC\/result\/rasterVis\/levelplot-methods.Rd_007_medium.png\" \/><\/a><a href=\"http:\/\/rgm3.lab.nig.ac.jp\/RGM-files\/R_CC\/result\/rasterVis\/levelplot-methods.Rd_008_large.png\"><img decoding=\"async\" alt=\"\" src=\"http:\/\/rgm3.lab.nig.ac.jp\/RGM-files\/R_CC\/result\/rasterVis\/levelplot-methods.Rd_008_medium.png\" \/><\/a><a href=\"http:\/\/rgm3.lab.nig.ac.jp\/RGM-files\/R_CC\/result\/rasterVis\/levelplot-methods.Rd_009_large.png\"><img decoding=\"async\" alt=\"\" src=\"http:\/\/rgm3.lab.nig.ac.jp\/RGM-files\/R_CC\/result\/rasterVis\/levelplot-methods.Rd_009_medium.png\" \/><\/a><a href=\"http:\/\/rgm3.lab.nig.ac.jp\/RGM-files\/R_CC\/result\/rasterVis\/levelplot-methods.Rd_010_large.png\"><img decoding=\"async\" alt=\"\" src=\"http:\/\/rgm3.lab.nig.ac.jp\/RGM-files\/R_CC\/result\/rasterVis\/levelplot-methods.Rd_010_medium.png\" \/><\/a><a href=\"http:\/\/rgm3.lab.nig.ac.jp\/RGM-files\/R_CC\/result\/rasterVis\/levelplot-methods.Rd_011_large.png\"><img decoding=\"async\" alt=\"\" src=\"http:\/\/rgm3.lab.nig.ac.jp\/RGM-files\/R_CC\/result\/rasterVis\/levelplot-methods.Rd_011_medium.png\" \/><\/a><a href=\"http:\/\/rgm3.lab.nig.ac.jp\/RGM-files\/R_CC\/result\/rasterVis\/levelplot-methods.Rd_012_large.png\"><img decoding=\"async\" alt=\"\" src=\"http:\/\/rgm3.lab.nig.ac.jp\/RGM-files\/R_CC\/result\/rasterVis\/levelplot-methods.Rd_012_medium.png\" \/><\/a><a href=\"http:\/\/rgm3.lab.nig.ac.jp\/RGM-files\/R_CC\/result\/rasterVis\/levelplot-methods.Rd_013_large.png\"><img decoding=\"async\" alt=\"\" src=\"http:\/\/rgm3.lab.nig.ac.jp\/RGM-files\/R_CC\/result\/rasterVis\/levelplot-methods.Rd_013_medium.png\" \/><\/a><a href=\"http:\/\/rgm3.lab.nig.ac.jp\/RGM-files\/R_CC\/result\/rasterVis\/levelplot-methods.Rd_014_large.png\"><img decoding=\"async\" alt=\"\" src=\"http:\/\/rgm3.lab.nig.ac.jp\/RGM-files\/R_CC\/result\/rasterVis\/levelplot-methods.Rd_014_medium.png\" \/><\/a><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n","protected":false},"excerpt":{"rendered":"<p>Description Level and contour plots of Raster objects with\u00a0lattice\u00a0methods and marginal plots with\u00a0grid\u00a0objects. Usage ## S4 method for signature &#8216;Raster,missing&#8217; levelplot(x, data=NULL, layers, margin=!(any(is.factor(x))), FUN.margin=mean,&hellip; <\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[20],"tags":[],"class_list":["post-536","post","type-post","status-publish","format-standard","hentry","category-r"],"_links":{"self":[{"href":"https:\/\/zhuoyao.net\/index.php\/wp-json\/wp\/v2\/posts\/536","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/zhuoyao.net\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/zhuoyao.net\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/zhuoyao.net\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/zhuoyao.net\/index.php\/wp-json\/wp\/v2\/comments?post=536"}],"version-history":[{"count":0,"href":"https:\/\/zhuoyao.net\/index.php\/wp-json\/wp\/v2\/posts\/536\/revisions"}],"wp:attachment":[{"href":"https:\/\/zhuoyao.net\/index.php\/wp-json\/wp\/v2\/media?parent=536"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zhuoyao.net\/index.php\/wp-json\/wp\/v2\/categories?post=536"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zhuoyao.net\/index.php\/wp-json\/wp\/v2\/tags?post=536"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}