colonelforbin97
colonelforbin97

Reputation: 105

What is the default color palette for the raster package in R?

How can I recreate the default color palette for the raster package in R? I cannot find details on the default raster plotting color scheme in online documentation. Thanks!

Upvotes: 4

Views: 1246

Answers (1)

Ian Campbell
Ian Campbell

Reputation: 24790

If we review the source code for the custom plot method for objects of type Raster, we will see that line 12 shows:

col <- rev(terrain.colors(255))

If you type terrain.colors you will see the code used to calculate the colors:

terrain.colors
function (n, alpha = 1, rev = FALSE) 
{
    if ((n <- as.integer(n[1L])) > 0) {
        k <- n%/%2
        h <- c(4/12, 2/12, 0/12)
        s <- c(1, 1, 0)
        v <- c(0.65, 0.9, 0.95)
        cols <- c(hsv(h = seq.int(h[1L], h[2L], length.out = k), 
            s = seq.int(s[1L], s[2L], length.out = k), v = seq.int(v[1L], 
                v[2L], length.out = k), alpha = alpha), hsv(h = seq.int(h[2L], 
            h[3L], length.out = n - k + 1)[-1L], s = seq.int(s[2L], 
            s[3L], length.out = n - k + 1)[-1L], v = seq.int(v[2L], 
            v[3L], length.out = n - k + 1)[-1L], alpha = alpha))
        if (rev) 
            cols <- rev(cols)
        cols
    }
    else character()
}

Upvotes: 5

Related Questions