dan_confused
dan_confused

Reputation: 75

Changing scale colour to specific number in R

I've uploaded my data onto github here: https://github.com/dschmidt42/Benguela_Chla_Danielle_Schmidt.

To set up my data:

chl_data <- "cmems_mod_glo_bgc_my_0.25_P1M-m_1673881119174.nc"
r <- rast(chl_data)
i <- seq(1, nlyr(r), 3) 
r1 <- r[[i]]
vm <- tapp(r1, "months", var)

Here are the resulting plots:

titles <- c("January", "February", 
            "March", "April", 
            "May", "June", 
            "July", "August", 
            "September", "October",
            "November", "December")
plot(vm, main = titles)

enter image description here

As you can see, the scales are all the same though they have different values. How can I change it so that 4.0 on one graph is not the same as 1.5 on another?

Upvotes: 0

Views: 66

Answers (2)

Robert Hijmans
Robert Hijmans

Reputation: 47146

There really ought to be a simple way to specify that you want that, but for now, a quick solution is

rr <- range(minmax(vm))
plot(vm, range=rr, main=month.name)

A more elaborate solution is

m <- cbind(matrix(1:12, ncol=4, byrow=TRUE), 13)
layout(m, c(1,1,1,1,.3))
for (i in 1:12) {
    plot(vm[[i]], legend=i==8, range=rr, mar=c(1,1,1,1), plg=list(cex=1.2))
    text(20,-17,month.abb[i])
}

And more:

m <- cbind(matrix(1:12, ncol=4, byrow=TRUE), 13)
layout(m, c(1, 1, 1, 1, .5))
par(mgp = c(2, .25, 0))
for (i in 1:12) {
    plot(vm[[i]], legend=i==8, range=rr, mar=c(1.2,1.2,.2,0), plg=list(cex=1.2), axes=FALSE)
    if (i %in% c(1,5,9)) axis(2, las=1, cex.axis=0.7)
    if (i > 8) axis(1, cex.axis=0.7)
    box()
    text(19,-17,month.abb[i])
}

enter image description here

Removing whitespace (while keeping outside axes), and using the log to get more contrast

lvm <- log(vm)
lrr <- range(minmax(lvm))
m <- matrix(1:12, ncol=4, byrow=TRUE)
m <- rbind(m, 14)
m <- cbind(14, m, 13)

layout(m, c(0.2, 1, 1, 1, 1, .5), c(1,1,1,0.1))
par(mgp = c(2, .25, 0))
for (i in 1:12) {
    plot(lvm[[i]], legend=i==8, range=lrr, mar=c(0,0,0,0), plg=list(cex=1.2), axes=FALSE)
    if (i %in% c(1,5,9)) axis(2, las=1, cex.axis=0.7)
    if (i > 8) axis(1, cex.axis=0.7)
    box()
    text(19,-17,month.abb[i])
}

enter image description here

Upvotes: 3

You can try setting the breaks manually and the type of the rasters as continuous.

library(terra)
plot(vm, 
     main = titles,
     type = "continuous",
     breaks = seq(0,4,0.5))

Upvotes: 3

Related Questions