Home > Software engineering >  Increase raster label size for axis, legend and title using terra
Increase raster label size for axis, legend and title using terra

Time:07-05

How can I increase the size of labels of legend break, axis and plot title using terra package. The plot created below has such small labels that I can't read them at all.

r1 <- rast(ncol=10, nrow=10, xmin=-150, xmax=-80, ymin=20, ymax=60)
r2 <- rast(ncol=10, nrow=10, xmin=-150, xmax=-80, ymin=20, ymax=60)
r3 <- rast(ncol=10, nrow=10, xmin=-150, xmax=-80, ymin=20, ymax=60)

values(r1) <- runif(ncell(r1))
values(r2) <- runif(ncell(r2))
values(r3) <- runif(ncell(r3))

rr <- c(r1, r2, r3)

png(file.path(dir_ls$input, '250k', 'output', paste0(year_ref,'_map.png')),
    width = 1000, height = 800)

terra::plot(rr,
            type = "continuous",
            range = c(0,1),
            nr = 3,
            main = c("r1","r2","r3"))
dev.off()

CodePudding user response:

Like this:

terra::plot(rr,
            type = "continuous",
            range = c(0,1),
            nr = 3,
            main = list("r1","r2","r3"),
            plg=list( # parameters for drawing legend
                title = "Legend title",
                title.cex = 2, # Legend title size
                cex = 2 # Legend text size
            ),
            pax=list( # parameters for drawing axes
                cex.axis = 2 # Axis text size 
            ), 
            cex.main = 2 # Title text size
            )

It took a bit of guess work on my part to work out where to put cex.main as it does not seem to be documented but there is a list of other parameters in the docs.

  • Related