[英]How can I label points in a Taylor diagram?
The plotrix package has a function called taylor.diagram
that plots two vectors - one representing data and the other model output. plotrix包有一个名为
taylor.diagram
的函数,它绘制了两个向量 - 一个表示数据,另一个表示模型输出。
Here is an example: 这是一个例子:
require(plotrix)
set.seed(10)
data <- sort(runif(100, 8,12))
model <- sort(rnorm(100, 10, 4))
taylor.diagram(data, model)
And in this example I want to update the plot after improving the model: 在这个例子中,我想在改进模型后更新图:
model2 <- sort(rnorm(100, 10,2))
taylor.diagram(data, model2, add = TRUE)
To produce this: 要产生这个:
How can I add labels such as "Model 1" and "Model 2" to identify these points? 如何添加“模型1”和“模型2”等标签来识别这些点? (Update: with the label position determined from the model values rather than done post-hoc)
(更新:标签位置由模型值确定,而不是事后完成)
A third solution is to create a modified version of the function taylor.diagram
that includes a text label. 第三种解决方案是创建包含文本标签的函数
taylor.diagram
的修改版本。 In this case all there is to do is adding a parameter, say text
, and after the call to points
in the original function (2 lines before the closing braces), the line text(sd.f * R, sd.f * sin(acos(R)), labels=text, pos=3)
. 在这种情况下,所有要做的就是添加一个参数,比如
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)
}
Then simply supply a label name in the text
argument: 然后只需在
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")
Here are two approaches 这有两种方法
example(taylor.diagram)
shows a decent approach to placing a legend in the upper right corner (at 1.5*sd(data), 1.5*sd(data)
), but this would require different colors for the two points. example(taylor.diagram)
显示了将图例放置在右上角( 1.5*sd(data), 1.5*sd(data)
)的合适方法,但这需要两个点的不同颜色。
Another option would be to calculate the locations based on the equations from the original Taylor 2001 reference - or copy them from source code to the taylor.diagram
function, near 另一种选择是根据原始Taylor 2001参考中的方程计算位置 - 或者将它们从源代码复制到
taylor.diagram
函数,附近
dy <- 1.1 # text offset coefficient sd.f <- sd(model) R <- cor(data, model, use = 'pairwise') x <- sd.f * R y <- sd.f * sin(acos(R)) + dy * sd.f text(x, y, "Model")
You would need to calculate these for each model, but only the model input and the label would change. 您需要为每个模型计算这些,但只有模型输入和标签会发生变化。 You probably want to keep the offset the same as well.
您可能希望保持偏移量相同。
The same way you label everything in base graphics, using text
: 使用
text
标记基本图形中的所有内容的方式相同:
text(1.5,0.5,labels = "Model2")
text(3.5,1,labels = "Model1")
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.