Increasing the Size of Labels for Axis, Legend, and Title in Terra Plots with Customizable Parameters

Understanding Raster Labeling with Terra

Introduction to Terra and Raster Data

Terra is a popular R package used for geospatial data analysis. It provides an interface to various raster data formats, including GeoTIFF, NetCDF, and others. Raster data represents a 2D grid of values that can represent different types of data such as elevation, temperature, or land cover.

In this article, we will explore how to increase the size of labels for axis, legend, and title in a Terra plot using various parameters available in the plot() function.

Basic Plotting with Terra

Before diving into customizing the labels, let’s start by creating a basic plot using Terra.

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()

This code creates three random raster datasets and plots them using Terra.

Customizing Plot Labels

To increase the size of labels for axis, legend, and title, we need to use various parameters available in the plot() function. In this section, we’ll explore these parameters in detail.

main Parameter

The main parameter is used to set the main title of the plot. We can pass a character string or a list of strings using the c() function.

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

In this example, we set the main title to a list of strings.

plg Parameter

The plg parameter is used to customize the legend parameters. We can pass a list of parameters using the list() function.

terra::plot(rr,
            type = "continuous",
            range = c(0,1),
            nr = 3,
            main = list("r1","r2","r3"),
            plg = list(
                title = "Legend title", # Set legend title
                title.cex = 2, # Legend title size
                cex = 2 # Legend text size
            ),
            ...
)

In this example, we set the legend title to “Legend title” and increase its size using title.cex. We also set the legend text size using cex.

pax Parameter

The pax parameter is used to customize the axis parameters. We can pass a list of parameters using the list() function.

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

In this example, we increase the axis text size using cex.axis.

cex.main Parameter

The cex.main parameter is used to set the main title size. We can pass a single value or a list of values.

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

In this example, we set the main title size using cex.main.

Additional Parameters

There are several additional parameters available in the plot() function that can be used to customize the plot further. Some of these include:

  • add : Adds a new layer to the plot.
  • col : Specifies the color palette for the plot.
  • border : Sets the border width and style.
terra::plot(rr,
            type = "continuous",
            range = c(0,1),
            nr = 3,
            main = list("r1","r2","r3"),
            plg = list(
                title = "Legend title", 
                title.cex = 2, 
                cex = 2
            ),
            pax = list(
                cex.axis = 2 
            ), 
            cex.main = 2,
            add = TRUE, # Add a new layer to the plot
            col = "black", # Specify the color palette for the plot
            border = 1 # Set the border width and style
)

Conclusion

In this article, we explored how to increase the size of labels for axis, legend, and title using various parameters available in the plot() function. We also examined additional parameters that can be used to customize the plot further.

By following these steps, you can create high-quality plots with customized labels and titles.

Example Use Case

Here is an example use case:

# Load required libraries
library(terra)

# Create three random raster datasets
r1 <- raster(100:200, 100:200)
r2 <- raster(300:400, 300:400)
r3 <- raster(500:600, 500:600)

# Set values for the rasters
values(r1) <- runif(ncell(r1))
values(r2) <- runif(ncell(r2))
values(r3) <- runif(ncell(r3))

# Plot the three rasters using Terra
rr <- stack(r1, r2, r3)
terra::plot(rr,
            type = "continuous",
            range = c(0,1),
            nr = 3,
            main = list("r1","r2","r3"),
            plg = list(
                title = "Legend title", 
                title.cex = 2, 
                cex = 2
            ),
            pax = list(
                cex.axis = 2 
            ), 
            cex.main = 2,
            add = TRUE,
            col = "black",
            border = 1
)

This code creates three random raster datasets and plots them using Terra.


Last modified on 2024-05-26