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?
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With