I am making a graphic using grid, and want to draw some text. I ran into an issue when specifying the coordinates of the text.
Specifically, I found out that I can use both a straight unit, such as unit(1, "native")
and an expression that evaluates to a unit, such as unit(1, "native") + unit(1, "mm")
. But as soon as I have two native units in the expression, the text is plotted not where I expect it, but in a position which might be using npc units.
In the following MWE, I would expect "3" to be printed to the right of the middle (0 in native units), but it is printed far to the left.
require(grid)
grid.newpage()
vp.data.region <- viewport(x = unit(0.5, "npc"), y = unit(0.5, "npc"), width = unit(0.8, "npc"), height = unit(0.8, "npc"), xscale=c(-1,1), yscale=c(-1,1), name = "data region")
pushViewport(vp.data.region)
grid.xaxis()
grid.yaxis()
grid.text("1", unit(0, "native"),unit(0, "native")) #places the text where it should be
grid.text("2", unit(0, "native") + unit(10, "mm"), unit(0, "native")) # it is possible to use an expression adding mixed units
grid.text("3", unit(0.6, "native") - unit(0.5, "native") + unit(1, "mm") , unit(0, "native")) # suddenly, it looks like the unit expression is being interpreted as npc?
What causes this problem, and how can I avoid it? Note that it would be very difficult to specify the unit with a single expression, because I start with a point already specified as native units plus some millimeters, and have to position the text a certain distance from it, which I have to again calculate using both native units and millimeters.