{"id":1835,"date":"2018-07-02T21:11:03","date_gmt":"2018-07-02T19:11:03","guid":{"rendered":"https:\/\/blogs.fu-berlin.de\/reseda\/?page_id=1835"},"modified":"2019-07-23T13:32:10","modified_gmt":"2019-07-23T11:32:10","slug":"visualize-in-r","status":"publish","type":"page","link":"https:\/\/blogs.fu-berlin.de\/reseda\/visualize-in-r\/","title":{"rendered":"Visualize in R"},"content":{"rendered":"<p>Also for this chapter it is a requirement that you own at least one Landsat 8 scene. We will visualize the Landsat 8 Level-2 scene (ID: LC08_L1TP_193023_20170602_20170615_01_T1) in R in this section, which you may have already downloaded in our <a href=\"https:\/\/blogs.fu-berlin.de\/reseda\/earthexplorer-exercise\/\">EarthExplorer download exercise<\/a>. You can download <a href=\"https:\/\/box.fu-berlin.de\/s\/ztHcGQtXMKJbcXN\/download?path=%2FLandsat_Data&amp;files=LC081930232017060201T1-SC20180613160412.tif\" target=\"_blank\" rel=\"noopener\">the preprocessed file here<\/a>.<\/p>\n<p>When you are ready, open R-Studio.<\/p>\n<p>We need the raster package again, so import it into your current working session first:<\/p>\n<pre class=\"theme:amityreseda\">library(raster)\r\n## Loading required package: sp\r\n<\/pre>\n<p>The raster package offers various structures for importing data, depending on how the image data precedes:<\/p>\n<ul>\n<li><span class=\"crayon-inline theme:amityreseda\">layer()<\/span> &#8211; one file, one band<\/li>\n<li><span class=\"crayon-inline theme:amityreseda\">stack()<\/span> &#8211; multiple files, multiple bands<\/li>\n<li><span class=\"crayon-inline theme:amityreseda\">brick()<\/span> &#8211; one file, multiple bands<\/li>\n<\/ul>\n<p>During preprocessing (chapter <a href=\"https:\/\/blogs.fu-berlin.de\/reseda\/landsat-8-preprocessing\/\">Preprocess<\/a>), we saved the individual bands of the Landsat 8 scenes in a single geotiff-container. So we created one file with several bands, so we need the <span class=\"crayon-inline theme:amityreseda\">brick()<\/span> function for an import into R:<\/p>\n<pre class=\"theme:amityreseda\">path &lt;- \"\/media\/sf_exchange\/landsatdata\/LC081930232017060201T1-SC20180613160412.tif\"\r\n\r\nimg &lt;- brick(path)\r\n\r\nimg\r\n## class       : RasterBrick \r\n## dimensions  : 8141, 8051, 65543191, 7  (nrow, ncol, ncell, nlayers)\r\n## resolution  : 30, 30  (x, y)\r\n## extent      : 278085, 519615, 5762085, 6006315  (xmin, xmax, ymin, ymax)\r\n## coord. ref. : +proj=utm +zone=33 +datum=WGS84 +units=m +no_defs +ellps=WGS84 +towgs84=0,0,0 \r\n## data source : \/media\/sf_exchange\/landsatdata\/LC081930232017060201T1-SC20180613160412.tif \r\n## names       : LC0819302\/\/13160412.1, LC0819302\/\/13160412.2, LC0819302\/\/13160412.3, LC0819302\/\/13160412.4, LC0819302\/\/13160412.5, LC0819302\/\/13160412.6, LC0819302\/\/13160412.7 \r\n## min values  :            -3.400e+38,            -2.000e+03,            -1.685e+03,            -1.384e+03,            -2.720e+02,            -1.060e+02,            -6.500e+01 \r\n## max values  :                 13333,                 13643,                 14703,                 14674,                 14331,                 15137,                 15638\r\n<\/pre>\n<p>By calling the object <span class=\"crayon-inline theme:amityreseda\">img<\/span> in line 5, the most important metadata can be viewed, e.g., raster dimensions, geometric resolution, geographical extent, coordinate system, file path, as well as minimum and maximum digital numbers per band. This completes the import of the L8 image as a RasterBrick object into R.<\/p>\n<p>We can now plot the image data. The <span class=\"crayon-inline theme:amityreseda\">plotRGB()<\/span> function is a convenient way to visualize different RGB composites. Check out the help for this feature in R-Studio by running <span class=\"crayon-inline theme:amityreseda\">?plotRGB<\/span>! The function has three arguments called <span class=\"crayon-inline theme:amityreseda\">r =<\/span>, <span class=\"crayon-inline theme:amityreseda\">g =<\/span>, and <span class=\"crayon-inline theme:amityreseda\">b =<\/span>, which define the three layers to be used for the RGB slots in a RasterBrick or Rasterstack.<br \/>\nIf we just want to visualize one band (singleband visualization), we will fill all three slots with the same band, e.g. the green band:<\/p>\n<pre class=\"theme:amityreseda\">plotRGB(img,\r\n        r = 3, g = 3, b = 3,\r\n        stretch = \"lin\"\r\n        )\r\n<\/pre>\n<p><a href=\"https:\/\/blogs.fu-berlin.de\/reseda\/files\/2018\/07\/vis_024.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-1952\" src=\"https:\/\/blogs.fu-berlin.de\/reseda\/files\/2018\/07\/vis_024.png\" alt=\"\" width=\"600\" height=\"955\" \/><\/a><\/p>\n<p>The stretch argument in line 3 was set to &#8220;lin&#8221; in order to perform a minimum-maximum contrast stretching to increase the contrast of the image, as described <a href=\"https:\/\/blogs.fu-berlin.de\/reseda\/visualization\/#3\">here<\/a>.<br \/>\nWe can also do a spatial subsetting here by specifying an &#8220;Extent&#8221;-object using the <span class=\"crayon-inline theme:amityreseda\">ext =<\/span> argument:<\/p>\n<pre class=\"theme:amityreseda\">plotRGB(img,\r\n        r = 3, g = 3, b = 3,\r\n        stretch = \"lin\",\r\n        ext = extent(369800, 400700, 5812400, 5827500)\r\n        )\r\n<\/pre>\n<p><a href=\"https:\/\/blogs.fu-berlin.de\/reseda\/files\/2018\/07\/vis_023.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-1951\" src=\"https:\/\/blogs.fu-berlin.de\/reseda\/files\/2018\/07\/vis_023.png\" alt=\"\" width=\"1344\" height=\"661\" srcset=\"https:\/\/blogs.fu-berlin.de\/reseda\/files\/2018\/07\/vis_023.png 1344w, https:\/\/blogs.fu-berlin.de\/reseda\/files\/2018\/07\/vis_023-300x148.png 300w, https:\/\/blogs.fu-berlin.de\/reseda\/files\/2018\/07\/vis_023-768x378.png 768w, https:\/\/blogs.fu-berlin.de\/reseda\/files\/2018\/07\/vis_023-1024x504.png 1024w, https:\/\/blogs.fu-berlin.de\/reseda\/files\/2018\/07\/vis_023-1200x590.png 1200w\" sizes=\"auto, (max-width: 709px) 85vw, (max-width: 909px) 67vw, (max-width: 1362px) 62vw, 840px\" \/><\/a><\/p>\n<p>Objects of class &#8220;Extent&#8221; are used to define the spatial extent. The four specified arguments are UTM coordinates for (min x, max x, min y, max y). You can set those UTM coordinates arbitrarily and get them, for example, by reading them from QGIS or Google Maps.<\/p>\n<p>By defining different bands for the RGB slots, we can, just as in QGIS, create all imaginable RGB composites:<\/p>\n<pre class=\"theme:amityreseda\">plotRGB(img,\r\n        r = 4, g = 3, b = 2,\r\n        stretch = \"lin\",\r\n        ext = extent(369800, 400700, 5812400, 5827500)\r\n        )\r\n<\/pre>\n<p><a href=\"https:\/\/blogs.fu-berlin.de\/reseda\/files\/2018\/07\/vis_022.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-1940\" src=\"https:\/\/blogs.fu-berlin.de\/reseda\/files\/2018\/07\/vis_022.png\" alt=\"\" width=\"1344\" height=\"960\" \/><\/a><\/p>\n<p>Make your spatial subset permament by writing the trimmed image as a new file to your hard drive with <span class=\"crayon-inline theme:amityreseda\">writeRaster<\/span>. This is especially useful if you want to test a classification workflow: small data sets will cheer up your work in QGIS, in R, as well as in all other software solutions.<\/p>\n<pre class=\"theme:amityreseda\">img.subset &lt;- crop(img, extent(369800, 400700, 5812400, 5827500) )\r\n\r\nwriteRaster(img.subset,\r\n            filename = \"\/media\/sf_exchange\/landsatdata\/LC081930232017060201T1-SC20180613160412_subset.tif\",\r\n            format = \"GTiff\",\r\n            overwrite = TRUE\r\n            )\r\n<\/pre>\n<p>Of course, it is also possible to examine the underlying data distribution in more detail. Therefore, we can look at a histogram of a specific band with the function <span class=\"crayon-inline theme:amityreseda\">hist()<\/span>:<\/p>\n<pre class=\"theme:amityreseda\">green &lt;- img.subset[[3]]\r\n\r\nhist(green,\r\n     breaks = 200,\r\n     xlim = c(0, 2000),\r\n     ylim = c(0, 120000),\r\n     xlab = \"band 3 reflectance value [DN * 0.01]\",\r\n     ylab = \"frequency\",\r\n     main = \"histogram L8 band 3 (green)\"\r\n     )\r\n<\/pre>\n<p><a href=\"https:\/\/blogs.fu-berlin.de\/reseda\/files\/2018\/07\/vis_026.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-1957\" src=\"https:\/\/blogs.fu-berlin.de\/reseda\/files\/2018\/07\/vis_026.png\" alt=\"\" width=\"600\" height=\"852\" \/><\/a><\/p>\n<p>The <span class=\"crayon-inline theme:amityreseda\">breaks<\/span>argument in line is assigned with a single number giving the number of cells for the histogram. So with more breaks, the bars in the histogram become narrower. With <span class=\"crayon-inline theme:amityreseda\">xlim =<\/span>and <span class=\"crayon-inline theme:amityreseda\">ylim =<\/span>you can narrow the x axis and y axis to a certain range.<\/p>\n<p>&nbsp;<\/p>\n<hr style=\"height: 4px;background-color: #6b9e1f\" \/>\n<div style=\"font-family: 'Noto Sans', sans-serif;line-height: 1.2;text-align: right\"><span style=\"font-size: 12px;color: #bfbfbf\"><strong><em>NEXT<\/em><\/strong><\/span><br \/>\n<a style=\"text-decoration: none\" href=\"https:\/\/blogs.fu-berlin.de\/reseda\/analyse\/\"><span style=\"font-size: 30px;color: #6b9e1f\"><strong><em>ANALYSE<\/em><\/strong><\/span><\/a><\/div>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Also for this chapter it is a requirement that you own at least one Landsat 8 scene. We will visualize the Landsat 8 Level-2 scene (ID: LC08_L1TP_193023_20170602_20170615_01_T1) in R in this section, which you may have already downloaded in our EarthExplorer download exercise. You can download the preprocessed file here. When you are ready, open &hellip; <a href=\"https:\/\/blogs.fu-berlin.de\/reseda\/visualize-in-r\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Visualize in R&#8221;<\/span><\/a><\/p>\n","protected":false},"author":3237,"featured_media":0,"parent":0,"menu_order":0,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-1835","page","type-page","status-publish","hentry"],"_links":{"self":[{"href":"https:\/\/blogs.fu-berlin.de\/reseda\/wp-json\/wp\/v2\/pages\/1835","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blogs.fu-berlin.de\/reseda\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/blogs.fu-berlin.de\/reseda\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/blogs.fu-berlin.de\/reseda\/wp-json\/wp\/v2\/users\/3237"}],"replies":[{"embeddable":true,"href":"https:\/\/blogs.fu-berlin.de\/reseda\/wp-json\/wp\/v2\/comments?post=1835"}],"version-history":[{"count":20,"href":"https:\/\/blogs.fu-berlin.de\/reseda\/wp-json\/wp\/v2\/pages\/1835\/revisions"}],"predecessor-version":[{"id":2950,"href":"https:\/\/blogs.fu-berlin.de\/reseda\/wp-json\/wp\/v2\/pages\/1835\/revisions\/2950"}],"wp:attachment":[{"href":"https:\/\/blogs.fu-berlin.de\/reseda\/wp-json\/wp\/v2\/media?parent=1835"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}