I'm trying to fix the position of some text in a rasterVis::vectorplot
so that it stays in the same position even if I change the width and height of the png file.
I tried using the margin parameters of par
but with no luck.
This is an example of what I got so far:
#Some raster data
proj <- CRS('+proj=longlat +datum=WGS84')
df <- expand.grid(x = seq(-2, 2, .01), y = seq(-2, 2, .01))
df$z <- with(df, (3*x^2 + y)*exp(-x^2-y^2))
r <- rasterFromXYZ(df, crs=proj)
#[A]
png("test01.png",width = 918,height = 850,res=100)
vectorplot(r,par.settings=list(layout.widths = list(axis.key.padding = 3)),
narrows = 500,length=0.1,lwd.arrows=0.4)
grid.text(substr(R.version.string, 1, 15),rot=90, x=0.92,y=0.14,gp = gpar(fontsize = 12, fontface = "italic"))
dev.off()
This is the output of [A]. That's how I want it:
Now, changing the width and height:
##[B]
png("test02.png",width = 1718,height = 850,res=100)
vectorplot(r,
par.settings=list(layout.widths = list(axis.key.padding = 3)),
narrows = 500,length=0.1,lwd.arrows=0.4)
grid.text(substr(R.version.string, 1, 15),rot=90, x=0.92,y=0.14,gp = gpar(fontsize = 12, fontface = "italic"))
dev.off()
This is the output of [B]:
As you can see the text doesn't stay in the same place. (I'm new with the rasterVis library.)
The layer
function of the latticeExtra
package with panel.text
will help you here. The panel.text
functions prints inside the area of the panel, so you have to add the option clip = list(panel = FALSE)
to the settings list in order to print outside this area:
library(grid)
library(rasterVis)
vectorplot(r,par.settings=list(clip = list(panel = FALSE),
layout.widths = list(axis.key.padding = 3)),
narrows = 500,length=0.1,lwd.arrows=0.4) +
layer(panel.text(2.05, -2,
substr(R.version.string, 1, 15),
adj = c(0, 1),
srt = 90))