使用GGPLOT创建ROC曲线

时间:2014-06-19 09:26:46

标签: r plot ggplot2

我有以下数据:

df <- structure(list(TPR = c(0.02, 0.04, 0.06, 0.08, 0.1, 0.12, 0.14, 
0.16, 0.18, 0.2, 0.22, 0.24, 0.26, 0.28, 0.3, 0.32, 0.34, 0.36, 
0.38, 0.4, 0.42, 0.44, 0.46, 0.48, 0.5, 0.52, 0.54, 0.56, 0.58, 
0.6, 0.62, 0.64, 0.64, 0.64, 0.66, 0.68, 0.7, 0.72, 0.74, 0.76, 
0.78, 0.8, 0.8, 0.82, 0.82, 0.84, 0.84, 0.84, 0.86, 0.86, 0.86, 
0.86, 0.88, 0.88, 0.9, 0.92, 0.92, 0.92, 0.92, 0.94, 0.94, 0.96, 
0.96, 0.96, 0.96, 0.96, 0.96, 0.98, 0.98, 0.98, 0.98, 0.98, 0.98, 
0.98, 0.98, 0.98, 0.98, 0.98, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0.02, 0.04, 0.06, 0.08, 0.1, 
0.12, 0.14, 0.16, 0.18, 0.2, 0.22, 0.24, 0.24, 0.26, 0.28, 0.3, 
0.32, 0.34, 0.36, 0.38, 0.4, 0.42, 0.42, 0.42, 0.44, 0.46, 0.48, 
0.5, 0.52, 0.54, 0.56, 0.58, 0.6, 0.6, 0.6, 0.6, 0.62, 0.62, 
0.62, 0.64, 0.66, 0.66, 0.68, 0.68, 0.68, 0.7, 0.72, 0.74, 0.76, 
0.78, 0.8, 0.8, 0.8, 0.82, 0.82, 0.84, 0.84, 0.84, 0.86, 0.86, 
0.86, 0.86, 0.86, 0.88, 0.88, 0.88, 0.9, 0.9, 0.9, 0.9, 0.9, 
0.9, 0.9, 0.92, 0.94, 0.96, 0.96, 0.96, 0.96, 0.96, 0.96, 0.96, 
0.98, 0.98, 0.98, 0.98, 0.98, 0.98, 0.98, 0.98, 0.98, 1, 1, 1, 
1, 1, 1, 1, 1, 1, 0.02, 0.04, 0.06, 0.08, 0.1, 0.1, 0.1, 0.12, 
0.14, 0.16, 0.18, 0.2, 0.22, 0.24, 0.24, 0.26, 0.28, 0.28, 0.3, 
0.32, 0.34, 0.36, 0.38, 0.4, 0.42, 0.42, 0.42, 0.42, 0.44, 0.44, 
0.44, 0.46, 0.48, 0.48, 0.5, 0.52, 0.54, 0.56, 0.58, 0.58, 0.6, 
0.62, 0.62, 0.62, 0.64, 0.66, 0.68, 0.68, 0.7, 0.72, 0.72, 0.72, 
0.72, 0.74, 0.74, 0.74, 0.76, 0.76, 0.78, 0.78, 0.8, 0.82, 0.84, 
0.84, 0.84, 0.86, 0.88, 0.88, 0.9, 0.9, 0.92, 0.92, 0.92, 0.92, 
0.92, 0.92, 0.92, 0.92, 0.94, 0.94, 0.96, 0.96, 0.96, 0.96, 0.98, 
0.98, 0.98, 0.98, 0.98, 0.98, 0.98, 0.98, 0.98, 0.98, 0.98, 0.98, 
1, 1, 1, 1, 0.02, 0.04, 0.06, 0.06, 0.06, 0.08, 0.08, 0.1, 0.12, 
0.14, 0.16, 0.16, 0.18, 0.2, 0.22, 0.24, 0.26, 0.28, 0.28, 0.3, 
0.32, 0.32, 0.34, 0.34, 0.36, 0.38, 0.4, 0.42, 0.42, 0.44, 0.46, 
0.46, 0.46, 0.48, 0.48, 0.5, 0.52, 0.54, 0.56, 0.56, 0.58, 0.6, 
0.62, 0.64, 0.64, 0.64, 0.64, 0.64, 0.66, 0.68, 0.68, 0.7, 0.7, 
0.7, 0.7, 0.7, 0.72, 0.74, 0.76, 0.76, 0.78, 0.78, 0.78, 0.8, 
0.8, 0.82, 0.82, 0.84, 0.86, 0.86, 0.86, 0.86, 0.88, 0.9, 0.92, 
0.92, 0.92, 0.92, 0.92, 0.92, 0.92, 0.92, 0.92, 0.94, 0.94, 0.94, 
0.94, 0.94, 0.94, 0.96, 0.98, 0.98, 0.98, 0.98, 1, 1, 1, 1, 1, 
1), FPR = c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0.02, 0.04, 0.04, 
0.04, 0.04, 0.04, 0.04, 0.04, 0.04, 0.04, 0.06, 0.06, 0.08, 0.08, 
0.1, 0.12, 0.12, 0.14, 0.16, 0.18, 0.18, 0.2, 0.2, 0.2, 0.22, 
0.24, 0.26, 0.26, 0.28, 0.28, 0.3, 0.32, 0.34, 0.36, 0.38, 0.38, 
0.4, 0.42, 0.44, 0.46, 0.48, 0.5, 0.52, 0.54, 0.56, 0.58, 0.58, 
0.6, 0.62, 0.64, 0.66, 0.68, 0.7, 0.72, 0.74, 0.76, 0.78, 0.8, 
0.82, 0.84, 0.86, 0.88, 0.9, 0.92, 0.94, 0.96, 0.98, 1, 0, 0, 
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0.02, 0.02, 0.02, 0.02, 0.02, 0.02, 
0.02, 0.02, 0.02, 0.02, 0.04, 0.06, 0.06, 0.06, 0.06, 0.06, 0.06, 
0.06, 0.06, 0.06, 0.06, 0.08, 0.1, 0.12, 0.12, 0.14, 0.16, 0.16, 
0.16, 0.18, 0.18, 0.2, 0.22, 0.22, 0.22, 0.22, 0.22, 0.22, 0.22, 
0.24, 0.26, 0.26, 0.28, 0.28, 0.3, 0.32, 0.32, 0.34, 0.36, 0.38, 
0.4, 0.4, 0.42, 0.44, 0.44, 0.46, 0.48, 0.5, 0.52, 0.54, 0.56, 
0.56, 0.56, 0.56, 0.58, 0.6, 0.62, 0.64, 0.66, 0.68, 0.68, 0.7, 
0.72, 0.74, 0.76, 0.78, 0.8, 0.82, 0.84, 0.84, 0.86, 0.88, 0.9, 
0.92, 0.94, 0.96, 0.98, 1, 0, 0, 0, 0, 0, 0.02, 0.04, 0.04, 0.04, 
0.04, 0.04, 0.04, 0.04, 0.04, 0.06, 0.06, 0.06, 0.08, 0.08, 0.08, 
0.08, 0.08, 0.08, 0.08, 0.08, 0.1, 0.12, 0.14, 0.14, 0.16, 0.18, 
0.18, 0.18, 0.2, 0.2, 0.2, 0.2, 0.2, 0.2, 0.22, 0.22, 0.22, 0.24, 
0.26, 0.26, 0.26, 0.26, 0.28, 0.28, 0.28, 0.3, 0.32, 0.34, 0.34, 
0.36, 0.38, 0.38, 0.4, 0.4, 0.42, 0.42, 0.42, 0.42, 0.44, 0.46, 
0.46, 0.46, 0.48, 0.48, 0.5, 0.5, 0.52, 0.54, 0.56, 0.58, 0.6, 
0.62, 0.64, 0.64, 0.66, 0.66, 0.68, 0.7, 0.72, 0.72, 0.74, 0.76, 
0.78, 0.8, 0.82, 0.84, 0.86, 0.88, 0.9, 0.92, 0.94, 0.94, 0.96, 
0.98, 1, 0, 0, 0, 0.02, 0.04, 0.04, 0.06, 0.06, 0.06, 0.06, 0.06, 
0.08, 0.08, 0.08, 0.08, 0.08, 0.08, 0.08, 0.1, 0.1, 0.1, 0.12, 
0.12, 0.14, 0.14, 0.14, 0.14, 0.14, 0.16, 0.16, 0.16, 0.18, 0.2, 
0.2, 0.22, 0.22, 0.22, 0.22, 0.22, 0.24, 0.24, 0.24, 0.24, 0.24, 
0.26, 0.28, 0.3, 0.32, 0.32, 0.32, 0.34, 0.34, 0.36, 0.38, 0.4, 
0.42, 0.42, 0.42, 0.42, 0.44, 0.44, 0.46, 0.48, 0.48, 0.5, 0.5, 
0.52, 0.52, 0.52, 0.54, 0.56, 0.58, 0.58, 0.58, 0.58, 0.6, 0.62, 
0.64, 0.66, 0.68, 0.7, 0.72, 0.74, 0.74, 0.76, 0.78, 0.8, 0.82, 
0.84, 0.84, 0.84, 0.86, 0.88, 0.9, 0.9, 0.92, 0.94, 0.96, 0.98, 
1), GeneSet = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1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L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 
3L, 3L, 3L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 
4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 
4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 
4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 
4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 
4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 
4L, 4L, 4L, 4L, 4L, 4L, 4L), .Label = c("Distort = 1", "Distort = 1.5", 
"Distort = 2", "Distort = 2.5"), class = "factor")), .Names = c("TPR", 
"FPR", "GeneSet"), row.names = c(NA, -400L), class = "data.frame")

但为什么以下代码无法创建所需的图?

library(ggplot2)
library(RColorBrewer)
p <- qplot(FPR, TPR, data = df, geom = "blank", main = "ROC curve", xlab = "False Positive Rate (1-Specificity)", ylab = "True Positive Rate (Sensitivity)" ) 
p <- p +  geom_line(aes(x = FPR, y = TPR, data = data, colour = GeneSet), size = 2, alpha = 0.7) + scale_colour_manual(values=colors)
p 

我收到此错误消息:

Don't know how to automatically pick scale for object of type data.frame. Defaulting to continuous
Error: Aesthetics must either be length one, or the same length as the dataProblems:data

理想的情节是:

enter image description here

1 个答案:

答案 0 :(得分:12)

您不需要绘制geom="blank"geom_line() - 只需geom_line()即可完成。只有颜色不能被复制,因为没有提供可变颜色。

ggplot(df,aes(FPR,TPR,color=GeneSet))+geom_line(size = 2, alpha = 0.7)+
      labs(title= "ROC curve", 
           x = "False Positive Rate (1-Specificity)", 
           y = "True Positive Rate (Sensitivity)")