[R-sig-Geo] Does plotKML handle skewed diverging continuous raster?

Tomislav Hengl hengl at spatial-analyst.net
Sun Sep 14 14:16:54 CEST 2014


You need to classify the data using the 'cut' function e.g.:

library(RColorBrewer)
library(plotKML)
data(eberg_grid)
gridded(eberg_grid) <- ~x+y
proj4string(eberg_grid) <- CRS("+init=epsg:31467")
eberg_grid$r <- eberg_grid$TWISRT6-mean(eberg_grid$TWISRT6)
DivColPalette <- colorRampPalette((brewer.pal(NDivBreaks, "RdBu")),
space="Lab")
DivColorBreaks <- c(min(eberg_grid$r),-1,-0.1,0.1,1,max(eberg_grid$r))
eberg_grid$rc <- cut(eberg_grid$r, breaks=DivColorBreaks, 
include.lowest=TRUE)
summary(eberg_grid$rc)
plotKML(eberg_grid["rc"], 
colour_scale=DivColPalette(length(levels(eberg_grid$rc))))

I've just discovered a small bug in my 'reproject.R' script that 
re-orders the class names, so you will need to get plotKML Version: 
0.4-6 from R-forge to get exactly what you look for:

install.packages("plotKML", repos=c("http://R-Forge.R-project.org"))

HTH,

T. Hengl

On 14-9-2014 11:36, DAlcaraz wrote:
> Hi,
> First of all, thank you very much for your great job with the plotKML
> package for R. It is simply GREAT!!!
> However, I've been fighting during one week with this issue and I wonder
> whether the package still does not handle it.
> How can a plot a kml from a raster whose values show deviations from 0, but
> follow an skewed distribution?
> Ideally, I would like negative values in reds, positive values in blues, and
> zero values in grey.
>
> Thank you very much in advance for your help.
> Domingo
>
> PS: I've pasted below a trivial example showing how the "plot" function can
> handle this issue but the plotKML does not.
>
> install.packages("raster", dep=T)
> install.packages("RColorBrewer", dep=T)
> install.packages("plotKML", dep=T)
> library(raster)
> library(RColorBrewer)
> library(plotKML)
>
> r<- raster(ncol=5,nrow=2)
> values(r) <- c(-5,-4,-1,0,0,3,-2,-6,-6,-6) #Positive and Negative changes
> as.matrix(r)
> hist(r, main="Diverging skewed distribution of raster data")
>
> DivColorBreaks <- c(-6,-3,-0.1,0.1,3,6)#
> NDivBreaks <- length(DivColorBreaks)-1
> DivColPalette <- colorRampPalette((brewer.pal(NDivBreaks, "RdBu")),
> space="Lab")
> DivCols <- DivColPalette(NDivBreaks)
>
> plot(r, useRaster=FALSE,
>       col=  DivCols, breaks=DivColorBreaks)
> #In the plot graph:
> #pixel r1,c3 = -1 is in reds. CORRECT
> #pixels r1,c4&5 = 0 are in grey. CORRECT
>
> kml_open("DivSkewd.kml")
> kml_layer.Raster(r, plot.legend = TRUE, metadata = NULL,
>                   png.width = ncol(r), png.height = nrow(r),
>                   min.png.width = 800,
>                   colour_scale =  DivCols, #the problem must be here?
>                   #Here I would need sth like "breaks=DivColorBreaks"
>                   raster_name="DivSkewd.png")
> kml_close("DivSkewd.kml")
> #In DivSkewd.png:
> #pixel r1,c3 = -1 is in grey. INCORRECT
> #pixels r1,c4&5 = 0 are in blue. INCORRECT
>
> #How could I plot the raster so that it fairly displays
> #which pixels have negative values in red and positive values in blue?
>
>
>
> --
> View this message in context: http://r-sig-geo.2731867.n2.nabble.com/Does-plotKML-handle-skewed-diverging-continuous-raster-tp7587124.html
> Sent from the R-sig-geo mailing list archive at Nabble.com.
>
> _______________________________________________
> R-sig-Geo mailing list
> R-sig-Geo at r-project.org
> https://stat.ethz.ch/mailman/listinfo/r-sig-geo
>



More information about the R-sig-Geo mailing list