我正在尝试使用ggplot2在单个绘图上绘制多个ROC曲线。这是我走了多远:
ggroc2 <- function(columns, data = mtcars, classification = "am",
interval = 0.2, breaks = seq(0, 1, interval)){
require(pROC)
require(ggplot2)
#The frame for the plot
g <- ggplot() + geom_segment(aes(x = 0, y = 1, xend = 1,yend = 0)) +
scale_x_reverse(name = "Specificity",limits = c(1,0), breaks = breaks,
expand = c(0.001,0.001)) +
scale_y_continuous(name = "Sensitivity", limits = c(0,1), breaks =
breaks, expand = c(0.001, 0.001)) +
theme_classic() + coord_equal()
#The loop to calculate ROC's and add them as new layers
for(i in 1:length(columns)){
croc <- roc(data[,classification], data[,columns[i]])
plotx <- rev(croc$specificities)
ploty <- rev(croc$sensitivities)
g <- g + geom_step(aes(x=plotx, y=ploty))
}
g
}
#Sample graph
ggroc2(c("mpg", "disp", "drat", "wt"))
问题在于仅columns
绘制了列表中的最后一个参数。aes()
阅读该问题的答案后,我发现该问题必须与懒惰评估有关。该示例使用geom_segment()
,并且aes()
完全删除后解决了问题。它对我不起作用,因为我需要以某种方式映射数据。当我移开aes()
这里时,什么也没画。如何解决geom_
依赖于的惰性评估问题aes()
?
这是您的代码的有效版本。
最终的图形结果不是很好,应该加以改进。
ggroc2 <- function(columns, data = mtcars, classification = "am",
interval = 0.2, breaks = seq(0, 1, interval)){
require(pROC)
require(ggplot2)
#The frame for the plot
g <- ggplot() + geom_segment(aes(x = 0, y = 1, xend = 1,yend = 0)) +
scale_x_reverse(name = "Specificity",limits = c(1,0), breaks = breaks,
expand = c(0.001,0.001)) +
scale_y_continuous(name = "Sensitivity", limits = c(0,1), breaks =
breaks, expand = c(0.001, 0.001)) +
theme_classic() + coord_equal()
#The loop to calculate ROC's and add them as new layers
cols <- palette()
for(i in 1:length(columns)){
croc <- roc(data[,classification], data[,columns[i]])
sens_spec <- data.frame(spec=rev(croc$specificities),
sens=rev(croc$sensitivities))
g <- g + geom_step(aes(x=spec, y=sens), data=sens_spec, col=cols[i], lwd=1)
}
g
}
#Sample graph
ggroc2(c("mpg", "disp", "drat", "wt"))
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句