Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

after par(fig), text in margins is not written

Tags:

plot

r

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?

like image 343
Berry Boessenkool Avatar asked Feb 22 '17 15:02

Berry Boessenkool


1 Answers

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.

like image 137
Nick Kennedy Avatar answered Sep 20 '22 18:09

Nick Kennedy