R ggplot添加新的roc曲线

时间:2016-02-07 20:39:05

标签: r ggplot2 roc

我想将一条ROC曲线添加到ggplot图表中,但它会返回一个错误 代码。

  library(ggplot2)
  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)

  test <- data.frame(D = D.ex, D.str = c("Healthy", "Ill")[D.ex + 1], 
                     M1 = M1, M2 = M2, stringsAsFactors = FALSE)
  plot<-ggplot(longtest, aes(d = D, m = M1 )) + geom_roc() + style_roc()
  plot

没关系,但是如果我添加新的ROC线它的返回错误

plot<-ggplot(longtest, aes(d = D, m = M1 )) + geom_roc() + style_roc()
plot+ggplot(test, aes(d = D, m = M2)) + geom_roc()
  

p + o出错:二元运算符的非数字参数另外:   警告消息:不兼容的方法(“+ .gg”,“Ops.data.frame”)   “+”

我如何添加新的线条和颜色所有线条不同的颜色,并添加图例

1 个答案:

答案 0 :(得分:6)

将数据框从宽格式转换为长格式,然后将变量名称映射到美学映射中的线条颜色:

ggplot(melt_roc(test, "D", c("M1", "M2")), 
       aes(d = D, m = M, color = name)) + 
    geom_roc() + 
    style_roc()

enter image description here

如果你愿意,你也可以这样做:

ggplot() + 
  geom_roc(aes(d = D, m = M1, color="roc1"), test) + 
  geom_roc(aes(d = D, m = M2, color="roc2"), test) + 
  scale_color_manual(values=c("roc1"="red", "roc2"="blue"), 
                     name="color legend", guide="legend") + 
  style_roc()