I would like to export a terra
plot to png and I need the exported figure to be almost perfectly clipped around the rast
er that is being plotted and the related legend.
However, if I run a default export with png()
, the resulting figure has wide margins. At the same time, I do not see how I could calculate the useful aspect ratio to avoid such extra margins (as in this approach), considering that the relevant area on which the aspect ratio would have to be calculated includes also the legend, so that it cannot be straightforwardly derived from the aspect ratio of the raster.
Notice that I am using terra
development version 1.8-36. Please find attached a minimal working example and the related figure, where hand-made arrow highlights the excessive left margin.
library(terra)
r <- rast(system.file("ex/elev.tif", package="terra"))
x <- scale_linear(r, 0, 30)
bks <- c(0,0.4,0.8,1.3,2,4,8,15,30)
colors <- c('#3182BD', '#9ECAE1', '#DEEBF7', '#FEE5D9', '#FCAE91',
'#FB6A4A', '#DE2D26', '#A50F15')
png("file_name.png", type="cairo-png")
plot(x, col=colors, box=F, axes=F, breaks=bks, reverse=TRUE,
plg=list(
title = "Title",
title.cex = 1.2,
cex = 1.2
))
dev.off()
There are two considerations: (1) remove the whitespace around the map with arguments "mar" and "buffer", and perhaps with trim(x)
; (2) match the relative dimensions of png to that of the plot.
Minimal example data
library(terra)
#terra 1.8.38
r <- rast(system.file("ex/elev.tif", package="terra"))
background="light gray"
so that you can see where the map area begins and ends. I moved the legend inside the map area to save space. If you want to keep it where it is, you could use, e.g., mar=c(0,0,0,4)
. Do not use "asp=NA" because then you distort your map.plot(r, mar=c(0,0,0,4), buffer=FALSE, breaks=5, box=F, axes=F, background="light gray",
plg=list(x=6.25, y=50.18, title="Title", title.cex=1.5))
If the raster data have outer rows and columns with missing values, you can remove these with
r <- trim(r)
w <- 600
png("plot.png", width=w, height=w*1.45, pointsize = 24)
plot(r, mar=c(0,0,0,0), buffer=FALSE, breaks=5, box=F, axes=F, background="light gray",
plg=list(x=6.25, y=50.18, title="Title", title.cex=1.5))
dev.off()
To keep the legend outside the map:
w <- 600
png("plot.png", width=w, height=w*1.15, pointsize = 24)
par(bg="light blue") # for illustration purposes
plot(r, mar=c(0,0,0,5), buffer=FALSE, breaks=5, box=F, axes=F, background="light gray",
plg=list(title="Title", title.cex=1.5))
dev.off()