plotrix 包有一个名为 taylor.diagram
的函数,它绘制两个向量——一个代表数据,另一个代表模型输出。
下面是一个例子:
require(plotrix)
set.seed(10)
data <- sort(runif(100, 8,12))
model <- sort(rnorm(100, 10, 4))
taylor.diagram(data, model)
在这个例子中,我想在改进模型后更新图:
model2 <- sort(rnorm(100, 10,2))
taylor.diagram(data, model2, add = TRUE)
要产生这个:
如何添加诸如“模型 1”和“模型 2”之类的标签来识别这些点? (更新:标签位置由模型值确定而不是事后完成)
最佳答案
第三种解决方案是创建包含文本标签的函数 taylor.diagram
的修改版本。在这种情况下,所有要做的就是添加一个参数,比如 text
,在原始函数中调用 points
之后(右大括号前的 2 行),行 text(sd.f * R, sd.f * sin(acos(R)), labels=text, pos=3)
。
taylor.diagram.modified <- function (ref, model, add = FALSE, col = "red",
pch = 19, pos.cor = TRUE, xlab = "", ylab = "",
main = "Taylor Diagram", show.gamma = TRUE,
ngamma = 3, gamma.col = 8, sd.arcs = 0, ref.sd = FALSE,
grad.corr.lines = c(0.2, 0.4, 0.6, 0.8, 0.9), pcex = 1,
cex.axis = 1, normalize = FALSE, mar = c(5, 4, 6, 6),
text, ...) #the added parameter
{
grad.corr.full <- c(0, 0.2, 0.4, 0.6, 0.8, 0.9, 0.95, 0.99,1)
R <- cor(ref, model, use = "pairwise")
sd.r <- sd(ref)
sd.f <- sd(model)
if (normalize) {
... #I didn't copy here the full function because it's quite long: to obtain it
... #simply call `taylor.diagram` in the console or `edit(taylor.diagram)`.
}
S <- (2 * (1 + R))/(sd.f + (1/sd.f))^2
}
}
points(sd.f * R, sd.f * sin(acos(R)), pch = pch, col = col,
cex = pcex)
text(sd.f * R, sd.f * sin(acos(R)), #the line to add
labels=text, cex = pcex, pos=3) #You can change the pos argument to your liking
invisible(oldpar)
}
然后只需在
text
参数中提供标签名称:require(plotrix)
set.seed(10)
data <- sort(runif(100, 8,12))
model <- sort(rnorm(100, 10, 4))
taylor.diagram.modified(data, model, text="Model 1")
model2 <- sort(rnorm(100, 10,2))
taylor.diagram.modified(data, model2, add = TRUE, text="Model 2")
关于r - 如何在泰勒图中标记点?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/15147452/