分别注释刻面图的AUC值

时间:2018-09-20 09:46:48

标签: r ggplot2 label roc

是否有一种方法可以分别注释刻面图的AUC值?下面的示例代码在两个图中都标注了两个AUC值。这对我来说尤其成问题,因为我需要绘制约10个ROC图,这使得注释非常长/难以理解,如下所示。谢谢。

library(plotROC)

set.seed(2529)
D.ex <- rbinom(200, size = 1, prob = .5)
M1 <- rnorm(200, mean = D.ex, sd = .65)
M2 <- rnorm(200, mean = D.ex, sd = 1.5)
M3 <- rnorm(200, mean = D.ex, sd = 1.7)
M4 <- rnorm(200, mean = D.ex, sd = 1.8)
M5 <- rnorm(200, mean = D.ex, sd = 1.9)
M6 <- rnorm(200, mean = D.ex, sd = 2.0)
M7 <- rnorm(200, mean = D.ex, sd = 2.2)
M8 <- rnorm(200, mean = D.ex, sd = 2.5)
M9 <- rnorm(200, mean = D.ex, sd = 2.7)
M10 <- rnorm(200, mean = D.ex, sd = 3.0)

test <- data.frame(D = D.ex, D.str = c("Healthy", "Ill")[D.ex + 1], M1 = M1, M2 = M2, M3 = M3, M4 = M4, M5 = M5, M6 = M6, M7 = M7, M8 = M8, M9 = M9, M10 = M10,
                   stringsAsFactors = FALSE)

longtest <- melt_roc(test, "D", c("M1", "M2", "M3", "M4", "M5", "M6", "M7", "M8", "M9", "M10"))

pairplot <- ggplot(longtest, aes(d = D, m = M)) + geom_roc() + facet_wrap(~name)

pairplot + geom_rocci(linetype = 1)

pairplot + 
  style_roc(theme = theme_grey) +
  theme(axis.text = element_text(colour = "blue")) +
  ggtitle("Themes and annotations") + 
  facet_wrap(~name) +
  annotate("text", x = .75, y = .25, label = paste("AUC =", round(calc_auc(pairplot)["AUC"], 2))) 

enter image description here

1 个答案:

答案 0 :(得分:1)

由于通往每个方面的字符串都是相同的,这意味着注释的输入向量可能只包含一个值。您可以运行以下命令来验证情况是否正确:

paste("AUC =", round(calc_auc(pairplot)["AUC"], 2))

[1] "AUC = c(0.83, 0.6, 0.68, 0.63, 0.67, 0.64, 0.68, 0.63, 0.61, 0.59)"

这确实是一个值。

如果您看一下进行舍入的部分,它将返回一个数据帧:

round(calc_auc(pairplot)["AUC"], 2)

    AUC
1  0.83
2  0.60
3  0.68
4  0.63
5  0.67
6  0.64
7  0.68
8  0.63
9  0.61
10 0.59

,它将数据帧的字符串表示形式粘贴到前缀“ AUC =“。您可以通过返回值的向量而不是将整个数据帧传递给paste函数来解决此问题。

paste("AUC = ", round(calc_auc(pairplot)[["AUC"]], 2))

# or equally

paste("AUC = ", round(calc_auc(pairplot)$AUC, 2))

然后您将获得所需的结果。

enter image description here