Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use colortable from the slot "legend" in levelplot #26

Open
oscarperpinan opened this issue Sep 6, 2015 · 2 comments
Open

Use colortable from the slot "legend" in levelplot #26

oscarperpinan opened this issue Sep 6, 2015 · 2 comments

Comments

@oscarperpinan
Copy link
Owner

Already implemented in plot3d.

A simple solution was proposed here

library(rasterVis) 
f <- system.file("external/test.grd", package="raster") 
r <- round(raster(f)) 

## assign a color table 
## if the original raster (on disk) had a color table it should already be in this slot 
r@legend@colortable=rainbow(cellStats(r,max)) 
str(r) 

## use the color table instead. Could write a simple wrapper function for this... 
## this might not work for all color tables.... 
cols=r@legend@colortable 
levelplot(r,col.regions=cols,at=0:length(cols)) 

@MatthieuStigler
Copy link

Any updates on this enhancement?

It seems there is an additional issue if the argument to col.regions is factor, in which case values get re-ordered according to levels, and hence colours are confused? See code below.

Also, the NA value is the colourtable could be tricky: it seems to be the first colour in the colourtable, but might not be reported by user in the attribute table?

 library(rasterVis) 
 library(raster) 

 r <- raster(nrows=5, ncols=5) 
 r[] <- rep(c(1:5), each=5)
 cols <- c("blue", "red", "yellow", "black", "green")

 r@legend@colortable <- c("white", cols)
 plot(r)


 df <- data.frame(ID=1:5, class=as.character(letters[1:5]), cols=cols)
is.factor(df$cols)
 ratify(r)
 levels(r) <- df

 plot(r)
levelplot(r, col.regions=df$cols, at=0:5)
levelplot(r, col.regions=as.character(df$cols), at=0:5)

@oscarperpinan
Copy link
Owner Author

Any updates on this enhancement?

No, sorry. Nowadays I have less time than desired for improvements.

It seems there is an additional issue if the argument to col.regions is factor, in which case values get re-ordered according to levels, and hence colours are confused?

This is a common problem with factors: I wouldn't use them for a palette. You can set stringsAsFactors = FALSE in the data.frame call.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants