我正在创建一个使用绘图函数的简单绘图,该函数调用另一个绘图函数来通过它添加一条平滑线。第二个函数lines()
接受一些标准的基本图形参数。第一个函数也使用相同的命名参数。现在我想改变第二个情节的一些元素而不改变第一个情节。我至少有几个我能想到的选择:
- 分别绘制每个
- 重写函数以将唯一命名的参数包含到第二个绘图函数中
- 为此可能使用 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])
}
}