before considering answer, correct question: , regarding question: “ i'm curious why, though, setting parameters after plotting vice before? “ actually, forgot mentioning something. correct order this:
using data again:
dose <- c(20, 30, 40, 45, 60) druga <- c(16, 20, 27, 40, 60) drugb <- c(15, 18, 25, 31, 40)
1st:run: plot(dose, druga, type="b", pch=19, lty=2, col="red"
(it self checking mechanism. mean proving myself first, generating plot default r settings)(plot_1)
2nd_run: opar.1<-par(pin=c(5, 3), lwd=2, cex=1.5)
3rd_run: plot(dose, druga, type="b", pch=19, lty=2, col="red")
(same plot opar.1 setting, compare first plot , second plot)(plot_2)
4th_run: par(opar.1)
(with intention of resetting opar.1 settings.)
5th_run: plot(dose, druga, type="b", pch=19, lty=2, col="red")
(and seeing third plot same first plot. hence, proves par(opar.1) command reset opar.1 settings , turn default settings.)(plot_3 same plot_1)
but, 5th_ when run opar.1
, 6th_run plot(dose, druga, type="b", pch=19, lty=2, col="red"
, not getting plot same plot_2. in conclusion,i wondering if getting wrong thinking “opar.1<-par(pin=c(5, 3), lwd=2, cex=1.5)
” command store “par(pin=c(5, 3), lwd=2, cex=1.5)
” settings , if use “par(opar.1)
” command after “opar.1<-par(pin=c(5, 3), lwd=2, cex=1.5)
” command , reset opar.1
settings, running “opar.1” can load “par(pin=c(5, 3), lwd=2, cex=1.5)
” settings?
there 2 mechanisms come mind, , though starting in right direction, you're missing final point(s). i'm curious why, though, setting par
ameters after plotting vice before? useful, typically need set them before calling plot
.
using data:
dose <- c(20, 30, 40, 45, 60) druga <- c(16, 20, 27, 40, 60) drugb <- c(15, 18, 25, 31, 40)
the first mechanism not require additional packages:
opar.1 <- par(pin = c(5, 3), lwd = 2, cex = 1.5) plot(dose, druga, type = "b", pch = 19, lty = 2, col = "red") par(opar.1)
one risk of doing way that, before restorative call
par
, if code fails (causingstop
or other early-exit function), secondpar
command may not called. in function (not command-line), problem can avoided following, provided (as @rawr mentioned) in?par
:opar.1 <- par(pin = c(5, 3), lwd = 2, cex = 1.5) on.exit(par(opar.1)) plot(dose, druga, type = "b", pch = 19, lty = 2, col = "red")
(this want when executed in function ... on command-line, you'll need first instance of calling
par
directly.)using
withr
package:library(withr) message('## before with_par') # ## before with_par str(par(c('pin', 'lwd', 'cex'))) # list of 3 # $ pin: num [1:2] 5.76 5.16 # $ lwd: num 1 # $ cex: num 1 with_par(list(pin = c(5, 3), lwd = 2, cex = 1.5), { plot(dose, druga, type="b", pch=19, lty=2, col="red") message('## inside with_par') str(par(c('pin', 'lwd', 'cex'))) }) # ## inside with_par # list of 3 # $ pin: num [1:2] 5 3 # $ lwd: num 2 # $ cex: num 1.5 message('## after with_par') # ## after with_par str(par(c('pin', 'lwd', 'cex'))) # list of 3 # $ pin: num [1:2] 5.76 5.16 # $ lwd: num 1 # $ cex: num 1
the
message
,str
calls before, inside, , afterwith_par
demonstrate parameters changed inside code block, relatively safe both in functions , on command-line.
both methods provide identical par
-adjusted plots.
Comments
Post a Comment