2

I am generating a chlorophyll map for lake. I want to fill the lake with blue colour where there is a very low chlorophyll concentration and light blue for NA values. I am using a code as given below

gplot(Chlorophyll_map_5) + geom_tile(aes(fill=value)) + scale_fill_gradient(low = 'blue', high = 'red', na.value='blue',name="Chl-a (ug/l)",limits=c(0,1000)) + coord_equal()+theme_bw()

Which gives me a plot like this for na.value='blue':

na.value='blue'

When I use na.value='transparent' I got this image:

na.value='transparent'

If I change the colour of the na.value it also changes the background. Is there a way to fill the lake with colour without changing the background?

The output of my data:`The output of my data:

Formal class 'RasterLayer' [package "raster"] with 12 slots
..@ file    :Formal class '.RasterFile' [package "raster"] with 13 slots
.. .. ..@ name        : chr    "/private/var/folders/68/hm_5ts9x7psb6j3wnb91_bfr0000gn/T/RtmpZ3BLZD/raster/r_tmp_2017-07-18_133827_28365_34843.grd"    
.. .. ..@ datanotation: chr "FLT8S"
.. .. ..@ byteorder   : Named chr "little"
.. .. .. ..- attr(*, "names")= chr "value"
.. .. ..@ nodatavalue : num -1.7e+308
.. .. ..@ NAchanged   : logi FALSE
.. .. ..@ nbands      : int 1
.. .. ..@ bandorder   : Named chr "BIL"
.. .. .. ..- attr(*, "names")= chr "value"
.. .. ..@ offset      : int 0
.. .. ..@ toptobottom : logi TRUE
.. .. ..@ blockrows   : int 0
.. .. ..@ blockcols   : int 0
.. .. ..@ driver      : chr "raster"
.. .. ..@ open        : logi FALSE
..@ data    :Formal class '.SingleLayerData' [package "raster"] with 13 slots
.. .. ..@ values    : logi(0) 
.. .. ..@ offset    : num 0
.. .. ..@ gain      : num 1
.. .. ..@ inmemory  : logi FALSE
.. .. ..@ fromdisk  : logi TRUE
.. .. ..@ isfactor  : logi FALSE
.. .. ..@ attributes: list()
.. .. ..@ haveminmax: logi TRUE
.. .. ..@ min       : num 0.00335
.. .. ..@ max       : num 3870657
.. .. ..@ band      : int 1
.. .. ..@ unit      : chr ""
.. .. ..@ names     : chr "layer"
..@ legend  :Formal class '.RasterLegend' [package "raster"] with 5 slots
.. .. ..@ type      : chr(0) 
.. .. ..@ values    : logi(0) 
.. .. ..@ color     : logi(0) 
.. .. ..@ names     : logi(0) 
.. .. ..@ colortable: logi(0) 
..@ title   : chr(0) 
..@ extent  :Formal class 'Extent' [package "raster"] with 4 slots
.. .. ..@ xmin: num 35.8
.. .. ..@ xmax: num 36.7
.. .. ..@ ymin: num 2.4
.. .. ..@ ymax: num 4.65
..@ rotated : logi FALSE
..@ rotation:Formal class '.Rotation' [package "raster"] with 2 slots
.. .. ..@ geotrans: num(0) 
.. .. ..@ transfun:function ()  
..@ ncols   : int 3240
..@ nrows   : int 8321
..@ crs     :Formal class 'CRS' [package "sp"] with 1 slot
.. .. ..@ projargs: chr "+proj=longlat +ellps=WGS84 +no_defs"
..@ history : list()
..@ z       : list()
D.Banerjee
  • 91
  • 10
  • It isn't changing the background from what I can see. What does your map data look like? You might have NAs over that entire boxed area. – troh Jul 18 '17 at 12:13
  • I have downloaded Landsat 8 OLI/TIRS images and applied regression model in them. How will I get rid of the NA values over the whole boxed area and retain only the lake? I have cropped the image previously with the lake shapefile. – D.Banerjee Jul 18 '17 at 12:30
  • Can you show the output of `str` of your data? – troh Jul 18 '17 at 13:26
  • @troh Please see the edited post with the output. – D.Banerjee Jul 18 '17 at 13:39

1 Answers1

0
x <- trim(Chlorophyll_map_5)
ggplot(Chlorophyll_map_5) + 
geom_tile(aes(fill=value)) + 
scale_fill_gradient(low = 'blue', high = 'red', na.value='blue',name="Chl-a (ug/l)",limits=c(0,1000)) + 
coord_equal()+theme_bw()

Per documentation, the trim function "crops a RasterLayer by removing the outer rows and columns that only contain NA values"

troh
  • 1,354
  • 10
  • 19
  • Sorry it did not work. I am getting the same picture that I have posted. – D.Banerjee Jul 18 '17 at 14:01
  • Maybe specifying the width of the tile will change it? It is difficult to reproduce without a minimal data example. – troh Jul 18 '17 at 14:06
  • I have cropped the image with the shapefile of the lake but still values are showing outside the lake area – D.Banerjee Jul 18 '17 at 14:23
  • I can't go any further without the data. Here's a good tutorial that might help. http://zevross.com/blog/2015/03/30/map-and-analyze-raster-data-in-r/. Also, the satellite photos might be in that square shape. You need to find lake polygon to subset to it. – troh Jul 18 '17 at 17:49
  • I have used the lake polygon only to subset the image. It seems like the code is taking the NA values outside the lake region also. As the Landsat files are very large, I cannot post that here. Anyway, I really appreciate the advice and time you have provided to me. Many thanks. – D.Banerjee Jul 18 '17 at 22:18