Reputation: 1538
After setting graphical parameters with par(fig)
and resetting them with the original parameters, text in the margins of plots is not written.
Only after another low level command inside the plotting region is performed will it work again. Here's an example:
dev.off()
plot(1:10)
op <- par(no.readonly = TRUE)
mtext("hello", adj=1, col=2) # written as expected
par(fig=c(0.1,0.6,0.5,0.8), new=TRUE)
par(op)
mtext("hello ", adj=1, col=3) # not written
mtext("hello ", adj=1, col=3, line=-1) # works inside plot region
mtext("hello ", adj=1, col=3) # still not written
text(50,20,"") # or abline # do something inside plot region
mtext("hello ", adj=1, col=3) # now it works!
This may be related to another question I posted under after par(fig), mtext is slightly off.
Besides mtext
, axis
also does not work. Besides text/abline/points
, title(main="dummy")
also solves the problem.
Could this be an R bug? Or am I missing something?
Upvotes: 5
Views: 317
Reputation: 12640
By trial and error, it comes down to par(mfg=c(1, 1, 1, 1))
.
plot(1:10)
op <- par(no.readonly = TRUE)
mtext("hello", adj=1, col=2) # written as expected
par(op[names(op) == "mfg"])
mtext("bye ", adj=1, col=3) # not written
mtext("hello ", adj=1, col=3, line=-1) # works inside plot region
plot(1:10)
op <- par(no.readonly = TRUE)
mtext("hello", adj=1, col=2) # written as expected
par(op[names(op) != "mfg"])
mtext("bye ", adj=1, col=3) # written as expected
mtext("hello ", adj=1, col=3, line=-1) # works inside plot region
It's not clear to me why setting the figure to be plotted next should disable printing text in the margin (but not in the figure), and since mtext
is implemented in C, it would take some effort to work it out.
Upvotes: 5