Search code examples
rrasterterra

Change axis label position, tick marks and legend in terra plots in R


I am new to using the terra package in R. I am trying to plot a SpatRaster using the package's plot function. However, I would like to change multiple layout characteristics but did not find the needed information online.

For example:

# Example data 
library(terra)
f <- system.file("ex/elev.tif", package="terra")
r <- rast(f)

## Define colours
colfunc <- colorRampPalette(c("#08306b", "#f7fbff"))

# list with details for legend
plg = list(
        title = expression(bold("Depth [m]")),
        title.cex = 0.9,
        cex = 0.7,
        shrink=0
        )

# list with details for axes
pax = list( 
     sides = c(1,2,3,4),
     labels = T
)
   

plot(r, mar=c(3.1, 3.1, 2.1, 7.1), plg=plg, pax=pax, las=1, col=colfunc(20))

This results in the following plot:

enter image description here

Now my question: is it possible to

a) increase the distance between the legend and the axis? b) have tick marks on all 4 axis sides but label the tick marks on 2 sides only? c) create tick marks that contain "longitude and latitude" information, i.e. N and W in it? d) change the position and label of the tick marks?

i.e. something like this: enter image description here

I am aware of the plg = and pax = arguments within the plot() call but I was not able to find a list of all possible arguments within plg and pax as they are seemingly different from the arguments used in the "normal" axis functions.


Solution

  • a) increase the distance between the legend and the axis?

    You can use plg$ext to set the location of a continuous legend (it works a bit different for other legend types and that needs to be improved)

    b) have tick marks on all 4 axis sides but label the tick marks on 2 sides only?

    You can use pax() values tick=1:4, side=1:4, lab=1:2

    c) create tick marks that contain "longitude and latitude" information, i.e. N and W in it?

    You can use pax() argument retro=TRUE (should get a better name) to use a sexagesimal notation.

    For example

    library(terra)
    #terra 1.6.24
    f <- system.file("ex/elev.tif", package="terra")
    r <- rast(f)
    colfunc <- colorRampPalette(c("#08306b", "#f7fbff"))
    
    # list with details for legend
    plg = list(ext=c(6.75, 6.8, 49.5, 50.1),
            title = expression(bold("Depth [m]")),
            title.cex = 0.9, cex = 0.7, shrink=0 )
            
    pax=list(side=2:3, tick=c(1,4), lab=1:2, retro=TRUE)
    plot(r, plg=plg, pax=pax, las=1, col=colfunc(20))
    

    enter image description here

    d) change the position and label of the tick marks?

    You can use pax() arguments xat and yat for the position and xlabs and ylabs for the labels. For example

    pax <- list(xat=c(6,6.4), yat=c(49.5, 49.75, 50), ylabs=c("A", "B", "C"), 
                retro=TRUE, tick=1:4)
    plot(r, plg=plg, pax=pax, las=1, col=colfunc(20))
    

    enter image description here

    The level of control that you were looking for was not available in the CRAN version (in a reasonably easy way), and you effectively requested new features (and they have not been documented yet). The best place to do that is on github, but I understand that it can be hard to know whether something cannot be done (feature request) or whether you just do not know how (ask for help on SO).