How to change labels inside a density plot to a legend on the side?

254 views Asked by At

I have a RasterStack with several 400MB raster files and I want to compare them among each other with the rasterVIS density function. The problem is that the labels of the plot overlap each other. Plot looks similar to this... Plot looks similar to this

So is it possible to change the labels inside a rasterVis density plot to a legend on the side?

I tried things like

library(raster)
library(rasterVis)

Rasterstack = stack(Rasterfile1,Rasterfile2,Rasterfile3,Rasterfile4)
labels = names(RasterStack)

densityplot(RasterStack, xlab="density", ylab="value", 
             auto.key= list(space="right", lines=TRUE, text=labels))
#or just#
densityplot(RasterStack, xlab="density", ylab="value", auto.key=T)

I also tried the solution mentioned here, but it is not functional for me because it generates vector sizes which are not processible. Any ideas?

1

There are 1 answers

1
Kilian On

Found the solution by adapting the RasterVis Source Code of the density plot function.

library(raster)
library(rasterVis)

#Adapted density plot function#

dp <- function(x, data = NULL, layers, FUN, maxpixels = 1e+05,
                   xlab = '', ylab = '', main = '',
                   par.settings = rasterTheme(), auto.key = list(columns = 4, space = "top"), ...) {
      
      if (!missing(layers)) x <- subset(x, layers)
      nl <- nlayers(x)
                
      if (nl > 1) {
        dat <- raster2dat(x, FUN, maxpixels)
        p <- densityplot(~values,
                         data = dat, groups = ind,
                         breaks = 100,
                         par.settings = par.settings, pch = '.',
                         xlab = xlab, ylab = ylab, main = main, auto.key = auto.key, 
                         panel = panel.superpose,
                         panel.groups = function(x, group.value, col.line, ...) {
                           panel.densityplot(x, col.line = col.line,
                                             plot.points = FALSE, ...)
                           # d <- density(x, na.rm = TRUE)
                           # i <- which.max(d$y)
                           # ltext(d$x[i],d$y[i],
                           #       group.value,
                           #       adj = c(0.3,0),
                           #       col = col.line,
                           #       cex = 0.7)
                          })
      } else {
        p <- densityplot(x, maxpixels = maxpixels,
                         main = main, xlab=xlab, ylab=ylab, ...)
      }
      p
    }


 #raster2dat function from the RaserVis package#

    raster2dat <- function(x, FUN, maxpixels){
      nl <- nlayers(x)
      if (maxpixels < ncell(x)) {
        dat <- sampleRandom(x, maxpixels)
      } else {
        dat <- getValues(x)
      }
      if (nl>1){
        dat <- as.data.frame(dat)
        ##http://r.789695.n4.nabble.com/Column-order-in-stacking-unstacking-td3349953.html
        idx <- sprintf("%s%03d", "X", 1:nl) 
        names(dat) <- idx
        dat <- stack(dat)
        z <- getZ(x)
        if (!missing(FUN) & !is.null(z)){
          FUN <- match.fun(FUN)   
          dat$ind <- factor(FUN(z))[dat$ind]
        } else {
          nms <- names(x)
          nms <- reorder(factor(nms), 1:nl)
          dat$ind <- nms[dat$ind]
        }
        dat
      } else {
        dat ##nl==1 --> raster2dat gives a vector 
      }
    }
    
    #Test

    f <- system.file("external/test.grd", package="raster")
    r <- raster(f)
    s <- stack(r, r-500, r+500, r - 120)
    
    dp(s)