简体   繁体   English

R:Plot 使用 ROCR 的多个不同颜色的 ROC 曲线

[英]R: Plot multiple different coloured ROC curves using ROCR

The following code was taken from @adibender answer to "Multiple ROC curves in one plot ROCR".以下代码摘自@adibender 对“一个 plot ROCR 中的多个 ROC 曲线”的回答。 The code is partly from?plot.performance.代码部分来自?plot.performance。

library(ROCR)
data(ROCR.simple)
preds <- cbind(p1 = ROCR.simple$predictions, 
            p2 = abs(ROCR.simple$predictions + 
            rnorm(length(ROCR.simple$predictions), 0, 0.1)))

pred.mat <- prediction(preds, labels = matrix(ROCR.simple$labels, 
            nrow = length(ROCR.simple$labels), ncol = 2) )

perf.mat <- performance(pred.mat, "tpr", "fpr")
plot(perf.mat)

I want to illustrate several ROC curves in a single plot, like the code above, using the r package ROCR.我想在单个 plot 中说明几个 ROC 曲线,就像上面的代码一样,使用 r package ROCR。 However, i would want the ROC curves to be in different colours.但是,我希望 ROC 曲线具有不同的颜色。 How do i apply different colours to different curves?如何将不同的颜色应用于不同的曲线? Thanks, in advance.提前致谢。

Try this (you can do it with ROCR ): 试试这个(您可以使用ROCR来做到):

library(ROCR)
data(ROCR.simple)
preds <- cbind(p1 = ROCR.simple$predictions, 
               p2 = abs(ROCR.simple$predictions + 
                          rnorm(length(ROCR.simple$predictions), 0, 0.1)))
n <- 2 # you have n models
colors <- c('red', 'blue') # 2 colors
for (i in 1:n) {
   plot(performance(prediction(preds[,i],ROCR.simple$labels),"tpr","fpr"), 
                                           add=(i!=1),col=colors[i],lwd=2)
}

在此处输入图片说明

The plot function for ROCR objects does not seem to offer this option. ROCR对象的plot功能似乎不提供此选项。 So, you'll have to pick apart the object and do it manually. 因此,您必须将对象拆开并手动进行。

For instance, using ggplot2 , 例如,使用ggplot2

library(ggplot2)
df <- data.frame(Curve=as.factor(rep(c(1,2), each=length(perf.mat@x.values[[1]]))), 
                 FalsePositive=c(perf.mat@x.values[[1]],perf.mat@x.values[[2]]),
                 TruePositive=c(perf.mat@y.values[[1]],perf.mat@y.values[[2]]))
plt <- ggplot(df, aes(x=FalsePositive, y=TruePositive, color=Curve)) + geom_line()
print(plt)

You can use just plot with add=TRUE您可以仅使用 plot 和 add=TRUE

pred <- prediction( predicted1, response )
pred2 <- prediction(predicted2, response)
perf <- performance( pred, "tpr", "fpr" )
perf2 <- performance(pred2, "tpr", "fpr")
plot( perf, colorize = FALSE,  type="l",col="blue")
abline(a=0,b=1)
plot(perf2,colorize = FALSE, add = TRUE,  type="l", lty=3,col="black")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM