【问题标题】:How can I label points in a Taylor diagram?如何在泰勒图中标记点?
【发布时间】:2013-02-28 23:08:08
【问题描述】:

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”等标签来识别这些点? (更新:标签位置由模型值确定,而不是事后完成)

【问题讨论】:

    标签: r plot


    【解决方案1】:

    第三种解决方案是创建包含文本标签的函数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")
    

    【讨论】:

      【解决方案2】:

      这里有两种方法

      1. example(taylor.diagram) 展示了一种在右上角(1.5*sd(data), 1.5*sd(data))放置图例的不错方法,但这需要两个点使用不同的颜色。

      2. 另一种选择是根据原始 Taylor 2001 reference 中的方程式计算位置 - 或将它们从源代码复制到附近的 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")
        

        您需要为每个模型计算这些,但只有模型输入和标签会改变。您可能还希望保持相同的偏移量。

      【讨论】:

        【解决方案3】:

        与在基本图形中标记所有内容的方式相同,使用 text

        text(1.5,0.5,labels = "Model2")
        text(3.5,1,labels = "Model1")
        

        【讨论】:

        • 我希望能够在不事先了解他们的位置的情况下绘制它们
        • @Abe taylor.diagram 的文档表明它不返回点的位置(或根本不返回任何绘图信息),所以我认为您在这里没有很多选择。
        猜你喜欢
        • 1970-01-01
        • 2014-09-19
        • 1970-01-01
        • 2021-04-04
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-04-10
        相关资源
        最近更新 更多