Quantcast

Specify color for "zero" raster values using levelplot

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
4 messages Options
Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Specify color for "zero" raster values using levelplot

R-sig-geo mailing list
Dear all,

I am trying to change the color for zero values in a map produced using levelplot to plot a raster file. Specifically, I want to reproduce this figure: http://i.imgur.com/mjXxZhO.png, where a red to blue scale is used, but notice that zero values have been replaced by grey.

As an example, let's use an adapted version of the August irradiation code from the rasterVis webpage:

################################
library(raster)
library(ncdf4)
library(rasterVis)

##Solar irradiation data from CMSAF
old <- setwd(tempdir())
download.file('https://raw.github.com/oscarperpinan/spacetime-vis/master/data/SISmm2008_CMSAF.zip',
'SISmm2008_CMSAF.zip', method='wget')
unzip('SISmm2008_CMSAF.zip')

listFich <- dir(pattern='\\.nc')
stackSIS <- stack(listFich)
stackSIS <- stackSIS * 24 ##from irradiance (W/m2) to irradiation Wh/m2
idx <- seq(as.Date('2008-01-15'), as.Date('2008-12-15'), 'month')

SISmm <- setZ(stackSIS, idx)
names(SISmm) <- month.abb

setwd(old)

# Set color palette
myTheme=rasterTheme(region=brewer.pal('RdBu', n=11))

Aug <- raster(SISmm, 8)
meanAug <- cellStats(Aug, mean)
levelplot(Aug - meanAug, par.settings = myTheme, margin=FALSE)

################################

In the example above, how can I replace the color of "zero values" with grey?
 
Thanks,
 -- Thiago V. dos Santos

PhD student
Land and Atmospheric Science
University of Minnesota

_______________________________________________
R-sig-Geo mailing list
[hidden email]
https://stat.ethz.ch/mailman/listinfo/r-sig-geo
Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Specify color for "zero" raster values using levelplot

Bacou, Melanie

Simply use the same palette as in the example:

library(lattice)

myPal <- read.table(sep="\t", text="
155     29     32    Brown
238     36     37    Tomato
238     77     34    Tomato
250    143     35    Dark orange
254    215     24    Gold
214    223     47    Bitter lemon
188    218    112    Sulu
179    179    179    Dark gray
124    199    177    Keppel
105    202    229    Viking
72     156    211    Curious blue
69     101    173    Chetwode blue
58     86     164    Governor bay
58     72     155    Dark slate blue
44     46     118    Blue bell")

myPal <- colorRampPalette(rgb(myPal[, 1:3], names=as.character(myPal$V4), maxColorValue=255))

x <- seq(pi/4, 5 * pi, length.out = 100)
y <- seq(pi/4, 5 * pi, length.out = 100)
r <- as.vector(sqrt(outer(x^2, y^2, "+")))
grid <- expand.grid(x=x, y=y)
grid$z <- cos(r^2) * exp(-r/(pi^3))
levelplot(z~x*y, grid, cuts=14, col.regions=myPal(15), margin=FALSE)

--Mel.

On 05/17/2017 01:25 PM, Thiago V. dos Santos via R-sig-Geo wrote:
Dear all,

I am trying to change the color for zero values in a map produced using levelplot to plot a raster file. Specifically, I want to reproduce this figure: http://i.imgur.com/mjXxZhO.png, where a red to blue scale is used, but notice that zero values have been replaced by grey.

As an example, let's use an adapted version of the August irradiation code from the rasterVis webpage:

################################
library(raster)
library(ncdf4)
library(rasterVis)

##Solar irradiation data from CMSAF 
old <- setwd(tempdir())
download.file('https://raw.github.com/oscarperpinan/spacetime-vis/master/data/SISmm2008_CMSAF.zip',
'SISmm2008_CMSAF.zip', method='wget')
unzip('SISmm2008_CMSAF.zip')

listFich <- dir(pattern='\\.nc')
stackSIS <- stack(listFich)
stackSIS <- stackSIS * 24 ##from irradiance (W/m2) to irradiation Wh/m2
idx <- seq(as.Date('2008-01-15'), as.Date('2008-12-15'), 'month')

SISmm <- setZ(stackSIS, idx)
names(SISmm) <- month.abb

setwd(old)

# Set color palette
myTheme=rasterTheme(region=brewer.pal('RdBu', n=11))

Aug <- raster(SISmm, 8)
meanAug <- cellStats(Aug, mean)
levelplot(Aug - meanAug, par.settings = myTheme, margin=FALSE)

################################

In the example above, how can I replace the color of "zero values" with grey?
 
Thanks,
 -- Thiago V. dos Santos

PhD student
Land and Atmospheric Science
University of Minnesota

_______________________________________________
R-sig-Geo mailing list
[hidden email]
https://stat.ethz.ch/mailman/listinfo/r-sig-geo


_______________________________________________
R-sig-Geo mailing list
[hidden email]
https://stat.ethz.ch/mailman/listinfo/r-sig-geo
Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Specify color for "zero" raster values using levelplot

R-sig-geo mailing list
Hi Mel,
Thank you ver much for the suggestion. I've reproduced your palette, which looks like exactly what I was looking for. However, the grey color is still not associated to zero values in the map.
After plotting my raster with your palette as an argument:
levelplot(annual.mask, cuts=14, col.regions=myPal(15), margin=F)

I ended up with a figure like this: http://i.imgur.com/aqZkCGZ.png, where I was hoping to have all white (i.e. zero values) in the map filled with grey.
Any other ideas to achieve that?
This is the file and code to reproduce the figure:
https://dl.dropboxusercontent.com/content_link/jnv58wx0QN8ObKaiqigVckXtDmanOVYlgBxXoiym4oty5MS93xHolItNZ8tJ5gVF/file?dl=1
########################library(rasterVis)
myPal <- read.table(sep=",", text="155,29,32,Brown238,36,37,Tomato238,77,34,Tomato250,143,35,Dark.orange254,215,24,Gold214,223,47,Bitter.lemon188,218,112,Sulu179,179,179,Dark.gray124,199,177,Keppel105,202,229,Viking72,156,211,Curious.blue69,101,173,Chetwode.blue58,86,164,Governor.bay58,72,155,Dark.slate.blue44,46,118,Blue.bell")
myPal <- colorRampPalette(rgb(myPal[, 1:3], names=as.character(myPal$V4), maxColorValue=255))
r.annual <- raster("Desktop/r.annual.tif")levelplot(r.annual, cuts=14, col.regions=myPal(15), margin=F)######################## Greetings, -- Thiago V. dos Santos
PhD studentLand and Atmospheric ScienceUniversity of Minnesota

    On Thursday, May 18, 2017 3:06 AM, Melanie Bacou <[hidden email]> wrote:
 

  Simply use the same palette as in the example: library(lattice)
 
 myPal <- read.table(sep="\t", text="
 155     29     32    Brown
 238     36     37    Tomato
 238     77     34    Tomato
 250    143     35    Dark orange
 254    215     24    Gold
 214    223     47    Bitter lemon
 188    218    112    Sulu
 179    179    179    Dark gray
 124    199    177    Keppel
 105    202    229    Viking
 72     156    211    Curious blue
 69     101    173    Chetwode blue
 58     86     164    Governor bay
 58     72     155    Dark slate blue
 44     46     118    Blue bell")
 
 myPal <- colorRampPalette(rgb(myPal[, 1:3], names=as.character(myPal$V4), maxColorValue=255))
 
 x <- seq(pi/4, 5 * pi, length.out = 100)
 y <- seq(pi/4, 5 * pi, length.out = 100)
 r <- as.vector(sqrt(outer(x^2, y^2, "+")))
 grid <- expand.grid(x=x, y=y)
 grid$z <- cos(r^2) * exp(-r/(pi^3))
 levelplot(z~x*y, grid, cuts=14, col.regions=myPal(15), margin=FALSE)
 
 --Mel.
 
 On 05/17/2017 01:25 PM, Thiago V. dos Santos via R-sig-Geo wrote:
 
 Dear all,

I am trying to change the color for zero values in a map produced using levelplot to plot a raster file. Specifically, I want to reproduce this figure: http://i.imgur.com/mjXxZhO.png, where a red to blue scale is used, but notice that zero values have been replaced by grey.

As an example, let's use an adapted version of the August irradiation code from the rasterVis webpage:

################################
library(raster)
library(ncdf4)
library(rasterVis)

##Solar irradiation data from CMSAF
old <- setwd(tempdir())
download.file('https://raw.github.com/oscarperpinan/spacetime-vis/master/data/SISmm2008_CMSAF.zip',
'SISmm2008_CMSAF.zip', method='wget')
unzip('SISmm2008_CMSAF.zip')

listFich <- dir(pattern='\\.nc')
stackSIS <- stack(listFich)
stackSIS <- stackSIS * 24 ##from irradiance (W/m2) to irradiation Wh/m2
idx <- seq(as.Date('2008-01-15'), as.Date('2008-12-15'), 'month')

SISmm <- setZ(stackSIS, idx)
names(SISmm) <- month.abb

setwd(old)

# Set color palette
myTheme=rasterTheme(region=brewer.pal('RdBu', n=11))

Aug <- raster(SISmm, 8)
meanAug <- cellStats(Aug, mean)
levelplot(Aug - meanAug, par.settings = myTheme, margin=FALSE)

################################

In the example above, how can I replace the color of "zero values" with grey?

Thanks,
 -- Thiago V. dos Santos

PhD student
Land and Atmospheric Science
University of Minnesota

_______________________________________________
R-sig-Geo mailing list
[hidden email]
https://stat.ethz.ch/mailman/listinfo/r-sig-geo




   
        [[alternative HTML version deleted]]

_______________________________________________
R-sig-Geo mailing list
[hidden email]
https://stat.ethz.ch/mailman/listinfo/r-sig-geo
Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Specify color for "zero" raster values using levelplot

Bacou, Melanie

You might need to try different values for the number of legend breaks depending on your dataset (e.g. `cuts=13, col.regions=myPal(14)`) and/or use argument `at=seq(-1, 1, .1)` to manually define breaks. Refer to ?levelplot() documentation.


On 05/18/2017 12:27 PM, Thiago V. dos Santos wrote:
Hi Mel,

Thank you ver much for the suggestion. I've reproduced your palette, which looks like exactly what I was looking for. However, the grey color is still not associated to zero values in the map.

After plotting my raster with your palette as an argument:

levelplot(annual.mask, cuts=14, col.regions=myPal(15), margin=F)

I ended up with a figure like this: http://i.imgur.com/aqZkCGZ.png, where I was hoping to have all white (i.e. zero values) in the map filled with grey.

Any other ideas to achieve that?

This is the file and code to reproduce the figure:

########################
library(rasterVis)

myPal <- read.table(sep=",", text="
155,29,32,Brown
238,36,37,Tomato
238,77,34,Tomato
250,143,35,Dark.orange
254,215,24,Gold
214,223,47,Bitter.lemon
188,218,112,Sulu
179,179,179,Dark.gray
124,199,177,Keppel
105,202,229,Viking
72,156,211,Curious.blue
69,101,173,Chetwode.blue
58,86,164,Governor.bay
58,72,155,Dark.slate.blue
44,46,118,Blue.bell")

myPal <- colorRampPalette(rgb(myPal[, 1:3], names=as.character(myPal$V4), maxColorValue=255))

r.annual <- raster("Desktop/r.annual.tif")
levelplot(r.annual, cuts=14, col.regions=myPal(15), margin=F)
########################
 
Greetings,
 -- Thiago V. dos Santos

PhD student
Land and Atmospheric Science
University of Minnesota


On Thursday, May 18, 2017 3:06 AM, Melanie Bacou [hidden email] wrote:


Simply use the same palette as in the example:
library(lattice)

myPal <- read.table(sep="\t", text="
155     29     32    Brown
238     36     37    Tomato
238     77     34    Tomato
250    143     35    Dark orange
254    215     24    Gold
214    223     47    Bitter lemon
188    218    112    Sulu
179    179    179    Dark gray
124    199    177    Keppel
105    202    229    Viking
72     156    211    Curious blue
69     101    173    Chetwode blue
58     86     164    Governor bay
58     72     155    Dark slate blue
44     46     118    Blue bell")

myPal <- colorRampPalette(rgb(myPal[, 1:3], names=as.character(myPal$V4), maxColorValue=255))

x <- seq(pi/4, 5 * pi, length.out = 100)
y <- seq(pi/4, 5 * pi, length.out = 100)
r <- as.vector(sqrt(outer(x^2, y^2, "+")))
grid <- expand.grid(x=x, y=y)
grid$z <- cos(r^2) * exp(-r/(pi^3))
levelplot(z~x*y, grid, cuts=14, col.regions=myPal(15), margin=FALSE)

--Mel.

On 05/17/2017 01:25 PM, Thiago V. dos Santos via R-sig-Geo wrote:
Dear all,

I am trying to change the color for zero values in a map produced using levelplot to plot a raster file. Specifically, I want to reproduce this figure: http://i.imgur.com/mjXxZhO.png, where a red to blue scale is used, but notice that zero values have been replaced by grey.

As an example, let's use an adapted version of the August irradiation code from the rasterVis webpage:

################################
library(raster)
library(ncdf4)
library(rasterVis)

##Solar irradiation data from CMSAF 
old <- setwd(tempdir())
download.file('https://raw.github.com/oscarperpinan/spacetime-vis/master/data/SISmm2008_CMSAF.zip',
'SISmm2008_CMSAF.zip', method='wget')
unzip('SISmm2008_CMSAF.zip')

listFich <- dir(pattern='\\.nc')
stackSIS <- stack(listFich)
stackSIS <- stackSIS * 24 ##from irradiance (W/m2) to irradiation Wh/m2
idx <- seq(as.Date('2008-01-15'), as.Date('2008-12-15'), 'month')

SISmm <- setZ(stackSIS, idx)
names(SISmm) <- month.abb

setwd(old)

# Set color palette
myTheme=rasterTheme(region=brewer.pal('RdBu', n=11))

Aug <- raster(SISmm, 8)
meanAug <- cellStats(Aug, mean)
levelplot(Aug - meanAug, par.settings = myTheme, margin=FALSE)

################################

In the example above, how can I replace the color of "zero values" with grey?
 
Thanks,
 -- Thiago V. dos Santos

PhD student
Land and Atmospheric Science
University of Minnesota

_______________________________________________
R-sig-Geo mailing list
[hidden email]
https://stat.ethz.ch/mailman/listinfo/r-sig-geo





_______________________________________________
R-sig-Geo mailing list
[hidden email]
https://stat.ethz.ch/mailman/listinfo/r-sig-geo
Loading...