5

別のプロット関数を呼び出して平滑化された線を追加するプロット関数を使用する単純なプロットを作成しています。2 番目の関数はlines()、いくつかの標準の基本グラフィック引数を取ります。最初の関数も同じ名前の引数を使用します。ここで、最初のプロットを変更せずに、2 番目のプロットのいくつかの要素を変更したいと思います。私が考えることができる少なくともいくつかのオプションがあります:

  • それぞれ別々にプロットする
  • 関数を書き直して、一意の名前の引数を 2 番目のプロット関数に含めます。
  • おそらくggplot2を使用して、このための新しいプロット関数を作成します

そして、ネストされた関数に引数の値を渡すことは、何も書き直さずに可能であると考えるようになりました。この質問では、プロットを行う関数を求めているわけではないことに注意してください-必要に応じて独自の関数を書くことができます.引数リストで引数を指定することが可能かどうか尋ねていtraceplotますlines。 traceplot の名前付き引数でもあります。例:

require(coda)
require(geoRglm)

# Some example data
mcmc <- c(0, 0, 0, 0, 0.5, 0.5, 0.5, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0.5, 0, 0, 0, 0, 0, 0, 0, 0, 0.5, 
0.5, 1, 0.5, 0, 0.5, 0.5, 0.5, 0.5, 0, 0, 0, 0, 0, 0, 1, 0, 0, 
0, 0.5, 0.5, 0, 0.5, 1, 0, 0.5, 1, 0.5, 0.5, 0.5, 0, 0.5, 0.5, 
0, 0, 0, 0, 0, 0, 0, 0.5, 0, 0, 0.5, 0, 0, 0, 0, 0, 0, 0, 0, 
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0.5, 0, 0, 0.5, 0, 0, 0, 
0.5, 0.5, 0, 0, 0, 0, 0, 0, 0, 0, 0.5, 0, 0.5, 1, 1, 0, 1, 0, 
0, 0.5, 0, 0.5, 0.5, 0, 0, 0, 0, 0, 0, 0.5, 0.5, 0.5, 0, 1, 0.5, 
1, 1, 1, 1, 1, 1, 1, 1, 1, 1.5, 1.5, 1.5, 1.5, 1.5, 1.5, 1.5, 
1.5, 1, 1.5, 1, 1, 1, 1, 0.5, 0.5, 1, 1, 1.5, 1.5, 1.5, 1, 1.5, 
1.5, 1.5, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1.5, 1, 1.5, 1.5, 
1, 1.5, 1.5, 1, 1, 1, 1, 1)

# Input parameters for mcmc object
input <- structure(list(S.scale = 3.75e-05, Htrunc = "default", S.start = "default", 
    burn.in = 0, thin = 100, n.iter = 20000, phi.start = 0, phi.scale = 1), .Names = c("S.scale", 
"Htrunc", "S.start", "burn.in", "thin", "n.iter", "phi.start", 
"phi.scale"), class = "mcmc.geoRglm")


# create mcmc object
mcmc_obj <- create.mcmc.coda( mcmc , mcmc.input = input )


#Plot - smooth = TRUE draws the smooth line through the data
traceplot( mcmc_obj , smooth = TRUE , main = "Default")


# Changing the colour argument passes the same argument through to the lines function called to plot the smooth line
traceplot( mcmc_obj , smooth = TRUE , col = 2 , main = "Change colour to red")

ここに画像の説明を入力

# In the source code of traceplot 'lines()' is called to plot the smooth line.
# Can I specify a colour argument to be passed only to lines without altering
# the function?
# Excerpt from the source code of traceplot:

function (x, smooth = FALSE, col = 1:6, type = "l", ylab = "", 
    ...) 
{
... some code here then...
if (smooth) {
            for (k in 1:nchain(x)) lines(lowess(xp, yp[, k]) 
        }
} 
4

1 に答える 1

1

2 つのリストを使用して、さまざまなパラメーターを渡すことができます。

myfun <- function(x,y,par1=NULL,par2=NULL){
  if(is.null(par1)){
    plot(x,y)
  }else{
    do.call(plot,c(list(x=x,y=y),par1))
  }
  if(is.null(par2)){
    lines(x,y)
  }else{
    do.call(lines,c(list(x=x,y=y),par2))
  }
}

par1for params toplotおよびpar2for params to lines. 次に、do.callそのようなパラメーターがある場合に使用します。

myfun(1:10,1:10) # no params
myfun(1:10,1:10,par1=list(pch=20)) # params only to plot
myfun(1:10,1:10,par1=list(pch=20),par2=list(col="red",lwd=2)) # params to plot and lines
于 2013-03-26T01:12:03.983 に答える